Skip to content

Commit

Permalink
Added support for serving static content.
Browse files Browse the repository at this point in the history
Via the SetStaticFS option you can serve content from embed.FS.
  • Loading branch information
kiivihal committed Oct 27, 2021
1 parent 4a14ee5 commit 612f438
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ikuzo/ikuzoctl/cmd/serve.go
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions ikuzo/options.go
Expand Up @@ -15,6 +15,7 @@
package ikuzo

import (
"io/fs"
"net/http"
"net/http/httputil"
"net/url"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 612f438

Please sign in to comment.