From 3020ab96b434be04518a4d86c1eb247880f46b1b Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 15 Jan 2021 16:28:45 +0100 Subject: [PATCH] Add BaseURL config parameter --- cmd/fake-sms/template/blocks/base.html.tmpl | 4 ++-- cmd/fake-sms/template/blocks/header.html.tmpl | 2 +- cmd/fake-sms/template/layouts/outbox.html.tmpl | 6 +++--- cmd/fake-sms/template/layouts/sms.html.tmpl | 4 ++-- internal/config/config.go | 1 + internal/route/helper.go | 16 ++++++++++++++++ 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/cmd/fake-sms/template/blocks/base.html.tmpl b/cmd/fake-sms/template/blocks/base.html.tmpl index 343bd13..2f542cb 100644 --- a/cmd/fake-sms/template/blocks/base.html.tmpl +++ b/cmd/fake-sms/template/blocks/base.html.tmpl @@ -6,10 +6,10 @@ {{block "title" . -}}{{- end}} {{- block "head_style" . -}} - + {{end}} {{- block "head_script" . -}} - + {{end}} diff --git a/cmd/fake-sms/template/blocks/header.html.tmpl b/cmd/fake-sms/template/blocks/header.html.tmpl index 7c5f16c..322f7c1 100644 --- a/cmd/fake-sms/template/blocks/header.html.tmpl +++ b/cmd/fake-sms/template/blocks/header.html.tmpl @@ -2,7 +2,7 @@

- + {{if or .Messages .SMS}} 📳 {{else}} diff --git a/cmd/fake-sms/template/layouts/outbox.html.tmpl b/cmd/fake-sms/template/layouts/outbox.html.tmpl index 21fc14b..6193a9d 100644 --- a/cmd/fake-sms/template/layouts/outbox.html.tmpl +++ b/cmd/fake-sms/template/layouts/outbox.html.tmpl @@ -2,7 +2,7 @@ {{define "header_buttons"}} {{end}} diff --git a/internal/config/config.go b/internal/config/config.go index 9f61720..79b4fc7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -20,6 +20,7 @@ type HTTPConfig struct { Address string `yaml:"address" env:"FAKESMS_HTTP_ADDRESS"` TemplateDir string `yaml:"templateDir" env:"FAKESMS_HTTP_TEMPLATEDIR"` PublicDir string `yaml:"publicDir" env:"FAKESMS_HTTP_PUBLICDIR"` + BaseURL string `yaml:"baseUrl" env:"FAKESMS_HTTP_BASEURL"` } type DataConfig struct { diff --git a/internal/route/helper.go b/internal/route/helper.go index 5639808..877272c 100644 --- a/internal/route/helper.go +++ b/internal/route/helper.go @@ -5,9 +5,11 @@ import ( "strconv" "time" + "forge.cadoles.com/Cadoles/fake-sms/internal/config" "forge.cadoles.com/Cadoles/fake-sms/internal/query" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/middleware/container" + "gitlab.com/wpetit/goweb/service" "gitlab.com/wpetit/goweb/service/template" ) @@ -15,6 +17,7 @@ func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Da ctn := container.Must(r.Context()) data, err := template.Extend(data, template.WithBuildInfo(w, r, ctn), + withBaseURL(ctn), ) if err != nil { @@ -24,6 +27,19 @@ func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Da return data } +func withBaseURL(ctn *service.Container) template.DataExtFunc { + return func(data template.Data) (template.Data, error) { + conf, err := config.From(ctn) + if err != nil { + return nil, errors.WithStack(err) + } + + data["BaseURL"] = conf.HTTP.BaseURL + + return data, nil + } +} + func createOutboxQueryFromRequest(r *http.Request) (*query.GetOutboxRequest, error) { orderBy := r.URL.Query().Get("orderBy") reverse := r.URL.Query().Get("reverse")