diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 24 |
1 files changed, 12 insertions, 12 deletions
@@ -10,15 +10,12 @@ import ( "log" "net/http" "net/netip" - "path/filepath" "text/template" ) -//go:embed simple.css/simple.css +//go:embed simple.css/simple.css templates/index.html var static embed.FS -var appdata string - type RuntimeConfig struct { Devices DevicesConfig Web WebConfig @@ -35,7 +32,7 @@ type WebConfig struct { func main() { configPath := parseFlags() c := parseConfig(configPath) - http.HandleFunc("/", index(c.Devices, appdata)) + http.HandleFunc("/", index(c.Devices)) http.HandleFunc("/api", api()) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(static)))) @@ -50,8 +47,6 @@ func parseFlags() string { flag.StringVar(&r, "c", "/etc/webiot/config.json", "path to configuration file") - flag.StringVar(&appdata, "a", "/usr/share/webiot", - "path to static application data") flag.Parse() return r @@ -76,13 +71,18 @@ func parseConfig(path string) RuntimeConfig { } // index() returns a HTTP handler for the index page. -func index(devices DevicesConfig, appdata string) func(http.ResponseWriter, *http.Request) { - - path := filepath.Join(appdata, "index.html.tmpl") - html := mustRender(path, devices) +func index(devices DevicesConfig) func(http.ResponseWriter, *http.Request) { + tmpl, err := template.ParseFS(static, "templates/index.html") + if err != nil { + log.Fatal(err) + } return func(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, html) + err = tmpl.Execute(w, devices) + if err != nil { + http.Error(w, fmt.Sprint(err), http.StatusInternalServerError) + return + } } } |