Files
llmgw/internal/docgen/gen/main.go
T
Ilker ManapandClaude Opus 5.5 fd3c7bda32
CI / test (push) Failing after 6m12s
The client API described in OpenAPI, and both APIs in Swagger UI
/v1/openapi.json is the API clients use — OpenAI's endpoints, Anthropic's,
and what llmgw adds — in OpenAPI 3.1, generated from internal/clientapi's
route table by the generator the admin API's document comes from, now
told each document's title, server, token and error. A test reads the
router's mux.HandleFunc calls and refuses a route described and not
mounted, or mounted and not described. Asked for before there is a key,
so it needs none.

/docs/api shows it in Swagger UI 5.33.1 (Apache 2.0, served from the
gateway as the page's policy requires): Authorize takes a key, Try it out
sends to this gateway. Staff are offered the admin API's document too,
from /docs/api/admin.json, which nobody else is given.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-10 20:22:48 +02:00

73 lines
2.4 KiB
Go

// Command gen writes docs/reference.md from the configuration structs, and
// docs/admin-api.openapi.json from the admin API route table.
//
// Run by `go generate ./...`, and checked by a test, so the reference cannot
// quietly stop describing the code.
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"gitea.mynodes.xyz/ilker/llmgw/internal/clientapi"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
"gitea.mynodes.xyz/ilker/llmgw/internal/docgen"
"gitea.mynodes.xyz/ilker/llmgw/internal/docgen/openapi"
)
func main() {
// The repository root, found from this file rather than from the working
// directory, so `go generate ./...` produces the same document wherever
// it is run from.
_, self, _, ok := runtime.Caller(0)
if !ok {
fmt.Fprintln(os.Stderr, "error: cannot locate the generator")
os.Exit(1)
}
root := filepath.Join(filepath.Dir(self), "..", "..", "..")
paths := make([]string, len(docgen.Sources))
for i, s := range docgen.Sources {
paths[i] = filepath.Join(root, s)
}
blocks, err := docgen.Collect(paths)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
if err := os.WriteFile(filepath.Join(root, "docs", "reference.md"), []byte(docgen.Render(blocks)), 0o644); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
if err := os.WriteFile(filepath.Join(root, "internal", "docgen", "reference_gen.go"), []byte(docgen.RenderGo(blocks)), 0o644); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
fmt.Printf("docs/reference.md and internal/docgen/reference_gen.go: %d blocks\n", len(blocks))
spec, err := openapi.Generate(root, adminapi.Routes, adminapi.PathParams)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
if err := os.WriteFile(filepath.Join(root, "docs", "admin-api.openapi.json"), spec, 0o644); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
fmt.Printf("docs/admin-api.openapi.json: %d routes\n", len(adminapi.Routes))
spec, err = openapi.GenerateDoc(root, openapi.Client, clientapi.Routes, clientapi.PathParams)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
if err := os.WriteFile(filepath.Join(root, "docs", "client-api.openapi.json"), spec, 0o644); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
fmt.Printf("docs/client-api.openapi.json: %d routes\n", len(clientapi.Routes))
}