apidoc

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2016 License: MIT Imports: 14 Imported by: 0

README

apidoc

Automatic API Document Generator.

Supporting web framework

  • gin

Usage

gin
type ginBodyWriter struct {
	gin.ResponseWriter
	body *bytes.Buffer
}

func (w ginBodyWriter) Body() []byte {
	return w.body.Bytes()
}

func (w ginBodyWriter) Write(b []byte) (int, error) {
	w.body.Write(b)
	return w.ResponseWriter.Write(b)
}

func (w ginBodyWriter) WriteString(s string) (int, error) {
	w.body.WriteString(s)
	return w.ResponseWriter.WriteString(s)
}

func newGinBodyWriter(w gin.ResponseWriter) *ginBodyWriter {
	return &ginBodyWriter{body: bytes.NewBufferString(""), ResponseWriter: w}
}

func apidocMiddleware(c *gin.Context) {
	if apidoc.IsDisabled() {
		return
	}

	api := apidoc.NewAPI()
	// Ignore header names
	api.SuppressedRequestHeaders("Cache-Control", "Content-Length", "X-Request-Id", "ETag", "Set-Cookie")
	api.ReadRequest(c.Request, false)

	// Need to implement own Writer intercepting Write(), WriteString() calls to get response body.
	gbw := newGinBodyWriter(c.Writer)
	c.Writer = gbw

	// before processing request

	c.Next()

	// after processing request

	// Ignore header names
	api.SuppressedResponseHeaders("Cache-Control", "Content-Length", "X-Request-Id", "X-Runtime", "X-XSS-Protection", "ETag")
	api.ReadResponseHeader(c.Writer.Header())
	api.WrapResponseBody(gbw.Body())
	api.ResponseStatusCode = c.Writer.Status()

	apidoc.Gen(api)
}

func getEngine() *gin.Engine {
	r := gin.Default()
	r.Use(apidocMiddleware)

	...

	return r
}

func init() {
	d := flag.Bool("d", false, "disable api doc")
	flag.Parse()
	if *d {
		apidoc.Disable()
	}
	if apidoc.IsDisabled() {
		return
	}
	apidoc.Init(apidoc.Project{
		DocumentTitle: "readme",
		DocumentPath:  "readme-apidoc.html",
		TemplatePath:  "readme.tpl.html",
	})
}

func main() {
	// Listen and Server in 0.0.0.0:8080
	getEngine().Run(":8080")
}

View

view.png

https://gotokatsuya.github.io/apidoc/example/gin/custom-apidoc.html

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear() error

Clear delete all files

func Disable

func Disable()

Disable disable generator

func Enable

func Enable()

Enable enable generator

func Gen

func Gen(api API) error

Gen generate api document

func Init

func Init(newProject Project) error

Init initialize project setting

func IsDisabled

func IsDisabled() bool

IsDisabled ref disable

func PrettyPrint

func PrettyPrint(in []byte) ([]byte, error)

PrettyPrint print rich json

Types

type API

type API struct {
	// Request
	RequestMethod            string            `json:"request_method"`
	RequestPath              string            `json:"request_path"`
	RequestHeaders           map[string]string `json:"request_headers"`
	RequestSuppressedHeaders map[string]bool   `json:"request_suppressed_headers"`
	RequestURLParams         map[string]string `json:"request_url_params"`
	RequestPostForms         map[string]string `json:"request_post_forms"`
	RequestBody              string            `json:"request_body"`

	// Response
	ResponseHeaders           map[string]string `json:"response_headers"`
	ResponseSuppressedHeaders map[string]bool   `json:"response_suppressed_headers"`
	ResponseStatusCode        int               `json:"response_status_code"`
	ResponseBody              string            `json:"response_body"`
}

API has request and response info

func NewAPI

func NewAPI() API

NewAPI new api instance

func (*API) ReadRequest

func (a *API) ReadRequest(req *http.Request, throwErr bool) error

ReadRequest read values from http.Request

func (*API) ReadRequestBody

func (a *API) ReadRequestBody(req *http.Request) error

ReadRequestBody read request body Reference https://golang.org/src/net/http/httputil/dump.go

func (*API) ReadRequestHeader

func (a *API) ReadRequestHeader(httpHeader http.Header) error

ReadRequestHeader read request http.Header

func (*API) ReadRequestURLParams

func (a *API) ReadRequestURLParams(uri string) error

ReadRequestURLParams read request uri

func (*API) ReadResponseHeader

func (a *API) ReadResponseHeader(httpHeader http.Header) error

ReadResponseHeader read http.Header

func (*API) SuppressedRequestHeaders

func (a *API) SuppressedRequestHeaders(headers ...string)

SuppressedRequestHeaders ignore request headers

func (*API) SuppressedResponseHeaders

func (a *API) SuppressedResponseHeaders(headers ...string)

SuppressedResponseHeaders ignore response headers

func (*API) WrapResponseBody

func (a *API) WrapResponseBody(body []byte) error

WrapResponseBody wrap body prettyprint if json

type Project

type Project struct {
	DocumentTitle string
	DocumentPath  string
	TemplatePath  string

	APIs []API
}

Project has project setting

Directories

Path Synopsis
example
gin

Jump to

Keyboard shortcuts

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