apidoc

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2021 License: MIT Imports: 9 Imported by: 2

Documentation

Overview

Package apidoc provides html document builder for http requests and responses.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultStyle

func DefaultStyle() *Element

func JsonResponse

func JsonResponse(resp *http.Response) *Element

func JsonResponseFrom

func JsonResponseFrom(h http.Handler, r *http.Request) *Element

func RawRequest

func RawRequest(r *http.Request) *Element

func RawResponse

func RawResponse(resp *http.Response) *Element

func RawResponseFrom

func RawResponseFrom(h http.Handler, r *http.Request) *Element

Types

type Doc

type Doc struct {
	*http.Request
	// contains filtered or unexported fields
}

func NewDoc

func NewDoc(router http.Handler) *Doc

NewDoc returns a documentation generator for the given router

Example
package main

import (
	"fmt"
	"net/http"
	"os"

	"github.com/gregoryv/web"
	"github.com/gregoryv/web/apidoc"
)

func someRouter(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, `{"animal": "Goat","age": 10, "friendly": "hell no, not this one"}`)
	}
	if r.Method == "POST" {
		w.WriteHeader(http.StatusCreated)
		fmt.Fprint(w, `{"message": "added"}`)
	}
}

func main() {
	doc := apidoc.NewDoc(http.HandlerFunc(someRouter))

	body := web.Body(
		doc.NewRequest("GET", "/", nil),
		doc.JsonResponse(),
	)
	out := web.NewHtmlEncoder(os.Stdout)
	out.Encode(body)
}
Output:
<body>
<pre class="request">HTTP/1.1 GET /
</pre><pre class="response">HTTP/1.1 200 OK

{
    "animal": "Goat",
    "age": 10,
    "friendly": "hell no, not this one"
}</pre></body>

func (*Doc) JsonResponse

func (d *Doc) JsonResponse() *Element

func (*Doc) NewRequest

func (d *Doc) NewRequest(method, path string, body io.Reader) *Element

func (*Doc) Response

func (d *Doc) Response() *Element

func (*Doc) Use

func (d *Doc) Use(r *http.Request) *Element

Jump to

Keyboard shortcuts

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