App health check endpoint
All checks were successful
ci/woodpecker/push/release Pipeline was successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/release Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful

This commit is contained in:
Ada 2024-05-13 12:14:22 +02:00
parent 1381a071d6
commit a1a1d13b86
Signed by: ada
GPG key ID: 6A7F898157C6DE6E
2 changed files with 36 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"git.gnous.eu/gnouseu/plakken/internal/constant" "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/plak"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
) )
@ -40,6 +41,7 @@ func (config ServerConfig) router() {
http.HandleFunc("GET /{$}", config.home) http.HandleFunc("GET /{$}", config.home)
http.Handle("GET /static/{file}", http.FileServer(staticFiles)) 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("GET /{key}/{settings...}", WebConfig.View)
http.HandleFunc("POST /{$}", WebConfig.CurlCreate) http.HandleFunc("POST /{$}", WebConfig.CurlCreate)
http.HandleFunc("POST /create/{$}", WebConfig.PostCreate) http.HandleFunc("POST /create/{$}", WebConfig.PostCreate)

View file

@ -0,0 +1,34 @@
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)
}
}