From 381a0dcc98ff94e15fe0b1c4d3cdfa1a9b22ff6f Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Tue, 8 Feb 2022 17:46:44 +0400 Subject: [PATCH] Jade can work with embed FS, so we do that. --- internal/handlers/handlers.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index bd8bc84..f0341b0 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -6,11 +6,13 @@ import ( "dwelling-upload/pkg/logging" "dwelling-upload/pkg/server" "dwelling-upload/pkg/utils" + "embed" "encoding/base64" "encoding/hex" "fmt" "html/template" "io" + "io/fs" "net/http" "os" "path" @@ -19,10 +21,14 @@ import ( "github.com/Joker/jade" ) -//var defaultAmberOptions = amber.Options{PrettyPrint: false, LineNumbers: false} - var compiledTemplates map[string]*template.Template +//go:embed web/assets +var assetsDir embed.FS + +//go:embed web/templates +var templatesDir embed.FS + type NotFoundData struct { MainSite string } @@ -64,6 +70,11 @@ func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logg uploadDirSize: uploadDirSize} } +func (h *UploadHandlers) AssetsFS() http.FileSystem { + f, _ := fs.Sub(assetsDir, "web/assets") + return http.FS(f) +} + func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) { var storCapacity int64 = h.conf.Uploads.Limits.Storage << 20 var fMaxSize int64 = h.conf.Uploads.Limits.FileSize << 20 @@ -216,7 +227,10 @@ func (h *UploadHandlers) NotFound(w http.ResponseWriter, r *http.Request) { func compileTemplates(dir string, lErr *logging.Logger) { compiledTemplates = make(map[string]*template.Template) - indexStr, err := jade.ParseFile(path.Join(dir, "index.jade")) + t, _ := fs.Sub(templatesDir, "web/templates") + templatesFS := http.FS(t) + + indexStr, err := jade.ParseFileFromFileSystem("index.jade", templatesFS) if err != nil { lErr.Fatalln(err) } @@ -228,7 +242,7 @@ func compileTemplates(dir string, lErr *logging.Logger) { compiledTemplates["index"] = indexTpl - uploadedStr, err := jade.ParseFile(path.Join(dir, "uploaded.jade")) + uploadedStr, err := jade.ParseFileFromFileSystem("uploaded.jade", templatesFS) if err != nil { lErr.Fatalln(err) } @@ -240,7 +254,7 @@ func compileTemplates(dir string, lErr *logging.Logger) { compiledTemplates["uploaded"] = uploadedTpl - notfoundStr, err := jade.ParseFile(path.Join(dir, "404.jade")) + notfoundStr, err := jade.ParseFileFromFileSystem("404.jade", templatesFS) if err != nil { lErr.Fatalln(err) }