From 9bd048b6468e1a28bc281c323fcdd07e517a811d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20B=C3=B6hm?= Date: Sun, 21 Feb 2021 15:26:23 +0100 Subject: [PATCH] support loading custom frontend templates from file --- .gitignore | 1 + frontend/frontend.go | 16 ++++++++++++---- shared/config.go | 8 +++++++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index d055efc..185ba3f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ /docker/docker-compose.*.yml /ddns dump.rdb +index.html /docker/.caddy_mount/ /docker/.redis_mount/ diff --git a/frontend/frontend.go b/frontend/frontend.go index 6c4de09..974883b 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -41,7 +41,7 @@ func (f *Frontend) Run() error { r.Use(gin.Logger()) } - r.SetHTMLTemplate(buildTemplate()) + r.SetHTMLTemplate(buildTemplate(f.config.CustomTemplatePath)) r.GET("/", func(g *gin.Context) { 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 -func buildTemplate() *template.Template { - html, err := template.New("index.html").Parse(indexTemplate) +func buildTemplate(customTemplatePath string) *template.Template { + 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 { - log.Fatal(err) + log.Fatalf("Error parsing frontend template: %v", err) } return html diff --git a/shared/config.go b/shared/config.go index 97269c0..da33698 100644 --- a/shared/config.go +++ b/shared/config.go @@ -15,6 +15,7 @@ type Config struct { ListenFrontend string ListenBackend string RedisHost string + CustomTemplatePath string fs *flag.FlagSet } @@ -33,6 +34,9 @@ func (c *Config) Initialize() { fs.StringVar(&c.ListenFrontend, "listen-frontend", ":8080", "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", "The Redis socket that should be used") @@ -46,7 +50,9 @@ func (c *Config) Initialize() { } 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 == "" { log.Fatal("You have to supply the domain via env variable DDNS_DOMAIN or command line flag --domain=DOMAIN")