WIP: App health check endpoint #48

Draft
ada wants to merge 2 commits from health-check into main
4 changed files with 98 additions and 36 deletions
Showing only changes of commit 36a6ad08e9 - Show all commits

View file

@ -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)

View file

@ -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)
}
}

View file

@ -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)
}

View file

@ -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()