From 167e3f2cfac2c3374f81bb705766ee9e71386b55 Mon Sep 17 00:00:00 2001 From: dev <> Date: Tue, 26 Dec 2023 14:39:28 +0000 Subject: [PATCH] origin: minor improvements --- origin/main.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/origin/main.go b/origin/main.go index fab4551..8c49855 100644 --- a/origin/main.go +++ b/origin/main.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "io/fs" "log/slog" "net/http" "os" @@ -22,7 +23,8 @@ import ( ) //go:embed public/* -var assetsFs embed.FS +var publicFS embed.FS +var assetsFS fs.FS const ( DEFAULT_PORT_HTTP = "8080" @@ -38,6 +40,10 @@ type Config struct { httpsLetsEncryptEmail string } +func init() { + assetsFS, _ = fs.Sub(publicFS, "public") +} + func main() { // for graceful shutdown ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill, @@ -207,7 +213,7 @@ func loadRouter() (router chi.Router) { } func IndexHandler(w http.ResponseWriter, req *http.Request) { - file, _ := assetsFs.Open("public/index.html") + file, _ := assetsFS.Open("index.html") defer file.Close() fileInfo, _ := file.Stat() @@ -221,7 +227,7 @@ func IndexHandler(w http.ResponseWriter, req *http.Request) { } func BinHandler(w http.ResponseWriter, req *http.Request) { - file, _ := assetsFs.Open("public/100k.bin") + file, _ := assetsFS.Open("100k.bin") defer file.Close() fileInfo, _ := file.Stat() @@ -235,7 +241,7 @@ func BinHandler(w http.ResponseWriter, req *http.Request) { } func CssHandler(w http.ResponseWriter, req *http.Request) { - file, _ := assetsFs.Open("public/100k.css") + file, _ := assetsFS.Open("100k.css") defer file.Close() fileInfo, _ := file.Stat() @@ -249,7 +255,7 @@ func CssHandler(w http.ResponseWriter, req *http.Request) { } func JpgHandler(w http.ResponseWriter, req *http.Request) { - file, _ := assetsFs.Open("public/100k.jpg") + file, _ := assetsFS.Open("100k.jpg") defer file.Close() fileInfo, _ := file.Stat() @@ -263,7 +269,7 @@ func JpgHandler(w http.ResponseWriter, req *http.Request) { } func JsHandler(w http.ResponseWriter, req *http.Request) { - file, _ := assetsFs.Open("public/100k.js") + file, _ := assetsFS.Open("100k.js") defer file.Close() fileInfo, _ := file.Stat() @@ -277,7 +283,7 @@ func JsHandler(w http.ResponseWriter, req *http.Request) { } func NotFoundHandler(w http.ResponseWriter, req *http.Request) { - file, _ := assetsFs.Open("public/404.html") + file, _ := assetsFS.Open("404.html") defer file.Close() fileInfo, _ := file.Stat()