From 7226e9f1e205d5be2e43e0aac0544dd733a64b6d Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 30 Dec 2018 13:21:30 +0100 Subject: [PATCH] Use esc to embed static assets --- .gitignore | 3 ++- main.go | 9 +-------- tmpl.go | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 8d2b8b5..bc33b94 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ chldapasswd -config.json \ No newline at end of file +config.json +static.go \ No newline at end of file diff --git a/main.go b/main.go index 97e81b0..1a183f8 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,6 @@ package main +//go:generate esc -o static.go static import ( "context" "encoding/json" @@ -12,13 +13,10 @@ import ( "os" "os/signal" "path" - "path/filepath" "strings" "syscall" ) -var StaticDir string = "./static/" - var myLDAP = LDAP{ Host: "localhost", Port: 389, @@ -70,15 +68,10 @@ func main() { var bind = flag.String("bind", "127.0.0.1:8080", "Bind port/socket") var baseURL = flag.String("baseurl", "/", "URL prepended to each URL") var configfile = flag.String("config", "config.json", "path to the configuration file") - flag.StringVar(&StaticDir, "static", StaticDir, "Directory containing static files") flag.Parse() // Sanitize options - var err error log.Println("Checking paths...") - if StaticDir, err = filepath.Abs(StaticDir); err != nil { - log.Fatal(err) - } if *baseURL != "/" { tmp := path.Clean(*baseURL) baseURL = &tmp diff --git a/tmpl.go b/tmpl.go index ba90fa9..b984a70 100644 --- a/tmpl.go +++ b/tmpl.go @@ -7,11 +7,19 @@ import ( ) func displayTmpl(w http.ResponseWriter, page string, vars map[string]interface{}) { - if t, err := template.ParseGlob(path.Join(StaticDir, "*.html")); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } else { - t.ExecuteTemplate(w, page, vars) + t := template.New("index") + for _, fname := range []string{ + "/static/change.html", + "/static/footer.html", + "/static/header.html", + "/static/login.html", + "/static/lost.html", + "/static/message.html", + "/static/reset.html", + } { + t.New(path.Base(fname)).Parse(FSMustString(false, fname)) } + t.ExecuteTemplate(w, page, vars) } func displayTmplError(w http.ResponseWriter, statusCode int, page string, vars map[string]interface{}) {