diff --git a/httpServer.go b/httpServer.go index 5f7c67c..9354ea9 100644 --- a/httpServer.go +++ b/httpServer.go @@ -11,6 +11,9 @@ import ( "gopkg.in/yaml.v2" ) +const dataFilename = "data-server.json" +const outputFilename = "syncthing-map-server.html" + // configuration file for each device and the folder holding config.xml type serverConfigT []struct { Device string @@ -18,21 +21,10 @@ type serverConfigT []struct { } func httpServer() { - const dataFilename = "data-server.json" - const outputFilename = "syncthing-map-server.html" - r := chi.NewRouter() r.Use(middleware.Logger) r.Get("/", func(w http.ResponseWriter, r *http.Request) { - conf := readServerConf() - // recreate a data file with each server - os.Remove(dataFilename) - for _, entry := range conf { - readConfigXml(entry.Device, filepath.Join(entry.Folder, "config.xml"), dataFilename) - log.Info().Msgf("parsed configuration for %s", entry.Device) - } - // write HTML file to serve - writeGraph(dataFilename, outputFilename) + writeHtmlFile() response, err := readFile(outputFilename) if err != nil { log.Error().Msgf("cannot read generated file: %v", err) @@ -42,6 +34,8 @@ func httpServer() { w.Write(response) } }) + // write output file at start + writeHtmlFile() log.Info().Msg("starting server on port 3000") http.ListenAndServe(":3000", r) } @@ -57,3 +51,15 @@ func readServerConf() (conf serverConfigT) { } return conf } + +func writeHtmlFile() { + conf := readServerConf() + // recreate a data file with each server + os.Remove(dataFilename) + for _, entry := range conf { + readConfigXml(entry.Device, filepath.Join(entry.Folder, "config.xml"), dataFilename) + log.Info().Msgf("parsed configuration for %s", entry.Device) + } + // write HTML file to serve + writeGraph(dataFilename, outputFilename) +}