router

package
v0.0.0-...-b3b2e96 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2014 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

go_router is a simple rest based router. The supported HTTP methods are GET, POST and DELETE. The url path has to be in the form `/version/resource/handler/param-name/param-value`.

Json is the supported response type. It also supports the use of filters for pre and post dispatch process.

@author: avarghese

Index

Constants

View Source
const (
	JSON = "application/json"
)

Variables

This section is empty.

Functions

func Dispatch

func Dispatch(w http.ResponseWriter, r *http.Request)

Dispatch a Request. Only supports json responses.

Usage:

    http.HandleFunc("/", router.Dispatch)
    http.ListenAndServe(":8080", nil)

func RegisterFilter

func RegisterFilter(name string, f Filter) error

Register a filter

Usage:

    go_router.RegisterFilte("filter", test_filter)

func RegisterRoute

func RegisterRoute(method string, path string, n Node) error

Register a route. Parameters required are http method, url path and a controller.

Usage:

    go_router.RegisterRoute(GET, "/v1/test/retrieve", test_controller.Retrieve)
    go_router.RegisterRoute(POST, "/v1/test/save", test_controller.Save)

Types

type Filter

type Filter interface {
	Name() string
	PreDispatch(*http.Request, Request) error
	PostDispatch(*http.Request, Request) error
}

Filters allow for pre and post dispatch work. For example verifying api key.

type Node

type Node interface{}

Node is a controller function. The function should have a pointer to all required request parameters. Returns an interface and an error. Example:

type Test struct {
    Id int64
}
func GetUser(t *Test) (string, error) {
    fmt.Println(t)
    return "user", nil
}

type Request

type Request map[string]*RequestParam

type RequestParam

type RequestParam struct {
	Value interface{}
}

Jump to

Keyboard shortcuts

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