diff options
| author | Paul Buetow <paul@buetow.org> | 2024-09-21 14:03:45 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-09-21 14:03:45 +0300 |
| commit | dff4d455e07d639b82a0bed814f41d0656e9b6d0 (patch) | |
| tree | 56289a6fd80a00a9724992ab9cb8b3d7215ebedf /internal/server/server.go | |
| parent | 780ade3dc066afb8a43be824373414f3d316ebd6 (diff) | |
cleanup
Diffstat (limited to 'internal/server/server.go')
| -rw-r--r-- | internal/server/server.go | 49 |
1 files changed, 0 insertions, 49 deletions
diff --git a/internal/server/server.go b/internal/server/server.go deleted file mode 100644 index b2cb0d0..0000000 --- a/internal/server/server.go +++ /dev/null @@ -1,49 +0,0 @@ -package server - -import ( - "fmt" - "log" - "net/http" - - config "codeberg.org/snonux/gos/internal/config/server" - "codeberg.org/snonux/gos/internal/server/health" -) - -type Server struct { - Status health.Status - Conf config.ServerConfig -} - -type HandlerFuncWithError func(http.ResponseWriter, *http.Request) error - -func New(conf config.ServerConfig, status health.Status) Server { - return Server{Conf: conf, Status: status} -} - -func (serv Server) Handle(name string, handler HandlerFuncWithError) { - var ( - handlerPath = fmt.Sprintf("/%s", name) - handlerName = fmt.Sprintf("%sHandler", name) - ) - - http.HandleFunc(handlerPath, func(w http.ResponseWriter, r *http.Request) { - log.Println("Someone requested", handlerName) - - // The health endpoint doesn't require an API key - if handlerName != "healthHandler" { - accessHealthStatusKey := "server.Handler.Access" - if r.Header.Get("X-API-KEY") != serv.Conf.APIKey { - http.Error(w, "Invalid API key", http.StatusUnauthorized) - serv.Status.Set(health.Critical, accessHealthStatusKey, fmt.Errorf("Unauthorized access attempt to %s", handlerName)) - return - } - serv.Status.Clear(accessHealthStatusKey) - } - - if err := handler(w, r); err != nil { - serv.Status.Set(health.Critical, handlerName, err) - return - } - serv.Status.Clear(handlerName) - }) -} |
