diff --git a/.devcontainer/bashrc b/.devcontainer/bashrc index 934f94a..3cb8d48 100644 --- a/.devcontainer/bashrc +++ b/.devcontainer/bashrc @@ -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 diff --git a/origin/go.mod b/origin/go.mod index 5d703da..869da85 100644 --- a/origin/go.mod +++ b/origin/go.mod @@ -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 ) diff --git a/origin/go.sum b/origin/go.sum index e4dca1e..05f9555 100644 --- a/origin/go.sum +++ b/origin/go.sum @@ -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= diff --git a/origin/main.go b/origin/main.go index 8c49855..fba67ea 100644 --- a/origin/main.go +++ b/origin/main.go @@ -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" @@ -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 @@ -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) @@ -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 } @@ -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) } diff --git a/origin/public/404.html b/origin/public/404.html index 0b5cbdb..51f3371 100644 --- a/origin/public/404.html +++ b/origin/public/404.html @@ -5,6 +5,7 @@ Not Found -

404 page not found

+

404 page not found
+ Back to Home

diff --git a/origin/public/index.html b/origin/public/index.html index 098628b..c7af59c 100644 --- a/origin/public/index.html +++ b/origin/public/index.html @@ -17,11 +17,16 @@
-

Cached files:

+

Dynamic files:

+
+ +

Cached files:

+