webdoc

package module
v0.0.0-...-8944624 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2016 License: MIT Imports: 6 Imported by: 0

README

webdoc

An goji Mux wrapper that allows you to add documentation to routes.

Please note to use go get goji.io

This package is an simple wrapper around goji's mux struct, adding functionality to document the API routes.

**Please keep in mind that this package in still under development

Example

import (
    "fmt"
    "net/http"
    
    "goji.io"
    "goji.io/pat"

    "github.com/donseba/webdoc"
)

var Doc map[string]webdoc.Info


//hello will say hello to :name
func hello(ctx context.Context, w http.ResponseWriter, r *http.Request) {
    name := pat.Param(ctx, "name")
    fmt.Fprintf(w, "Hello, %s!", name)
}

//routes will output an json of routes
func routes(c context.Context, w http.ResponseWriter, r *http.Request) {
    out, err := json.Marshal(core.DocMap)
    if err != nil {
        panic(err)
    }

    fmt.Fprint(w, out)
}

func main() {
    // instead of web.New()
    wd := webdoc.New()
    
    wd.Get("/hello/:name", hello, webdoc.Doc{Title: "Say hello", Description: "Say hello to :name"})
    wd.Get("/routes", routes, webdoc.Doc{Title: "API Routes", Description: "Retrieve the list of API Routes in JSON format"})
    
    // Assing doc to an global variable
    Doc = wd.DocMap

    mux := goji.NewMux()
    mux.Handle(pat.New("/*"), wd.Mux())
        
    http.ListenAndServe("localhost:8080", mux)
}

Running the example and pointing the browser to /routes

Would return the following :

{
	"/": {
		"methods": {
			"GET": {
				"title": "Index",
				"description": "Index Page"
			}
		}
	},
	"/hello": {
		"routes": {
			"/:name": {
				"methods": {
					"GET": {
						"documentation": {
							"title": "Say Hello",
							"description": "Say hello to :name"
						}
					},
					"PUT": {
						"documentation": {
							"title": "Put some data",
							"description": "Say hello to :name"
						}
					}
				}
			}
		}
	},
	"/routes": {
		"methods": {
			"GET": {
				"Title": "API Routes",
				"Description": "Retrieve the list of API Routes in JSON format"
			}
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Doc

type Doc struct {
	Title       string            `json:"title,omitempty"`
	Description string            `json:"description,omitempty"`
	In          interface{}       `json:"in,omitempty"`
	Out         interface{}       `json:"out,omitempty"`
	FormValue   map[string]string `json:"form_value,omitempty"`
	URLParams   map[string]string `json:"url_params,omitempty"`
}

type Info

type Info struct {
	Routes  map[string]Info `json:"routes,omitempty"`
	Methods map[string]*Doc `json:"methods,omitempty"`
}

type Mux

type Mux struct {
	DocMap map[string]Info // Doc Map
	// contains filtered or unexported fields
}

func New

func New() *Mux

New creates a new webdoc.Mux

func NewSub

func NewSub() *Mux

NewSub creates a new goji.SubMux()

func (*Mux) Delete

func (m *Mux) Delete(pattern string, handler goji.Handler, doc *Doc)

func (*Mux) Get

func (m *Mux) Get(pattern string, handler goji.Handler, doc *Doc)

func (*Mux) Handle

func (m *Mux) Handle(pattern string, submux *Mux)

func (*Mux) Head

func (m *Mux) Head(pattern string, handler goji.Handler, doc *Doc)

func (*Mux) Mux

func (m *Mux) Mux() *goji.Mux

Mux returns the goji's *web.Mux

func (*Mux) Options

func (m *Mux) Options(pattern string, handler goji.Handler, doc *Doc)

func (*Mux) Patch

func (m *Mux) Patch(pattern string, handler goji.Handler, doc *Doc)

func (*Mux) Post

func (m *Mux) Post(pattern string, handler goji.Handler, doc *Doc)

func (*Mux) Put

func (m *Mux) Put(pattern string, handler goji.Handler, doc *Doc)

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Mux) ServeHTTPC

func (m *Mux) ServeHTTPC(c context.Context, w http.ResponseWriter, r *http.Request)

func (*Mux) Use

func (m *Mux) Use(middleware func(http.Handler) http.Handler)

func (*Mux) UseC

func (m *Mux) UseC(middleware func(goji.Handler) goji.Handler)

Jump to

Keyboard shortcuts

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