# README
goapidoc
- A golang library for generating api document, including swagger2 and apib.
Function
- Support api, routes and definitions information
- Support generic definition type
- Support most of the functions for swagger 2
- Support basic functions for API Blueprint 1A
Usage
- Visit generate_test.go for detail examples, and api1.json / api2.apib for generated documents.
package main
import (
. "github.com/Aoi-hosizora/goapidoc"
)
func main() {
SetDocument("localhost:60001", "/",
NewInfo("Demo api", "This is a demo api only for testing goapidoc.", "1.0.0").
License(NewLicense("MIT", "")).
Contact(NewContact("", "https://github.com/Aoi-hosizora", "")),
)
SetOption(NewOption().
Schemes("http").
Tags(
NewTag("Authorization", "auth-controller"),
NewTag("User", "user-controller"),
).
Securities(
NewApiKeySecurity("jwt", HEADER, "Authorization"),
).
GlobalParams(
NewQueryParam("force_refresh", "boolean", false, "force refresh flag").Default(false),
NewHeaderParam("X-Special-Flag", "string", false, "a special flag in header").Example("token-xxx"),
),
)
AddOperations(
NewPostOperation("/auth/register", "Sign up").
Tags("Authorization").
Params(
NewBodyParam("param", "RegisterParam", true, "register param"),
).
Responses(
NewResponse(200, "Result"),
),
NewPostOperation("/auth/login", "Sign in").
Tags("Authorization").
Params(
NewBodyParam("param", "LoginParam", true, "login param"),
).
Responses(
NewResponse(200, "_Result<LoginDto>"),
),
NewGetOperation("/auth/me", "Get the authorized user").
Tags("Authorization").
Securities("jwt").
Responses(
NewResponse(200, "_Result<UserDto>"),
),
NewDeleteOperation("/auth/logout", "Sign out").
Tags("Authorization").
Securities("jwt").
Responses(
NewResponse(200, "Result"),
),
)
AddOperations(
NewGetOperation("/user", "Query all users").
Tags("User").
Securities("jwt").
Params(
NewQueryParam("page", "integer#int32", false, "query page").Default(1),
NewQueryParam("limit", "integer#int32", false, "page size").Default(20),
NewQueryParam("force_refresh", "boolean", false, "force refresh flag for querying users").Default(false),
NewHeaderParam("X-Special-Flag", "string", true, "a special flag in header, which must be set for querying users"),
).
Responses(
NewResponse(200, "_Result<_Page<UserDto>>"),
),
NewGetOperation("/user/{id}", "Query the specific user").
Tags("User").
Securities("jwt").
Params(
NewPathParam("id", "integer#int64", true, "user id"),
).
Responses(
NewResponse(200, "_Result<UserDto>"),
),
NewPutOperation("/user", "Update the authorized user").
Tags("User").
Securities("jwt").
Params(
NewBodyParam("param", "UpdateUserParam", true, "update user param"),
).
Responses(
NewResponse(200, "Result"),
),
NewDeleteOperation("/user", "Delete the authorized user").
Tags("User").
Securities("jwt").
Responses(
NewResponse(200, "Result"),
),
)
AddDefinitions(
NewDefinition("Result", "Global response").
Properties(
NewProperty("code", "integer#int32", true, "status code"),
NewProperty("message", "string", true, "status message"),
),
NewDefinition("_Result", "Global generic response").
Generics("T").
Properties(
NewProperty("code", "integer#int32", true, "status code"),
NewProperty("message", "string", true, "status message"),
NewProperty("data", "T", true, "response data"),
),
NewDefinition("_Page", "Global generic page response").
Generics("T").
Properties(
NewProperty("page", "integer#int32", true, "current page"),
NewProperty("limit", "integer#int32", true, "page size"),
NewProperty("total", "integer#int32", true, "total count"),
NewProperty("data", "T[]", true, "response data"),
),
NewDefinition("LoginParam", "Login parameter").
Properties(
NewProperty("username", "string", true, "username"),
NewProperty("password", "string", true, "password"),
),
NewDefinition("RegisterParam", "Register parameter").
Properties(
NewProperty("username", "string", true, "username"),
NewProperty("password", "string", true, "password"),
),
NewDefinition("UpdateUserParam", "Update user parameter").
Properties(
NewProperty("username", "string", true, "username"),
NewProperty("bio", "string", true, "user bio"),
NewProperty("gender", "string", true, "user gender").Enum("Secret", "Male", "Female"),
NewProperty("birthday", "string#date", true, "user birthday"),
),
NewDefinition("LoginDto", "Login response").
Properties(
NewProperty("user", "UserDto", true, "authorized user"),
NewProperty("token", "string", true, "access token"),
),
NewDefinition("UserDto", "User response").
Properties(
NewProperty("id", "integer#int64", true, "user id"),
NewProperty("username", "string", true, "username"),
NewProperty("bio", "string", true, "user bio"),
NewProperty("gender", "string", true, "user gender").Enum("Secret", "Male", "Female"),
NewProperty("birthday", "string#date", true, "user birthday"),
),
)
_, _ = SaveSwaggerYaml("./docs/api3.yaml")
_, _ = SaveSwaggerJson("./docs/api3.json")
_, _ = SaveApib("./docs/api3.apib")
}
References
# Functions
AddDefinitions adds some definitions into global Document.
AddOperations adds some operations into global Document.
CleanupDocument cleans up the global Document.
DisableWarningLogger disables the warning logger switcher.
EnableWarningLogger enables the warning logger switcher.
GenerateApib generates apib script and returns byte array.
GenerateSwaggerJson generates swagger json script and returns byte array.
GenerateSwaggerYaml generates swagger yaml script and returns byte array.
GetBasePath returns the basePath from global Document.
GetDefinitions returns the whole definitions from global Document.
GetHost returns the host from global Document.
GetInfo returns the info from global Document.
GetOperations returns the whole operations from global Document.
GetOption returns the option from global Document.
NewApiKeySecurity creates an apiKey authentication Security with given arguments.
NewBasicSecurity creates a basic authentication Security with given arguments.
NewBodyParam creates a body Param with given arguments.
NewContact creates a default Contact with given arguments.
NewDefinition creates a default Definition with given arguments.
NewDeleteOperation creates a delete Operation with given arguments.
NewDocument creates a default Document with given arguments.
NewExternalDoc creates a default ExternalDoc with given arguments.
NewFormParam creates a form Param with given arguments.
NewGetOperation creates a get Operation with given arguments.
NewHeaderParam creates a header Param with given arguments.
NewHeadOperation creates a head Operation with given arguments.
NewInfo creates a default Info with given arguments.
NewItemOption creates a default ItemOption.
NewLicense creates a default License with given arguments.
NewOAuth2Security creates an oauth2 authentication Security with given arguments.
NewOperation creates a default Operation with given arguments.
NewOption creates a default Option.
NewOptionsOperation creates an options Operation with given arguments.
NewParam creates a default Param with given arguments.
NewPatchOperation creates a patch Operation with given arguments.
NewPathParam creates a path Param with given arguments.
NewPostOperation creates a post Operation with given arguments.
NewProperty creates a default Property with given arguments.
NewPutOperation creates a put Operation with given arguments.
NewQueryParam creates a query Param with given arguments.
NewResponse creates a default Response with given arguments.
NewResponseExample creates a default ResponseExample with given arguments.
NewResponseHeader creates a default Header with given arguments.
NewRoutesOption creates a default RoutesOption with given arguments.
NewSecurity creates a default Security with given arguments.
NewSecurityScope creates a default SecurityScope with given arguments.
NewTag creates a default Tag with given arguments.
NewXMLRepr creates a default XMLRepr with given arguments.
SaveApib generates apib script and saves into file.
SaveSwaggerJson generates swagger json script and saves into file.
SaveSwaggerYaml generates swagger yaml script and saves into file.
SetBasePath sets the basePath in global Document.
SetDefinitions sets the whole definitions in global Document.
SetDocument sets the basic information for global Document.
SetHost sets the host in global Document.
SetInfo sets the info in global Document.
SetOperations sets the whole operations in global Document.
SetOption sets the option in Document.
# Constants
ACCESSCODE_FLOW oauth2 flow: accessCode.
ALL mime data: */*.
APIKEY security type: api key authentication.
APPLICATION_FLOW oauth2 flow: application.
ARRAY type: array.
BASIC security type: basic authentication.
BINARY format: any sequence of octets.
BODY param.
BOOLEAN type: boolean.
BYTE format: base64 encoded characters.
CSV collection format: foo,bar.
DATE format: As defined by full-date - RFC3339.
DATETIME format: As defined by date-time - RFC3339.
DELETE method.
DOUBLE format: double.
FILE type: file.
FLOAT format: float.
FORM param.
GET method.
GIF mime data: image/gif.
HEAD method.
HEADER param.
HTML mime data: text/html.
IMPLICIT_FLOW oauth2 flow: implicit.
INT32 format: signed 32 bits.
INT64 format: signed 64 bits.
INTEGER type: integer, long.
JPEG mime data: image/jpeg.
JSON mime data: application/json.
MPFD mime data: multipart/form-data.
MULTI collection format: foo=bar&foo=baz.
NUMBER type: float, double.
OAUTH2 security type: oauth2 authentication.
OBJECT type: object.
OPTIONS method.
PASSWORD format: Used to hint UIs the input needs to be obscured.
PASSWORD_FLOW oauth2 flow: password.
PATCH method.
PATH param.
PIPES collection format: foo|bar.
PLAIN mime data: text/plain.
PNG mime data: image/png.
POST method.
PUT method.
QUERY param.
SSV collection format: foo bar.
STRING type: string, byte, binary, date, dateTime, password.
TSV collection format: foo\tbar.
URL mime data: application/x-www-form-urlencoded.
XML mime data: application/xml.
# Structs
Contact represents a contact information of Document.
Definition represents an api definition information of Document.
Document represents an api document information.
ExternalDoc represents an external documentation information of Document, Tag and Operation.
Info represents a basic information of Document.
ItemOption represents an array type's item option of Param, Property and ItemOption itself, this is only supported in Swagger.
License represents a license information of Document.
Operation represents an api operation information of Document.
Option represents an api extra option of Document.
Param represents a request parameter information of Operation.
Property represents a definition property information of Definition.
Response represents an operation response information of Operation.
ResponseExample represents a response example information of Response.
ResponseHeader represents a response header information of Response.
RoutesOption represents a routes group option of Document, this is only supported in API Blueprint.
Security represents a security definition information of Document.
SecurityScope represents a security scope information of Document.
Tag represents a tag information of Document.
XMLRepr represents a xml representation format information of Definition, Property, Param an ItemOption, this is only supported in Swagger.