// Command openapi writes the API's OpenAPI spec to api/openapi.json. // Clients (the web frontend, generated SDKs) treat that file as the // contract for what the server exposes; regenerate it after any route // change with `just openapi`. // // It builds the same route tree as cmd/api, but against a stub echo // server with no real database connection: registering routes only // inspects Go types via reflection and never calls the database, so a nil // *db.Queries (which still satisfies db.Querier) is enough. package main import ( "fmt" "os" "github.com/danielgtaylor/huma/v2" "github.com/danielgtaylor/huma/v2/adapters/humaecho" "github.com/labstack/echo/v4" "completed/internal/api" "completed/internal/db" ) func main() { e := echo.New() humaApi := humaecho.New(e, huma.DefaultConfig("Completed API", "1.0.0")) var queries db.Querier = (*db.Queries)(nil) api.RegisterRoutes(humaApi, queries) data, err := humaApi.OpenAPI().MarshalJSON() if err != nil { fmt.Fprintf(os.Stderr, "marshal OpenAPI spec: %v\n", err) os.Exit(1) } const outPath = "api/openapi.json" if err := os.MkdirAll("api", 0o755); err != nil { fmt.Fprintf(os.Stderr, "create api/ directory: %v\n", err) os.Exit(1) } if err := os.WriteFile(outPath, append(data, '\n'), 0o644); err != nil { fmt.Fprintf(os.Stderr, "write %s: %v\n", outPath, err) os.Exit(1) } fmt.Printf("Wrote %s\n", outPath) }