Skip to content

Commit

Permalink
origin: Improve files handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dev committed Dec 26, 2023
1 parent 3e13b7e commit bff509e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 81 deletions.
6 changes: 6 additions & 0 deletions .devcontainer/bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ alias ga="git add"
alias gu="git add -u"
alias gm="git commit -m"
alias gp="git push"


export GOPATH="$HOME/.local/gopath"
export GOROOT="$HOME/.local/go"
export PATH="$PATH:$GOROOT/bin:$GOPATH/bin"
export GOPROXY=direct
1 change: 1 addition & 0 deletions origin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/skerkour/cloudflare-for-speed-and-security/origin
go 1.21.5

require (
github.com/bloom42/stdx v0.0.0-20231226155351-fa0ff3a7e645
github.com/go-chi/chi/v5 v5.0.11
golang.org/x/crypto v0.17.0
)
Expand Down
2 changes: 2 additions & 0 deletions origin/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/bloom42/stdx v0.0.0-20231226155351-fa0ff3a7e645 h1:1QfapnvgM6/P3MPKQftTncM6vz6TN6H3xNSR3C/r9RU=
github.com/bloom42/stdx v0.0.0-20231226155351-fa0ff3a7e645/go.mod h1:EZT4MIjPfxv03r8l4AWIuoaUqJarn3x6B1JxF3rkDGA=
github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
Expand Down
92 changes: 14 additions & 78 deletions origin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"syscall"
"time"

"github.com/bloom42/stdx/httpx"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"golang.org/x/crypto/acme/autocert"
Expand Down Expand Up @@ -70,7 +71,11 @@ func main() {
slog.String("https_lets_encrypt_email", config.httpsLetsEncryptEmail),
))

router := loadRouter()
router, err := loadRouter()
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}

var gracefulShutdownWaitGroup sync.WaitGroup
var httpServer *http.Server
Expand Down Expand Up @@ -192,7 +197,7 @@ func loadConfig() (config Config, err error) {
return
}

func loadRouter() (router chi.Router) {
func loadRouter() (router chi.Router, err error) {
router = chi.NewRouter()

router.Use(middleware.Recoverer)
Expand All @@ -202,12 +207,13 @@ func loadRouter() (router chi.Router) {
router.Use(middleware.CleanPath)

router.Get("/", IndexHandler)
router.Get("/100k.bin", BinHandler)
router.Get("/100k.css", CssHandler)
router.Get("/100k.jpg", JpgHandler)
router.Get("/100k.js", JsHandler)

router.NotFound(NotFoundHandler)
webappHandler, err := httpx.WebappHandler(assetsFS, "404.html", http.StatusNotFound)
if err != nil {
return
}

router.NotFound(webappHandler)

return
}
Expand All @@ -218,80 +224,10 @@ func IndexHandler(w http.ResponseWriter, req *http.Request) {

fileInfo, _ := file.Stat()

w.Header().Add("Content-Type", "text/html; charset=utf-8")
w.Header().Add("Content-Length", strconv.Itoa(int(fileInfo.Size())))
w.Header().Add("Cache-Control", "public, no-cache, must-revalidate")

w.WriteHeader(http.StatusOK)
io.Copy(w, file)
}

func BinHandler(w http.ResponseWriter, req *http.Request) {
file, _ := assetsFS.Open("100k.bin")
defer file.Close()

fileInfo, _ := file.Stat()

w.Header().Add("Content-Type", "application/octet-stream")
w.Header().Add("Content-Length", strconv.Itoa(int(fileInfo.Size())))
w.Header().Add("Cache-Control", "public, max-age=31536000, immutable")

w.WriteHeader(http.StatusOK)
io.Copy(w, file)
}

func CssHandler(w http.ResponseWriter, req *http.Request) {
file, _ := assetsFS.Open("100k.css")
defer file.Close()

fileInfo, _ := file.Stat()

w.Header().Add("Content-Type", "text/css")
w.Header().Add("Content-Length", strconv.Itoa(int(fileInfo.Size())))
w.Header().Add("Cache-Control", "public, max-age=31536000, immutable")

w.WriteHeader(http.StatusOK)
io.Copy(w, file)
}

func JpgHandler(w http.ResponseWriter, req *http.Request) {
file, _ := assetsFS.Open("100k.jpg")
defer file.Close()

fileInfo, _ := file.Stat()

w.Header().Add("Content-Type", "image/jpeg")
w.Header().Add("Content-Length", strconv.Itoa(int(fileInfo.Size())))
w.Header().Add("Cache-Control", "public, max-age=31536000, immutable")

w.WriteHeader(http.StatusOK)
io.Copy(w, file)
}

func JsHandler(w http.ResponseWriter, req *http.Request) {
file, _ := assetsFS.Open("100k.js")
defer file.Close()

fileInfo, _ := file.Stat()

w.Header().Add("Content-Type", "application/javascript")
w.Header().Add("Content-Length", strconv.Itoa(int(fileInfo.Size())))
w.Header().Add("Cache-Control", "public, max-age=31536000, immutable")

w.WriteHeader(http.StatusOK)
io.Copy(w, file)
}

func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
file, _ := assetsFS.Open("404.html")
defer file.Close()

fileInfo, _ := file.Stat()

w.Header().Add("Content-Type", "text/html; charset=utf-8")
w.Header().Add("Content-Length", strconv.Itoa(int(fileInfo.Size())))
w.Header().Add("Cache-Control", "private, no-cache, no-store, must-revalidate")

w.WriteHeader(http.StatusNotFound)
w.WriteHeader(http.StatusOK)
io.Copy(w, file)
}
3 changes: 2 additions & 1 deletion origin/public/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<title>Not Found</title>
</head>
<body>
<p>404 page not found</p>
<p>404 page not found <br />
<a href="/">Back to Home</a></p>
</body>
</html>
9 changes: 7 additions & 2 deletions origin/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
</ul>
<hr />

<p>Cached files:</p>
<p>Dynamic files:</p>
<ul>
<li><a href="/100k.bin">/100k.bin</a></li>
<li><a href="/100k.css">/100k.css</a></li>
<li><a href="/100k.jpg">/100k.jpg</a></li>
</ul>
<hr />

<p>Cached files:</p>
<ul>
<li><a href="/100k.css">/100k.css</a></li>
<li><a href="/100k.js">/100k.js</a></li>
</ul>

Expand Down

0 comments on commit bff509e

Please sign in to comment.