From 612f4389252c8970f0a88b963442925bf7bdaf6e Mon Sep 17 00:00:00 2001 From: Sjoerd Siebinga Date: Mon, 23 Aug 2021 22:49:22 +0200 Subject: [PATCH] Added support for serving static content. Via the SetStaticFS option you can serve content from embed.FS. --- ikuzo/ikuzoctl/cmd/serve.go | 6 ++++++ ikuzo/{ => ikuzoctl/cmd}/static/favicon.ico | Bin ikuzo/options.go | 17 +++++++++++++++++ 3 files changed, 23 insertions(+) rename ikuzo/{ => ikuzoctl/cmd}/static/favicon.ico (100%) diff --git a/ikuzo/ikuzoctl/cmd/serve.go b/ikuzo/ikuzoctl/cmd/serve.go index aaddff17..62852e75 100644 --- a/ikuzo/ikuzoctl/cmd/serve.go +++ b/ikuzo/ikuzoctl/cmd/serve.go @@ -15,12 +15,17 @@ package cmd import ( + "embed" + "github.com/delving/hub3/hub3/server/http/handlers" "github.com/delving/hub3/ikuzo" "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) +//go:embed static +var staticFS embed.FS + // serveCmd represents the serve command var serveCmd = &cobra.Command{ Use: "serve", @@ -59,6 +64,7 @@ func serve() { handlers.RegisterEAD, ), ikuzo.SetEnableLegacyConfig(cfgFile), + ikuzo.SetStaticFS(staticFS), ) // load dataNodeProxy last so that other urls are overwritten in the router diff --git a/ikuzo/static/favicon.ico b/ikuzo/ikuzoctl/cmd/static/favicon.ico similarity index 100% rename from ikuzo/static/favicon.ico rename to ikuzo/ikuzoctl/cmd/static/favicon.ico diff --git a/ikuzo/options.go b/ikuzo/options.go index 0b457d44..ed3fcbd5 100644 --- a/ikuzo/options.go +++ b/ikuzo/options.go @@ -15,6 +15,7 @@ package ikuzo import ( + "io/fs" "net/http" "net/http/httputil" "net/url" @@ -29,6 +30,7 @@ import ( "github.com/delving/hub3/ikuzo/service/x/oaipmh" "github.com/delving/hub3/ikuzo/service/x/revision" "github.com/delving/hub3/ikuzo/storage/x/elasticsearch" + "github.com/delving/hub3/ikuzo/webapp" "github.com/go-chi/chi" "github.com/pacedotdev/oto/otohttp" ) @@ -205,6 +207,21 @@ func SetLegacyRouters(routers ...RouterFunc) Option { } } +func SetStaticFS(static fs.FS) Option { + return func(s *server) error { + s.routerFuncs = append(s.routerFuncs, + func(r chi.Router) { + r.Get("/static/*", webapp.NewStaticHandler(static)) + r.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/static/favicon.ico", http.StatusMovedPermanently) + }) + }, + ) + + return nil + } +} + func SetEADService(svc *ead.Service) Option { return func(s *server) error { s.routerFuncs = append(s.routerFuncs,