Skip to content

Commit

Permalink
server HTML file is also written upon server start
Browse files Browse the repository at this point in the history
  • Loading branch information
wsw70 committed Jan 17, 2023
1 parent 10ae9f8 commit de70dac
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions httpServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,20 @@ 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
Folder string
}

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)
Expand All @@ -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)
}
Expand All @@ -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)
}

0 comments on commit de70dac

Please sign in to comment.