Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

apidocgen is a tool for Go to generate apis markdown docs and mocks.

License

NotificationsYou must be signed in to change notification settings

alovn/apidocgen

Repository files navigation

Build Status

English |简体中文

apidocgen is a tool for Go to generate apis markdown docs and mocks.

Install

go install github.com/alovn/apidocgen@latest

Cli

$ apidocgen --helpapidocgen is a toolfor Go to generate apis markdown docs and mocks. For example:apidocgen --dir= --excludes= --output= --output-index= --template= --single --gen-mocksapidocgen --mock --mock-listen=:8001apidocgen mock --data= --listen=Usage:  apidocgen [flags]  apidocgen [command]Available Commands:help        Help about anycommand  mock          version     print versionFlags:      --dir string            Search apis dir, comma separated (default".")      --excludes string       Exclude directories and files when searching, comma separated      --gen-mocks             Is generate the mock files  -h, --helphelpfor apidocgen      --mock-listen string    Mock Server listen address (default"localhost:8001")      --mock                  Serve a mock server      --output string         Generate markdown files dir (default"./docs/")      --output-index string   Generate index file name      --single                Is generate a single doc      --template string       Template name or custom template directory, built-in includes markdown and apidocsUse"apidocgen [command] --help"for more information about a command.

Template

The built-in templates includemarkdown andapidocs, default ismarkdown.

The templateapidocs is the template for generate websiteapidocs.

You can also use the custom template:

apidocgen \    --dir=svc-user,common \    --template=/Users/xxx/workspace/apidocs/custom-template-direcoty \    --output=./docs

How to use

apidocgen supported any web frameworks. here are an example usingbytego.

  1. Add API annotations in main.go code:

    //@title UserService//@service svc-user//@version 1.0.1//@desc the api about users//@baseurl /userfuncmain() {r:=bytego.New()c:=controller.NewController()//@group account//@title Account//@desc account register and loginaccount:=r.Group("/account")    {account.POST("/register",c.Register)account.POST("/login",c.Login)    }_=r.Run(":8000")}
  2. Add API annotations in httpHandler.

    //@title AccountLogin//@api POST /account/login//@group account//@accept json//@format json//@request LoginRequest//@response 200 common.Response{code=0,msg="success",data=LoginResponse} "success description" //mock//@response 200 common.Response{code=10020,msg="password_error"} "error description"//@author alovnfunc (c*Controller)Login(c*bytego.Ctx) {//bind LoginRequestres:=common.NewResponse(0,"success",&LoginResponse{WelcomeMsg:"welcome",    })c.JSON(http.StatusOK,res)}
  3. Executeapidocgen.

    apidocgen

    The markdown files will be generated in./docs.

Examples

Some examples and generated markdown docs are here:apidocgen/examples.

An online generated api docs site:https://apidocgen.netlify.app/

Comments(annotations)

annotationdescriptionexample
serviceRequired, the service's identification@service svc-user
baseurlThe base url of api@baseurl /
groupThe group of service, add it at api comment or at the head of file comment.@group account
titleThe title of service, group and api@title UserService
descThe description of service, group and api@desc xxx
apiThe http api handler@api POST /account/register
orderSort groups and apis@order 1
authorThe author of api@author alovn
versionThe version of service or api@version 1.0.1
acceptThe request format, support json/xml@accept json
formatThe response format, support json/xml@format json
requestThe request body@request LoginRequest
responseThe response body, [http code] [data type]@response 200 LoginResponse
successAs same as response@success 200 LoginResponse
failureAs same as response@failure 200 LoginResponse
paramThe path parameter of router/user/:id, parameters separated by spaces [name] [type] [required] [comment],@param id int true "user_id"
queryThe query parameter of route,/user?id=, parameters same as @param@query id int true "user_id"
headerThe request HTTP header parameter, parameters same as @param@header X-Request-ID string false "request id"
formThe form parameter of request, parameters same as @param@form id int true "user_id"
deprecatedMark api as deprecated.@deprecated

Response

  1. response user defined struct.

    typeUserstruct {IDint`json:"id"`Namestring`json:"name"`}//@response 200 User "description"
  2. response struct with array.

    //@response 200 []User
  3. a composition common response.

    typeResponsestruct {Codeint`json:"code"`Msgstring`json:"msg"`Datainterface{}`json:"data"`}//@response 200 Response{code=10001,msg="some error"} "some error description"//@response 200 Response{code=0,data=User} "success description"//@response 200 Response{code=0,data=[]User} "success description"

    if import package ofcommon.Response:

    import ("common")//@response 200 common.Response{code=0,data=User} "success description"
  4. example value of struct

    typeUserstruct {IDint`json:"id" example:"100010"`Namestring`json:"name" example:"user name"`}//User Infomation

Mock

  1. generate apis mocks files. add//mock at the end ofresponse, default use first.

    //@response 200 Response{code=0,data=[]User} "success description" //mock

    generate apis mocks files, default generated in the directory./docs/mocks.

    apidocgen --dir=common,svc-user --gen-mocks
  2. serve the mock server from generated mock files.

    apidocgen mock --data=./mocks --listen=:8001
  3. serve the mock server from source code (dot't generate mock files).

    apidocgen --dir=common,svc-user --mock --mock-listen=:8001
  4. request mock server usingcurl client.

    $ curl -X POST -d"" localhost:8001/user/account/login   {"code": 0,"data": {"welcome_msg":"string"  },"msg":"success"}

About

apidocgen is a tool for Go to generate apis markdown docs and mocks.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp