[breaking-change] Add a health check endpoint.

Also, simplify the listen endpoint configuration format.
This commit is contained in:
Catherine
2025-09-17 15:36:27 +00:00
parent 13c72015a7
commit ea2c2c5d2e
4 changed files with 37 additions and 19 deletions

View File

@@ -1,10 +1,7 @@
[pages]
protocol = "tcp"
address = ":3333"
[caddy]
protocol = "tcp"
address = ":3334"
[listen]
pages = "tcp/:3000"
caddy = "tcp/:3001"
health = "tcp/:3002"
[wildcard]
domain = "codeberg.page"

View File

@@ -6,19 +6,17 @@ import (
"github.com/pelletier/go-toml/v2"
)
type ListenConfig struct {
Protocol string `toml:"protocol"`
Address string `toml:"address"`
}
type CacheConfig struct {
MaxSize uint64 `toml:"max-size"` // in bytes
MaxAge string `toml:"max-age"`
}
type Config struct {
Pages ListenConfig `toml:"pages"`
Caddy ListenConfig `toml:"caddy"`
Listen struct {
Pages string `toml:"pages"`
Caddy string `toml:"caddy"`
Health string `toml:"health"`
} `toml:"listen"`
Wildcard struct {
Domain string `toml:"domain"`
CloneURL string `toml:"clone-url"`

11
src/health.go Normal file
View File

@@ -0,0 +1,11 @@
package main
import (
"fmt"
"net/http"
)
func ServeHealth(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "ok")
}

View File

@@ -6,12 +6,18 @@ import (
"net"
"net/http"
"os"
"strings"
)
var backend Backend
func serveHandler(name string, listen ListenConfig, serve func(http.ResponseWriter, *http.Request)) {
listener, err := net.Listen(listen.Protocol, listen.Address)
func serveHandler(name string, listen string, serve func(http.ResponseWriter, *http.Request)) {
protocol, address, ok := strings.Cut(listen, "/")
if !ok {
log.Fatalf("%s: %s: malformed endpoint", name, listen)
}
listener, err := net.Listen(protocol, address)
if err != nil {
log.Fatalf("%s: %s\n", name, err)
}
@@ -73,9 +79,15 @@ func main() {
log.Println("ready")
if config.Caddy != (ListenConfig{}) {
go serveHandler("caddy", config.Caddy, ServeCaddy)
go serveHandler("pages", config.Listen.Pages, ServePages)
if config.Listen.Caddy != "" {
go serveHandler("caddy", config.Listen.Caddy, ServeCaddy)
}
serveHandler("pages", config.Pages, ServePages)
if config.Listen.Health != "" {
go serveHandler("health", config.Listen.Health, ServeHealth)
}
select {}
}