From d6cf67b3d7747b6274d92e394d75d348060fa5f5 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sun, 25 Jun 2023 16:03:36 +0200 Subject: feat: Add static file to output bin Now the final binary has a standalone web server including necessary static file. --- README.md | 2 +- cmd/server/main.go | 4 ++-- pkg/ext/fileserver.go | 18 ++++++++++++++++++ pkg/worker/list_processor_test.go | 3 ++- static.go | 34 ++++++++++++++++++++++++++++++++++ tmpl.go | 29 ----------------------------- 6 files changed, 57 insertions(+), 33 deletions(-) create mode 100644 pkg/ext/fileserver.go create mode 100644 static.go delete mode 100644 tmpl.go diff --git a/README.md b/README.md index 0940ae4..6103dbf 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A read only file explorer with media capabilities. * Thumbnail system * Initial setup process * Also allow setup user config file -* Single binary output +* ~~Single binary output~~ * Better worker pool. Allow cron job and ui config * Alpine package and demo site * Single image viewer and show exif info (not sure how yet) diff --git a/cmd/server/main.go b/cmd/server/main.go index 0fa5fea..4ca39de 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -16,6 +16,7 @@ import ( "gorm.io/driver/sqlite" "gorm.io/gorm" + "git.sr.ht/~gabrielgio/img" "git.sr.ht/~gabrielgio/img/pkg/components/auth" "git.sr.ht/~gabrielgio/img/pkg/components/filesystem" "git.sr.ht/~gabrielgio/img/pkg/components/media" @@ -69,8 +70,7 @@ func main() { } r := router.New() - r.ServeFiles("/static/{filepath:*}", "./static") - r.NotFound = ext.NotFoundHTML + r.GET("/static/{filepath:*}", ext.FileServer(img.StaticFS, "static/")) authMiddleware := ext.NewAuthMiddleware(hexKey, logger.WithField("context", "auth")) logMiddleware := ext.NewLogMiddleare(logger.WithField("context", "http")) diff --git a/pkg/ext/fileserver.go b/pkg/ext/fileserver.go new file mode 100644 index 0000000..fdea08e --- /dev/null +++ b/pkg/ext/fileserver.go @@ -0,0 +1,18 @@ +package ext + +import ( + "io/fs" + + "github.com/valyala/fasthttp" +) + +type FileSystem interface { + Open(name string) (fs.File, error) +} + +func FileServer(rootFS FileSystem, rootPath string) fasthttp.RequestHandler { + return func(r *fasthttp.RequestCtx) { + path := r.UserValue("filepath").(string) + r.SendFile(rootPath + path) + } +} diff --git a/pkg/worker/list_processor_test.go b/pkg/worker/list_processor_test.go index 35672f3..ce3ff0a 100644 --- a/pkg/worker/list_processor_test.go +++ b/pkg/worker/list_processor_test.go @@ -9,8 +9,9 @@ import ( "sync" "testing" - "git.sr.ht/~gabrielgio/img/pkg/testkit" "github.com/sirupsen/logrus" + + "git.sr.ht/~gabrielgio/img/pkg/testkit" ) type ( diff --git a/static.go b/static.go new file mode 100644 index 0000000..1c6a086 --- /dev/null +++ b/static.go @@ -0,0 +1,34 @@ +package img + +import ( + "embed" + "fmt" + "html/template" + "io" +) + +var ( + //go:embed templates/*.html + TemplateFS embed.FS + + //go:embed static/* + StaticFS embed.FS + + Template *template.Template +) + +type HTMLView[T any] struct { + Title string + Username string + Data T +} + +func Render[T any](w io.Writer, page string, view *HTMLView[T]) error { + pageFile := fmt.Sprintf("templates/%s", page) + tmpl, err := template.New("").ParseFS(TemplateFS, "templates/layout.html", pageFile) + if err != nil { + return err + } + + return tmpl.ExecuteTemplate(w, page, view) +} diff --git a/tmpl.go b/tmpl.go deleted file mode 100644 index b11f962..0000000 --- a/tmpl.go +++ /dev/null @@ -1,29 +0,0 @@ -package img - -import ( - "embed" - "fmt" - "html/template" - "io" -) - -//go:embed templates/*.html -var TemplateFS embed.FS - -var Template *template.Template - -type HTMLView[T any] struct { - Title string - Username string - Data T -} - -func Render[T any](w io.Writer, page string, view *HTMLView[T]) error { - pageFile := fmt.Sprintf("templates/%s", page) - tmpl, err := template.New("").ParseFS(TemplateFS, "templates/layout.html", pageFile) - if err != nil { - return err - } - - return tmpl.ExecuteTemplate(w, page, view) -} -- cgit v1.2.3