Add a command to write the OpenAPI spec without a server
`just openapi [dir]` (go run ./cmd/openapi) writes openapi.json (3.1) and openapi-3.0.json without a database or configuration. A test checks it matches the spec the server serves.
This commit is contained in:
1 parent
973ebf224b
commit
6da96f79e7
6 files changed
+108
-1
No files matched your search
@@ -0,0 +1,51 @@
|
||||
// Command openapi writes the MusicServer API description without starting the server.
|
||||
//
|
||||
// go run ./cmd/openapi -out .
|
||||
//
|
||||
// It writes openapi.json (OpenAPI 3.1) and openapi-3.0.json (OpenAPI 3.0, for tools
|
||||
// that don't support 3.1 yet). A running server serves the same files at /openapi.json
|
||||
// and /openapi-3.0.json.
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"music-server/internal/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
out := flag.String("out", ".", "directory to write openapi.json and openapi-3.0.json to")
|
||||
flag.Parse()
|
||||
|
||||
if err := run(*out); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "openapi:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(out string) error {
|
||||
spec := server.OpenAPISpec()
|
||||
v31, err := spec.MarshalJSON()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v30, err := spec.Downgrade()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(out, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
for name, data := range map[string][]byte{"openapi.json": v31, "openapi-3.0.json": v30} {
|
||||
path := filepath.Join(out, name)
|
||||
if err := os.WriteFile(path, data, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Wrote", path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user