diff options
| author | Paul Buetow <paul@buetow.org> | 2024-05-11 12:52:28 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-05-11 12:52:28 +0300 |
| commit | 653057ac9d2ba6783233bc0f59a6a7ca111ad11c (patch) | |
| tree | e5be6be1f6df5066885b73bf20d06acb42acc718 /cmd | |
| parent | 248e4919501a472b90505c0ada6a47113acd6688 (diff) | |
refactor server
refactor config
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/gosd/main.go | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/cmd/gosd/main.go b/cmd/gosd/main.go index de5a129..f0ad096 100644 --- a/cmd/gosd/main.go +++ b/cmd/gosd/main.go @@ -6,18 +6,21 @@ import ( "log" "net/http" + "codeberg.org/snonux/gos/internal/config" "codeberg.org/snonux/gos/internal/server/handle" "codeberg.org/snonux/gos/internal/server/health" ) -const apiKey = "banana" // for dev purposes only, will be changed to something else const healthHandlerName = `healthHandler` -var hs = health.NewStatus() +type server struct { + hs health.Status + conf config.Config +} type handlerFuncWithError func(http.ResponseWriter, *http.Request) error -func httpHandle(name string, handler handlerFuncWithError) { +func (s server) httpHandle(name string, handler handlerFuncWithError) { var ( handlerPath = fmt.Sprintf("/%s", name) handlerName = fmt.Sprintf("%sHandler", name) @@ -27,43 +30,52 @@ func httpHandle(name string, handler handlerFuncWithError) { log.Println("Someone requested", handlerName) // The health endpoint doesn't require an API key - if handlerName != healthHandlerName && r.Header.Get("X-API-KEY") != apiKey { + if handlerName != healthHandlerName && r.Header.Get("X-API-KEY") != s.conf.ApiKey { http.Error(w, "Invalid API key", http.StatusUnauthorized) log.Println("Unauthorized access attempt to", handlerName) return } if err := handler(w, r); err != nil { - hs.Set(health.Critical, handlerName, err.Error()) + s.hs.Set(health.Critical, handlerName, err.Error()) return } - hs.Clear(handlerName) + s.hs.Clear(handlerName) }) } func main() { - listenAddr := flag.String("listenAddr", "localhost:8080", "The listen address") - dataDir := flag.String("dataDir", "data", "The data directory") + configFile := flag.String("cfg", "/etc/gos.json", "The configuration file") + + conf, err := config.New(*configFile) + if err != nil { + log.Fatal("error building config:", err) + } + + serv := server{ + conf: conf, + hs: health.NewStatus(), + } - httpHandle("health", func(w http.ResponseWriter, r *http.Request) error { - fmt.Fprint(w, hs.String()) + serv.httpHandle("health", func(w http.ResponseWriter, r *http.Request) error { + fmt.Fprint(w, serv.hs.String()) return nil }) - httpHandle("submit", func(w http.ResponseWriter, r *http.Request) error { - return handle.Submit(w, r, *dataDir) + serv.httpHandle("submit", func(w http.ResponseWriter, r *http.Request) error { + return handle.Submit(w, r, serv.conf.DataDir) }) - httpHandle("list", func(w http.ResponseWriter, r *http.Request) error { - return handle.List(w, r, *dataDir) + serv.httpHandle("list", func(w http.ResponseWriter, r *http.Request) error { + return handle.List(w, r, serv.conf.DataDir) }) - httpHandle("get", func(w http.ResponseWriter, r *http.Request) error { - return handle.Get(w, r, *dataDir) + serv.httpHandle("get", func(w http.ResponseWriter, r *http.Request) error { + return handle.Get(w, r, serv.conf.DataDir) }) - log.Println("Server is starting on", *listenAddr) - if err := http.ListenAndServe(*listenAddr, nil); err != err { - log.Fatal("Error starting server: ", err) + log.Println("Server is starting on", serv.conf.ListenAddr) + if err := http.ListenAndServe(serv.conf.ListenAddr, nil); err != err { + log.Fatal("error starting server:", err) } } |
