From 94b2c702ce7eb95e0bb54f768cde4be77a2d4d14 Mon Sep 17 00:00:00 2001 From: Muhammed Efe Cetin Date: Mon, 17 Mar 2025 00:52:26 +0300 Subject: [PATCH] static: embed static files --- internal/server/server.go | 9 ++++----- static/static.go | 6 ++++++ 2 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 static/static.go diff --git a/internal/server/server.go b/internal/server/server.go index b7bab91..c1d813a 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -4,7 +4,6 @@ import ( "context" "io" "net/http" - "os" "strconv" "strings" "time" @@ -13,6 +12,7 @@ import ( "github.com/armbian/ansi-hastebin/handler" "github.com/armbian/ansi-hastebin/internal/keygenerator" "github.com/armbian/ansi-hastebin/internal/storage" + "github.com/armbian/ansi-hastebin/static" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "github.com/go-chi/httprate" @@ -67,18 +67,17 @@ func (s *Server) RegisterRoutes() { }) // Register static files - static := os.DirFS("static") - fileServer := http.FileServer(http.FS(static)) + fileServer := http.FileServer(http.FS(static.StaticFS)) s.mux.Get("/*", func(w http.ResponseWriter, r *http.Request) { path := strings.TrimPrefix(r.URL.Path, "/") - if _, err := static.Open(path); err == nil { + if _, err := static.StaticFS.Open(path); err == nil { fileServer.ServeHTTP(w, r) return } // If file does not exist, serve index.html - index, err := static.Open("index.html") + index, err := static.StaticFS.Open("index.html") if err != nil { http.Error(w, "Not found", http.StatusNotFound) return diff --git a/static/static.go b/static/static.go new file mode 100644 index 0000000..8e6722c --- /dev/null +++ b/static/static.go @@ -0,0 +1,6 @@ +package static + +import "embed" + +//go:embed *.html *.css *.js *.ico *.png *.txt +var StaticFS embed.FS