diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-25 16:03:36 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-25 16:12:05 +0200 |
commit | d6cf67b3d7747b6274d92e394d75d348060fa5f5 (patch) | |
tree | 8c48f947e7ab732a38b15eab6a898cb14caa1669 | |
parent | 57b41ad766b3c4505672c12f058f10c7a132dd5b (diff) | |
download | lens-d6cf67b3d7747b6274d92e394d75d348060fa5f5.tar.gz lens-d6cf67b3d7747b6274d92e394d75d348060fa5f5.tar.bz2 lens-d6cf67b3d7747b6274d92e394d75d348060fa5f5.zip |
feat: Add static file to output bin
Now the final binary has a standalone web server including necessary
static file.
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | cmd/server/main.go | 4 | ||||
-rw-r--r-- | pkg/ext/fileserver.go | 18 | ||||
-rw-r--r-- | pkg/worker/list_processor_test.go | 3 | ||||
-rw-r--r-- | static.go (renamed from tmpl.go) | 11 |
5 files changed, 31 insertions, 7 deletions
@@ -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 ( @@ -7,10 +7,15 @@ import ( "io" ) -//go:embed templates/*.html -var TemplateFS embed.FS +var ( + //go:embed templates/*.html + TemplateFS embed.FS -var Template *template.Template + //go:embed static/* + StaticFS embed.FS + + Template *template.Template +) type HTMLView[T any] struct { Title string |