Allow specify base for asset url

This commit is contained in:
Sung Won Cho 2022-04-18 17:08:08 +10:00
commit 8c13525177
6 changed files with 28 additions and 17 deletions

View file

@ -105,6 +105,7 @@ type Config struct {
DB PostgresConfig
PageTemplateDir string
StaticDir string
AssetBaseURL string
}
func getAppEnv() string {
@ -131,6 +132,7 @@ func Load() Config {
OnPremise: readBoolEnv("OnPremise"),
DisableRegistration: readBoolEnv("DisableRegistration"),
DB: loadDBConfig(),
AssetBaseURL: "",
}
if err := validate(c); err != nil {
@ -155,6 +157,11 @@ func (c *Config) SetStaticDir(d string) {
c.StaticDir = d
}
// SetAssetBaseURL sets static dir for the confi
func (c *Config) SetAssetBaseURL(d string) {
c.AssetBaseURL = d
}
// IsProd checks if the app environment is configured to be production.
func (c Config) IsProd() bool {
return c.AppEnv == AppEnvProduction

View file

@ -6,12 +6,13 @@ import (
"strings"
"time"
"github.com/dnote/dnote/pkg/server/app"
"github.com/dnote/dnote/pkg/server/buildinfo"
"github.com/pkg/errors"
"html/template"
)
func initHelpers(c Config) template.FuncMap {
func initHelpers(c Config, a *app.App) template.FuncMap {
ctx := newViewCtx(c)
ret := template.FuncMap{
@ -30,6 +31,9 @@ func initHelpers(c Config) template.FuncMap {
"dict": ctx.dict,
"defaultValue": ctx.defaultValue,
"add": ctx.add,
"assetBaseURL": func() string {
return a.Config.AssetBaseURL
},
}
// extend with helpers that are defined specific to a view

View file

@ -6,20 +6,20 @@
<meta name="viewport" content="width=device-width,minimum-scale=1" />
<title>{{ title }}</title>
<link rel="icon" type="image/x-icon" href="/static/favicon.ico" />
<link rel="apple-touch-icon" sizes="57x57" href="/static/apple-icon-57x57.png" />
<link rel="apple-touch-icon" sizes="60x60" href="/static/apple-icon-60x60.png" />
<link rel="apple-touch-icon" sizes="72x72" href="/static/apple-icon-72x72.png" />
<link rel="apple-touch-icon" sizes="76x76" href="/static/apple-icon-76x76.png" />
<link rel="apple-touch-icon" sizes="114x114" href="/static/apple-icon-114x114.png" />
<link rel="apple-touch-icon" sizes="120x120" href="/static/apple-icon-120x120.png" />
<link rel="apple-touch-icon" sizes="144x144" href="/static/apple-icon-144x144.png" />
<link rel="apple-touch-icon" sizes="152x152" href="/static/apple-icon-152x152.png" />
<link rel="apple-touch-icon" sizes="180x180" href="/static/apple-icon-180x180.png" />
<link rel="icon" type="image/png" sizes="192x192" href="/static/android-icon-192x192.png" />
<link rel="manifest" href="/static/manifest.json" />
<link rel="icon" type="image/x-icon" href="{{ assetBaseURL }}/static/favicon.ico" />
<link rel="apple-touch-icon" sizes="57x57" href="{{ assetBaseURL }}/static/apple-icon-57x57.png" />
<link rel="apple-touch-icon" sizes="60x60" href="{{ assetBaseURL }}/static/apple-icon-60x60.png" />
<link rel="apple-touch-icon" sizes="72x72" href="{{ assetBaseURL }}/static/apple-icon-72x72.png" />
<link rel="apple-touch-icon" sizes="76x76" href="{{ assetBaseURL }}/static/apple-icon-76x76.png" />
<link rel="apple-touch-icon" sizes="114x114" href="{{ assetBaseURL }}/static/apple-icon-114x114.png" />
<link rel="apple-touch-icon" sizes="120x120" href="{{ assetBaseURL }}/static/apple-icon-120x120.png" />
<link rel="apple-touch-icon" sizes="144x144" href="{{ assetBaseURL }}/static/apple-icon-144x144.png" />
<link rel="apple-touch-icon" sizes="152x152" href="{{ assetBaseURL }}/static/apple-icon-152x152.png" />
<link rel="apple-touch-icon" sizes="180x180" href="{{ assetBaseURL }}/static/apple-icon-180x180.png" />
<link rel="icon" type="image/png" sizes="192x192" href="{{ assetBaseURL }}/static/android-icon-192x192.png" />
<link rel="manifest" href="{{ assetBaseURL }}/static/manifest.json" />
<meta name="msapplication-TileColor" content="#ffffff" />
<meta name="msapplication-TileImage" content="/static/ms-icon-144x144.png" />
<meta name="msapplication-TileImage" content="{{ assetBaseURL }}/static/ms-icon-144x144.png" />
<meta name="theme-color" content="#ffffff" />
{{template "css" .}}

View file

@ -1,5 +1,5 @@
{{define "css"}}
{{range css}}
<link rel="stylesheet" href="/static/{{ . }}">
<link rel="stylesheet" href="{{ assetBaseURL }}/static/{{ . }}">
{{end}}
{{end}}

View file

@ -1,5 +1,5 @@
{{define "js"}}
{{range js}}
<script src="/static/{{ . }}"></script>
<script src="{{ assetBaseURL }}/static/{{ . }}"></script>
{{end}}
{{end}}

View file

@ -77,7 +77,7 @@ func NewView(baseDir string, app *app.App, viewConfig Config, files ...string) *
files = append(files, layoutFiles(baseDir)...)
files = append(files, partialFiles(baseDir)...)
viewHelpers := initHelpers(viewConfig)
viewHelpers := initHelpers(viewConfig, app)
t := template.New(viewConfig.Title).Funcs(viewHelpers)
t, err := t.ParseFiles(files...)