openapi

package
v0.133.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package openapi provides types and helpers for building OpenAPI 3.0.3 specification documents that fleet services expose at /openapi.json.

Typical usage:

spec := openapi.New(cfg.AppName, cfg.Version)
// optionally enrich with handler annotations:
openapi.ScanDir(".", spec)
srv := server.New(cfg, server.WithOpenAPI(spec))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ScanDir

func ScanDir(dir string, spec *Spec) (int, error)

ScanDir walks dir for *.go files and adds any @openapi-annotated handler comments to spec. Returns the number of routes added.

Comment annotation format (all lines must be consecutive, directly above the func declaration or anywhere in the file; mixed-in blank comment lines are tolerated):

// @openapi GET /example
// @summary Returns an example response
// @tag example
// @param query url string true URL to scan
// @param query target string false Alias for url
// @response 200 {"message":"string"}
func handleExample(w http.ResponseWriter, r *http.Request) { … }

@param format: @param <in> <name> <type> <required> <description...>

in:       query | path | header | cookie
type:     string | integer | boolean | number
required: true | false
description: remainder of the line (spaces allowed, no quoting needed)

Only @openapi, @summary, @tag, @param, and @response are consumed. Unknown @-prefixed lines are silently ignored so the format can be extended forwards-compatibly.

Types

type Components

type Components struct {
	Schemas map[string]*Schema `json:"schemas,omitempty"`
}

Components holds reusable schema definitions.

type Info

type Info struct {
	Title       string `json:"title"`
	Version     string `json:"version"`
	Description string `json:"description,omitempty"`
}

Info carries the service identity block.

type MediaTypeObject

type MediaTypeObject struct {
	Schema *Schema `json:"schema,omitempty"`
}

MediaTypeObject wraps a Schema for a given media-type key.

type Operation

type Operation struct {
	Summary     string              `json:"summary,omitempty"`
	Description string              `json:"description,omitempty"`
	Tags        []string            `json:"tags,omitempty"`
	Parameters  []Parameter         `json:"parameters,omitempty"`
	Responses   map[string]Response `json:"responses"`
}

Operation is a single HTTP method on a path.

type Parameter

type Parameter struct {
	Name        string  `json:"name"`
	In          string  `json:"in"` // "path", "query", "header", "cookie"
	Description string  `json:"description,omitempty"`
	Required    bool    `json:"required,omitempty"`
	Schema      *Schema `json:"schema,omitempty"`
}

Parameter describes a path, query, header, or cookie parameter.

type PathItem

type PathItem struct {
	Get    *Operation `json:"get,omitempty"`
	Post   *Operation `json:"post,omitempty"`
	Put    *Operation `json:"put,omitempty"`
	Delete *Operation `json:"delete,omitempty"`
	Patch  *Operation `json:"patch,omitempty"`
}

PathItem groups the operations available on a single URL path.

type Response

type Response struct {
	Description string                     `json:"description"`
	Content     map[string]MediaTypeObject `json:"content,omitempty"`
}

Response describes a single HTTP response.

type Schema

type Schema struct {
	Type       string             `json:"type,omitempty"`
	Properties map[string]*Schema `json:"properties,omitempty"`
	Example    interface{}        `json:"example,omitempty"`
}

Schema is a simplified JSON Schema subset used inside OpenAPI.

type Spec

type Spec struct {
	OpenAPI    string              `json:"openapi"` // always "3.0.3"
	Info       Info                `json:"info"`
	Paths      map[string]PathItem `json:"paths"`
	Components *Components         `json:"components,omitempty"`
}

Spec is the root OpenAPI 3.0.3 document. Only the fields fleet services use are included — the type is intentionally minimal.

func New

func New(title, version string) *Spec

New creates a Spec pre-populated with the four standard fleet endpoints that every container service exposes via go-common/server:

  • GET /health → 200 {"status":"healthy"}
  • GET /version → 200 text/plain
  • GET /selftest → 200 {}
  • GET /openapi.json → 200 (this document, self-referential)

func (*Spec) AddRoute

func (s *Spec) AddRoute(method, path string, op Operation)

AddRoute adds or replaces an operation for method+path in the spec. method is case-insensitive ("GET", "get", etc.).

func (*Spec) JSON

func (s *Spec) JSON() ([]byte, error)

JSON serialises the spec to indented JSON bytes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL