Compare commits

..

2 commits

Author SHA1 Message Date
Ada
36a6ad08e9
WIP
Some checks failed
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/release 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 failed
2024-05-16 16:01:23 +02:00
Ada
a1a1d13b86
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
2024-05-13 12:17:36 +02:00
8 changed files with 104 additions and 14 deletions

View file

@ -1,6 +1,6 @@
steps:
lint:
image: golang:1.23
image: golang:1.22
commands:
- go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
- golangci-lint run

View file

@ -1,6 +1,6 @@
steps:
- name: Build
image: golang:1.23
image: golang:1.22
commands:
- go mod download
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-w -s" -o plakken-linux-amd64 # Enable static binary, target Linux, remove debug information and strip binary

View file

@ -1,5 +1,5 @@
# Build
FROM golang:1.23 AS build
FROM golang:1.22 AS build
WORKDIR /build

6
go.mod
View file

@ -4,12 +4,12 @@ go 1.22
require (
github.com/joho/godotenv v1.5.1
github.com/redis/go-redis/v9 v9.6.1
golang.org/x/crypto v0.27.0
github.com/redis/go-redis/v9 v9.5.1
golang.org/x/crypto v0.23.0
)
require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
)

8
go.sum
View file

@ -10,15 +10,7 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.5.2 h1:L0L3fcSNReTRGyZ6AqAEN0K56wYeYAwapBIhkvh0f3E=
github.com/redis/go-redis/v9 v9.5.2/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

View file

@ -4,9 +4,11 @@ import (
"embed"
"log"
"net/http"
"time"
"git.gnous.eu/gnouseu/plakken/internal/constant"
"git.gnous.eu/gnouseu/plakken/internal/web/plak"
"git.gnous.eu/gnouseu/plakken/internal/web/status"
"github.com/redis/go-redis/v9"
)
@ -16,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) {
@ -40,6 +43,9 @@ func (config ServerConfig) router() {
http.HandleFunc("GET /{$}", config.home)
http.Handle("GET /static/{file}", http.FileServer(staticFiles))
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

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