diff --git a/internal/httpserver/server.go b/internal/httpserver/server.go index 2065dbd..56c1053 100644 --- a/internal/httpserver/server.go +++ b/internal/httpserver/server.go @@ -6,6 +6,7 @@ import ( "net/http" "git.gnous.eu/gnouseu/plakken/internal/constant" + "git.gnous.eu/gnouseu/plakken/internal/web/health" "git.gnous.eu/gnouseu/plakken/internal/web/plak" "github.com/redis/go-redis/v9" ) @@ -40,6 +41,7 @@ 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 /{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 new file mode 100644 index 0000000..6a533c6 --- /dev/null +++ b/internal/web/health/health.go @@ -0,0 +1,33 @@ +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) + } +}