Skip to content

Commit

Permalink
Add custom logo configuration (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomiceli authored Jan 20, 2024
1 parent 0597a6b commit d394c97
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
6 changes: 6 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
2 changes: 2 additions & 0 deletions docs/configuration/cheat-sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 21 additions & 9 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"html/template"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
Expand Down Expand Up @@ -88,7 +91,8 @@ var (

return defaultAvatar()
},
"asset": asset,
"asset": asset,
"custom": customAsset,
"dev": func() bool {
return dev
},
Expand Down Expand Up @@ -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("")
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion opengist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
18 changes: 15 additions & 3 deletions templates/base/base_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
)

</script>
<link rel="icon" type="image/png" sizes="32x32" href="{{ asset "favicon-32.png" }}">
{{ if $.c.CustomFavicon }}
<link rel="icon" type="image/png" sizes="32x32" href="{{ custom $.c.CustomFavicon }}">
{{ else }}
<link rel="icon" type="image/png" sizes="32x32" href="{{ asset "favicon-32.png" }}">
{{ end }}
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

{{ if dev }}
Expand Down Expand Up @@ -66,13 +70,21 @@
</div>
<div class="flex-shrink-0 items-center hidden sm:flex">
<a href="{{ $.c.ExternalUrl }}/">
<img src="{{ asset "opengist.svg" }}" class="object-cover h-12 w-12">
{{ if $.c.CustomLogo }}
<img src="{{ custom $.c.CustomLogo }}" class="object-cover h-12">
{{ else }}
<img src="{{ asset "opengist.svg" }}" class="object-cover h-12 w-12">
{{ end }}
</a>
</div>
<div class="flex-1 flex items-center justify-center sm:items-stretch sm:justify-start">
<div class="flex-shrink-0 items-center flex sm:hidden">
<a href="{{ $.c.ExternalUrl }}/">
<img src="{{ asset "opengist.svg" }}" class="object-cover h-12 w-12">
{{ if $.c.CustomLogo }}
<img src="{{ custom $.c.CustomLogo }}" class="object-cover h-12">
{{ else }}
<img src="{{ asset "opengist.svg" }}" class="object-cover h-12 w-12">
{{ end }}
</a>
</div>
<div class="hidden sm:block sm:ml-6">
Expand Down

0 comments on commit d394c97

Please sign in to comment.