mirror of
https://github.com/juanfont/headscale.git
synced 2026-07-17 16:36:02 +00:00
Reimplement the v1 API as a code-first Huma service in hscontrol/api/v1, a thin adapter over the state layer. Huma emits the OpenAPI 3.1 document (openapi/v1/headscale.yaml) from the Go operation and type definitions; cmd/gen-openapi writes it and the 3.0.3 downgrade the client is generated from. Responses reproduce the protojson wire contract (string-encoded 64-bit IDs, all fields emitted, RFC3339/null timestamps); errors map to the correct HTTP status (404/400/409, 500 for server faults) via mapError. Serve it on the chi router at /api/v1 behind the existing API-key middleware, and point /swagger at the emitted spec.
41 lines
1 KiB
Go
41 lines
1 KiB
Go
package apiv1
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
)
|
|
|
|
// HealthResponseBody mirrors the v1 HealthResponse message. database_connectivity
|
|
// is reported true only when the database responds to a ping.
|
|
type HealthResponseBody struct {
|
|
DatabaseConnectivity bool `json:"databaseConnectivity"`
|
|
}
|
|
|
|
type healthOutput struct {
|
|
Body HealthResponseBody
|
|
}
|
|
|
|
func init() {
|
|
registrations = append(registrations, registerHealth)
|
|
}
|
|
|
|
func registerHealth(api huma.API, b Backend) {
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "health",
|
|
Method: http.MethodGet,
|
|
Path: "/api/v1/health",
|
|
Summary: "Health check",
|
|
Description: "Reports server health, including database connectivity.",
|
|
Tags: []string{"Health"},
|
|
Security: bearerAuth,
|
|
}, func(ctx context.Context, _ *struct{}) (*healthOutput, error) {
|
|
err := b.State.PingDB(ctx)
|
|
if err != nil {
|
|
return nil, mapError("pinging database", err)
|
|
}
|
|
|
|
return &healthOutput{Body: HealthResponseBody{DatabaseConnectivity: true}}, nil
|
|
})
|
|
}
|