diff --git a/config.yml b/config.yml index 7f27bf9..77b063d 100644 --- a/config.yml +++ b/config.yml @@ -90,3 +90,9 @@ oidc.client-key: oidc.secret: # Discovery endpoint of the OpenID provider. Generally something like http://auth.example.com/.well-known/openid-configuration oidc.discovery-url: + + +# Custom assets +# Add your own custom assets to $opengist-home/custom/ +custom.logo: +custom.favicon: diff --git a/docs/configuration/cheat-sheet.md b/docs/configuration/cheat-sheet.md index e75d410..e637b1a 100644 --- a/docs/configuration/cheat-sheet.md +++ b/docs/configuration/cheat-sheet.md @@ -30,3 +30,5 @@ | oidc.client-key | OG_OIDC_CLIENT_KEY | none | The client key for the OpenID application. | | oidc.secret | OG_OIDC_SECRET | none | The secret for the OpenID application. | | oidc.discovery-url | OG_OIDC_DISCOVERY_URL | none | Discovery endpoint of the OpenID provider. | +| custom.logo | OG_CUSTOM_LOGO | none | Path to an image, relative to $opengist-home/custom | +| custom.favicon | OG_CUSTOM_FAVICON | none | Path to an image, relative to $opengist-home/custom | diff --git a/internal/config/config.go b/internal/config/config.go index 8f78085..e15e775 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -60,6 +60,9 @@ type config struct { OIDCClientKey string `yaml:"oidc.client-key" env:"OG_OIDC_CLIENT_KEY"` OIDCSecret string `yaml:"oidc.secret" env:"OG_OIDC_SECRET"` OIDCDiscoveryUrl string `yaml:"oidc.discovery-url" env:"OG_OIDC_DISCOVERY_URL"` + + CustomLogo string `yaml:"custom.logo" env:"OG_CUSTOM_LOGO"` + CustomFavicon string `yaml:"custom.favicon" env:"OG_CUSTOM_FAVICON"` } func configWithDefaults() (*config, error) { diff --git a/internal/web/server.go b/internal/web/server.go index 994e6c2..2aaea44 100644 --- a/internal/web/server.go +++ b/internal/web/server.go @@ -10,6 +10,9 @@ import ( "html/template" "io" "net/http" + "net/url" + "os" + "path" "path/filepath" "regexp" "strconv" @@ -88,7 +91,8 @@ var ( return defaultAvatar() }, - "asset": asset, + "asset": asset, + "custom": customAsset, "dev": func() bool { return dev }, @@ -205,8 +209,15 @@ func NewServer(isDev bool) *Server { if !dev { parseManifestEntries() - e.GET("/assets/*", cacheControl(echo.WrapHandler(http.FileServer(http.FS(public.Files))))) } + customFs := os.DirFS(filepath.Join(config.GetHomeDir(), "custom")) + e.GET("/assets/*", func(c echo.Context) error { + if _, err := public.Files.Open(path.Join("assets", c.Param("*"))); !dev && err == nil { + return echo.WrapHandler(http.FileServer(http.FS(public.Files)))(c) + } + + return echo.WrapHandler(http.StripPrefix("/assets/", http.FileServer(http.FS(customFs))))(c) + }) // Web based routes g1 := e.Group("") @@ -466,13 +477,6 @@ func checkRequireLogin(next echo.HandlerFunc) echo.HandlerFunc { } } -func cacheControl(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - c.Response().Header().Set(echo.HeaderCacheControl, "public, max-age=31536000") - return next(c) - } -} - func noRouteFound(echo.Context) error { return notFound("Page not found") } @@ -512,3 +516,11 @@ func asset(file string) string { } return config.C.ExternalUrl + "/" + manifestEntries[file].File } + +func customAsset(file string) string { + assetpath, err := url.JoinPath("/", "assets", file) + if err != nil { + log.Error().Err(err).Msgf("Failed to join path for custom file %s", file) + } + return config.C.ExternalUrl + assetpath +} diff --git a/opengist.go b/opengist.go index ea36c30..956e2c7 100644 --- a/opengist.go +++ b/opengist.go @@ -51,7 +51,9 @@ func initialize() { if err := os.MkdirAll(filepath.Join(homePath, "tmp", "repos"), 0755); err != nil { log.Fatal().Err(err).Send() } - + if err := os.MkdirAll(filepath.Join(homePath, "custom"), 0755); err != nil { + log.Fatal().Err(err).Send() + } log.Info().Msg("Database file: " + filepath.Join(homePath, config.C.DBFilename)) if err := db.Setup(filepath.Join(homePath, config.C.DBFilename), false); err != nil { log.Fatal().Err(err).Msg("Failed to initialize database") diff --git a/templates/base/base_header.html b/templates/base/base_header.html index bb7883a..90b915f 100644 --- a/templates/base/base_header.html +++ b/templates/base/base_header.html @@ -28,7 +28,11 @@ ) - + {{ if $.c.CustomFavicon }} + + {{ else }} + + {{ end }} {{ if dev }} @@ -66,13 +70,21 @@
- + {{ if $.c.CustomLogo }} + + {{ else }} + + {{ end }}