-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpServer.go
65 lines (58 loc) · 1.62 KB
/
httpServer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"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() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
writeHtmlFile()
response, err := readFile(outputFilename)
if err != nil {
log.Error().Msgf("cannot read generated file: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("cannot read generated file: %v", err)))
} else {
w.Write(response)
}
})
// write output file at start
writeHtmlFile()
log.Info().Msg("starting server on port 3000")
http.ListenAndServe(":3000", r)
}
func readServerConf() (conf serverConfigT) {
configContent, err := readFile("syncthing-map-server.yaml")
if err != nil {
log.Fatal().Msgf("cannot read server configuration file: %v", err)
}
err = yaml.Unmarshal(configContent, &conf)
if err != nil {
log.Fatal().Msgf("cannot unmarshal server configuration file: %v", err)
}
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)
}