Use esc to embed static assets

This commit is contained in:
nemunaire 2018-12-30 13:21:30 +01:00
parent 1d3ad672cb
commit 7226e9f1e2
3 changed files with 15 additions and 13 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
chldapasswd
config.json
config.json
static.go

View File

@ -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

16
tmpl.go
View File

@ -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{}) {