support loading custom frontend templates from file

This commit is contained in:
Philipp Böhm 2021-02-21 15:26:23 +01:00
parent e62dbbda94
commit 9bd048b646
3 changed files with 20 additions and 5 deletions

1
.gitignore vendored
View File

@ -2,5 +2,6 @@
/docker/docker-compose.*.yml /docker/docker-compose.*.yml
/ddns /ddns
dump.rdb dump.rdb
index.html
/docker/.caddy_mount/ /docker/.caddy_mount/
/docker/.redis_mount/ /docker/.redis_mount/

View File

@ -41,7 +41,7 @@ func (f *Frontend) Run() error {
r.Use(gin.Logger()) r.Use(gin.Logger())
} }
r.SetHTMLTemplate(buildTemplate()) r.SetHTMLTemplate(buildTemplate(f.config.CustomTemplatePath))
r.GET("/", func(g *gin.Context) { r.GET("/", func(g *gin.Context) {
g.HTML(200, "index.html", gin.H{"domain": f.config.Domain}) g.HTML(200, "index.html", gin.H{"domain": f.config.Domain})
@ -153,10 +153,18 @@ func extractRemoteAddr(req *http.Request) (string, error) {
} }
// Get index template from bindata // Get index template from bindata
func buildTemplate() *template.Template { func buildTemplate(customTemplatePath string) *template.Template {
html, err := template.New("index.html").Parse(indexTemplate) var html *template.Template
var err error
if customTemplatePath != "" {
html, err = template.ParseFiles(customTemplatePath)
} else {
html, err = template.New("index.html").Parse(indexTemplate)
}
if err != nil { if err != nil {
log.Fatal(err) log.Fatalf("Error parsing frontend template: %v", err)
} }
return html return html

View File

@ -15,6 +15,7 @@ type Config struct {
ListenFrontend string ListenFrontend string
ListenBackend string ListenBackend string
RedisHost string RedisHost string
CustomTemplatePath string
fs *flag.FlagSet fs *flag.FlagSet
} }
@ -33,6 +34,9 @@ func (c *Config) Initialize() {
fs.StringVar(&c.ListenFrontend, "listen-frontend", ":8080", fs.StringVar(&c.ListenFrontend, "listen-frontend", ":8080",
"Which socket should the frontend web service use to bind itself") "Which socket should the frontend web service use to bind itself")
fs.StringVar(&c.CustomTemplatePath, "custom-template-path", "",
"A path to a custom `index.html` file that will be used instead of the default frontend template")
fs.StringVar(&c.RedisHost, "redis-host", ":6379", fs.StringVar(&c.RedisHost, "redis-host", ":6379",
"The Redis socket that should be used") "The Redis socket that should be used")
@ -46,7 +50,9 @@ func (c *Config) Initialize() {
} }
func (c *Config) Validate() { func (c *Config) Validate() {
c.fs.Parse(os.Args[1:]) if err := c.fs.Parse(os.Args[1:]); err != nil {
log.Fatalf("Error parsing configuration: %v", err)
}
if c.Domain == "" { if c.Domain == "" {
log.Fatal("You have to supply the domain via env variable DDNS_DOMAIN or command line flag --domain=DOMAIN") log.Fatal("You have to supply the domain via env variable DDNS_DOMAIN or command line flag --domain=DOMAIN")