From 36a6ad08e9ea20d3f67aba266b2e4b86c1be8dd3 Mon Sep 17 00:00:00 2001 From: Ada Date: Thu, 16 May 2024 15:49:01 +0200 Subject: [PATCH] WIP --- internal/httpserver/server.go | 8 +++- internal/web/health/health.go | 34 ------------- internal/web/status/status.go | 90 +++++++++++++++++++++++++++++++++++ main.go | 2 + 4 files changed, 98 insertions(+), 36 deletions(-) delete mode 100644 internal/web/health/health.go create mode 100644 internal/web/status/status.go diff --git a/internal/httpserver/server.go b/internal/httpserver/server.go index 56c1053..fbc55fa 100644 --- a/internal/httpserver/server.go +++ b/internal/httpserver/server.go @@ -4,10 +4,11 @@ import ( "embed" "log" "net/http" + "time" "git.gnous.eu/gnouseu/plakken/internal/constant" - "git.gnous.eu/gnouseu/plakken/internal/web/health" "git.gnous.eu/gnouseu/plakken/internal/web/plak" + "git.gnous.eu/gnouseu/plakken/internal/web/status" "github.com/redis/go-redis/v9" ) @@ -17,6 +18,7 @@ type ServerConfig struct { DB *redis.Client Static embed.FS Templates embed.FS + StartTime time.Time } func (config ServerConfig) home(w http.ResponseWriter, _ *http.Request) { @@ -41,7 +43,9 @@ func (config ServerConfig) router() { http.HandleFunc("GET /{$}", config.home) http.Handle("GET /static/{file}", http.FileServer(staticFiles)) - http.HandleFunc("GET /health/", health.Config{DB: config.DB}.Health) + http.HandleFunc("GET /readyz", status.Config{DB: config.DB}.Ready) + http.HandleFunc("GET /infoz", status.Config{DB: config.DB, StartTime: config.StartTime}.Info) + http.HandleFunc("GET /healthz", status.Config{DB: config.DB}.Health) http.HandleFunc("GET /{key}/{settings...}", WebConfig.View) http.HandleFunc("POST /{$}", WebConfig.CurlCreate) http.HandleFunc("POST /create/{$}", WebConfig.PostCreate) diff --git a/internal/web/health/health.go b/internal/web/health/health.go deleted file mode 100644 index 40b0aaa..0000000 --- a/internal/web/health/health.go +++ /dev/null @@ -1,34 +0,0 @@ -package health - -import ( - "io" - "log" - "net/http" - - "git.gnous.eu/gnouseu/plakken/internal/database" - "github.com/redis/go-redis/v9" -) - -type Config struct { - DB *redis.Client -} - -func (config Config) Health(w http.ResponseWriter, _ *http.Request) { - err := database.Ping(config.DB) - w.Header().Set("Content-Type", "text/plain; charset=utf-8") - if err != nil { - log.Println(err) - w.WriteHeader(http.StatusInternalServerError) - _, err := io.WriteString(w, "Redis connection has failed") - if err != nil { - log.Println(err) - } - - return - } - w.WriteHeader(http.StatusOK) - _, err = io.WriteString(w, "ok") - if err != nil { - log.Println(err) - } -} diff --git a/internal/web/status/status.go b/internal/web/status/status.go new file mode 100644 index 0000000..789ce8a --- /dev/null +++ b/internal/web/status/status.go @@ -0,0 +1,90 @@ +package status + +import ( + "encoding/json" + "io" + "log" + "net/http" + "runtime" + "time" + + "git.gnous.eu/gnouseu/plakken/internal/database" + "github.com/redis/go-redis/v9" +) + +type Config struct { + DB *redis.Client + StartTime time.Time +} + +type info struct { + Uptime time.Duration `json:"uptime"` + Version string `json:"version"` + GoVersion string `json:"goVersion"` + Source string `json:"source"` +} + +type health struct { + Status string `json:"status"` + DB string `json:"db"` // TODO: struct with ping duration ? +} + +func (config Config) Ready(w http.ResponseWriter, _ *http.Request) { + err := database.Ping(config.DB) + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusServiceUnavailable) + _, err := io.WriteString(w, "ko") + if err != nil { + log.Println(err) + } + + return + } + w.WriteHeader(http.StatusOK) + _, err = io.WriteString(w, "ok") + if err != nil { + log.Println(err) + } +} + +func (config Config) Info(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + response := info{ + Uptime: uptime(config.StartTime), + Version: "nightly", // TODO + GoVersion: runtime.Version(), + Source: "https://git.gnous.eu/gnouseu/plakken", + } + + err := json.NewEncoder(w).Encode(response) + if err != nil { + log.Println(err) + } +} + +func (config Config) Health(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + var response health + if database.Ping(config.DB) != nil { + response.DB = "ko" + } else { + response.DB = "ok" + } + + if response.DB == "ok" { + response.Status = "ok" + } else { + response.Status = "ko" + } + + err := json.NewEncoder(w).Encode(response) + if err != nil { + log.Println(err) + } +} + +func uptime(startTime time.Time) time.Duration { + return time.Since(startTime) +} diff --git a/main.go b/main.go index a79b0be..1bf8832 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "embed" "log" + "time" "git.gnous.eu/gnouseu/plakken/internal/config" "git.gnous.eu/gnouseu/plakken/internal/database" @@ -31,6 +32,7 @@ func main() { DB: db, Static: static, Templates: templates, + StartTime: time.Now(), } serverConfig.Server()