diff --git a/main.go b/main.go index 4509470..5e1e6a2 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "net/http" + "path/filepath" "sync" "text/template" ) @@ -22,10 +23,11 @@ var m = sync.Mutex{} var addr = flag.String("addr", ":8000", "http service address") var static_files = flag.String("static", "./static", "location of static files") var db_file = flag.String("db", "db.json", "output database") -var homeTempl = template.Must(template.ParseFiles("index.html")) +var template_dir = flag.String("templates", "templates", "template dir") +var templates *template.Template func homeHandler(c http.ResponseWriter, req *http.Request) { - homeTempl.Execute(c, req.Host) + templates.Execute(c, req.Host) } func passHandler(resp http.ResponseWriter, req *http.Request) { @@ -69,6 +71,14 @@ func init_db() { func main() { flag.Parse() init_db() + pattern := filepath.Join(*template_dir, "*.html") + var err error + templates, err = template.ParseGlob(pattern) + if err != nil { + println(*template_dir) + println(pattern) + log.Fatal("problem parsing template dir:", *template_dir) + } http.HandleFunc("/", homeHandler) http.HandleFunc("/pass", passHandler) http.HandleFunc("/api/1.0/creds/", credHandler) diff --git a/index.html b/templates/index.html similarity index 100% rename from index.html rename to templates/index.html