summaryrefslogtreecommitdiff
path: root/internal/server
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-08-22 11:14:01 +0300
committerPaul Buetow <paul@buetow.org>2024-08-22 11:14:01 +0300
commit97c9760b33888972b0488775b62bee6b6a9fa57e (patch)
tree9c755b965a95165447321be340cb9d32cc51cb0b /internal/server
parenta856e039035e14dc50ab6519e2ca9cd2508eb21b (diff)
some refactoring
Diffstat (limited to 'internal/server')
-rw-r--r--internal/server/cron/cron.go14
-rw-r--r--internal/server/health/health.go18
-rw-r--r--internal/server/server.go22
3 files changed, 26 insertions, 28 deletions
diff --git a/internal/server/cron/cron.go b/internal/server/cron/cron.go
index af9bdad..3de44b8 100644
--- a/internal/server/cron/cron.go
+++ b/internal/server/cron/cron.go
@@ -21,23 +21,23 @@ func Run(ctx context.Context, conf config.ServerConfig, status health.Status) {
case <-ctx.Done():
return
case <-helloTicker.C:
- run(ctx, "cron.Hello", status, func(ctx context.Context) error {
+ run(ctx, "cron->Hello", status, func(ctx context.Context) error {
log.Println("hello world")
return nil
})
case <-mergeTicker.C:
- run(ctx, "cron.Repository.Merge", status, repository.Instance(conf).MergeRemotely)
+ run(ctx, "cron->repository.Merge", status, repository.Instance(conf).MergeRemotely)
case <-scheduleTicker.C:
- run(ctx, "cron.Scheduler.Run", status, scheduler.Run)
+ run(ctx, "cron->scheduler.Run", status, scheduler.Run)
}
}
}
-func run(ctx context.Context, handlerName string, status health.Status, cb func(ctx context.Context) error) {
- log.Println("CRON ticker initiating", handlerName)
+func run(ctx context.Context, what string, status health.Status, cb func(ctx context.Context) error) {
+ log.Println("CRON ticker initiating", what)
if err := cb(ctx); err != nil {
- status.Set(health.Critical, handlerName, err)
+ status.Set(health.Critical, what, err)
return
}
- status.Clear(handlerName)
+ status.Clear(what)
}
diff --git a/internal/server/health/health.go b/internal/server/health/health.go
index 29cc5d1..261167e 100644
--- a/internal/server/health/health.go
+++ b/internal/server/health/health.go
@@ -52,26 +52,26 @@ func NewStatus() Status {
}
}
-func (hs Status) Set(s Severity, handlerName string, info any) {
+func (hs Status) Set(s Severity, healthStatusKey string, info any) {
hs.mu.Lock()
defer hs.mu.Unlock()
text := fmt.Sprintf("%v", info)
- log.Printf("status: alerting %s as %s: %v", handlerName, s, info)
+ log.Printf("status: alerting %s as %s: %v", healthStatusKey, s, info)
- hs.alerts[handlerName] = alert{
+ hs.alerts[healthStatusKey] = alert{
text: text,
severity: s,
}
}
-func (hs Status) Clear(handlerName string) {
+func (hs Status) Clear(healthStatusKey string) {
hs.mu.Lock()
defer hs.mu.Unlock()
- if _, ok := hs.alerts[handlerName]; ok {
- log.Println("status: clearing ", handlerName)
- delete(hs.alerts, handlerName)
+ if _, ok := hs.alerts[healthStatusKey]; ok {
+ log.Println("status: clearing ", healthStatusKey)
+ delete(hs.alerts, healthStatusKey)
}
}
@@ -84,8 +84,8 @@ func (hs Status) String() string {
hs.mu.Lock()
defer hs.mu.Unlock()
- for handlerName, alert := range hs.alerts {
- str := fmt.Sprintf("%s (handler %s)", alert, handlerName)
+ for healthStatusKey, alert := range hs.alerts {
+ str := fmt.Sprintf("%s (handler %s)", alert, healthStatusKey)
alerts[alert.severity] = append(alerts[alert.severity], str)
}
diff --git a/internal/server/server.go b/internal/server/server.go
index 0c77d4f..b2cb0d0 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -9,8 +9,6 @@ import (
"codeberg.org/snonux/gos/internal/server/health"
)
-const HealthHandlerName = `healthHandler`
-
type Server struct {
Status health.Status
Conf config.ServerConfig
@@ -19,10 +17,7 @@ type Server struct {
type HandlerFuncWithError func(http.ResponseWriter, *http.Request) error
func New(conf config.ServerConfig, status health.Status) Server {
- return Server{
- Conf: conf,
- Status: status,
- }
+ return Server{Conf: conf, Status: status}
}
func (serv Server) Handle(name string, handler HandlerFuncWithError) {
@@ -35,15 +30,18 @@ func (serv Server) Handle(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") != serv.Conf.APIKey {
- http.Error(w, "Invalid API key", http.StatusUnauthorized)
- log.Println("Unauthorized access attempt to", handlerName)
- return
+ 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 {
- log.Println(err)
- serv.Status.Set(health.Critical, handlerName, err.Error())
+ serv.Status.Set(health.Critical, handlerName, err)
return
}
serv.Status.Clear(handlerName)