diff --git a/internal/httpServer/server.go b/internal/httpServer/server.go index 3d3ea96..b2910d0 100644 --- a/internal/httpServer/server.go +++ b/internal/httpServer/server.go @@ -1,12 +1,12 @@ package httpServer import ( + "embed" "log" "net/http" "git.gnous.eu/gnouseu/plakken/internal/constant" "git.gnous.eu/gnouseu/plakken/internal/web/plak" - "git.gnous.eu/gnouseu/plakken/internal/web/static" "github.com/redis/go-redis/v9" ) @@ -14,6 +14,19 @@ type ServerConfig struct { HTTPServer *http.Server UrlLength uint8 DB *redis.Client + Static embed.FS + Templates embed.FS +} + +func (config ServerConfig) home(w http.ResponseWriter, r *http.Request) { + index, err := config.Static.ReadFile("static/index.html") + if err != nil { + log.Println(err) + } + _, err = w.Write(index) + if err != nil { + log.Println(err) + } } // Configure HTTP router @@ -21,11 +34,13 @@ func (config ServerConfig) router() { WebConfig := plak.WebConfig{ DB: config.DB, UrlLength: config.UrlLength, + Templates: config.Templates, } + staticFiles := http.FS(config.Static) - http.HandleFunc("GET /{$}", static.Home) + http.HandleFunc("GET /{$}", config.home) + http.Handle("GET /static/{file}", http.FileServer(staticFiles)) http.HandleFunc("GET /{key}/{settings...}", WebConfig.View) - http.HandleFunc("GET /static/{file}", static.ServeStatic) http.HandleFunc("POST /{$}", WebConfig.Create) http.HandleFunc("DELETE /{key}", WebConfig.Delete) } diff --git a/internal/web/plak/plak.go b/internal/web/plak/plak.go index d854a75..51b8af6 100644 --- a/internal/web/plak/plak.go +++ b/internal/web/plak/plak.go @@ -2,6 +2,7 @@ package plak import ( "context" + "embed" "io" "log" "net/http" @@ -19,6 +20,7 @@ var ctx = context.Background() type WebConfig struct { DB *redis.Client UrlLength uint8 + Templates embed.FS } // Plak "Object" for plak @@ -76,8 +78,9 @@ func (config WebConfig) View(w http.ResponseWriter, r *http.Request) { log.Println(err) } } else { - t, err := template.ParseFiles("templates/paste.html") + t, err := template.ParseFS(config.Templates, "templates/paste.html") if err != nil { + w.WriteHeader(http.StatusInternalServerError) log.Println(err) } err = t.Execute(w, plak) diff --git a/internal/web/static/static.go b/internal/web/static/static.go deleted file mode 100644 index 4c4a059..0000000 --- a/internal/web/static/static.go +++ /dev/null @@ -1,15 +0,0 @@ -package static - -import ( - "net/http" -) - -// ServeStatic Serve static file from static -func ServeStatic(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "./static/"+r.PathValue("file")) // TODO: vérifier si c'est safe -} - -// Home Serve index.html -func Home(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "./static/index.html") -} diff --git a/main.go b/main.go index 6ced2da..df25d23 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,20 @@ package main import ( + "embed" + "git.gnous.eu/gnouseu/plakken/internal/config" "git.gnous.eu/gnouseu/plakken/internal/database" "git.gnous.eu/gnouseu/plakken/internal/httpServer" ) +var ( + //go:embed templates + templates embed.FS + //go:embed static + static embed.FS +) + func main() { initConfig := config.GetConfig() dbConfig := database.InitDB(initConfig.RedisAddress, initConfig.RedisUser, initConfig.RedisPassword, initConfig.RedisDB) @@ -15,6 +24,8 @@ func main() { HTTPServer: httpServer.Config(initConfig.ListenAddress), UrlLength: initConfig.UrlLength, DB: db, + Static: static, + Templates: templates, } serverConfig.Server()