az

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

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

Go to latest
Published: Jun 10, 2022 License: MIT Imports: 8 Imported by: 0

README

AZ http router with middleware and self documentation

It is a lightweight high performance HTTP request router for Go.

In contrast to other routers it don`t have pattern matching and all parameters is passed by url query parameters. It is like function call with arguments via HTTP.

It has extendable middleware system and it is self documenting with GUI

Example:

  • Req: GET /ping
  • Res: "pong"
Router.Route("/ping", Router.Handler(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("pong"))
}))

more complicated:

  • Req: GET /admin/user?name=Ernest //only match get request with parameter name
  • Res: "user name is Ernest"
  • Req: GET /admin/user?hobby=sport //only match get request with parameter hobby
  • Res: "your hobby is sport"
  • Req: POST /admin/user?id=10 //only match post request with parameter id
  • Res: "I done with id=10"
a := API.Router.Namespace("admin")
a.Route("/user",
    Router.Method("get",
        Router.Param("name",
            Router.Handler(func(w http.ResponseWriter, r *http.Request) {
                name := r.URL.Query().Get("name")
		        w.Write([]byte("user name is" + name))
            })
        ),
        Router.Param("hobby",
            Router.Handler(func(w http.ResponseWriter, r *http.Request) {
                name := r.URL.Query().Get("hobby")
		        w.Write([]byte("your hobby is" + hobby))
            })
        )
    ),
    Router.Method("post",
        Router.Param("id",
            Router.Handler(func(w http.ResponseWriter, r *http.Request) {
                id := r.URL.Query().Get("id")
                // do something with id 
		        w.Write([]byte("I done with id=" + id))
            })
        )
    )
)

az az

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Middlewares = struct {
	Logger    middlewareFn
	BasicAuth middlewareFn
	CORS      middlewareFn
}{
	Logger:    logger,
	BasicAuth: basicAuth,
	CORS:      CORS,
}

Default Middlewares

Functions

func CORS

func CORS(h http.HandlerFunc, params ...interface{}) http.HandlerFunc

Types

type CorsParams

type CorsParams struct {
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	ExposedHeaders   []string
	AllowCredentials bool
	MaxAge           int
	Debug            bool
}

type HandlerStruct

type HandlerStruct struct {
	// contains filtered or unexported fields
}

func Handler

func Handler(handlerFunc ...http.HandlerFunc) *HandlerStruct

func (*HandlerStruct) Cookie

func (h *HandlerStruct) Cookie(name string, description string) *HandlerStruct

func (*HandlerStruct) Doc

func (h *HandlerStruct) Doc(name string, description string) *HandlerStruct

func (*HandlerStruct) HandlerFunc

func (h *HandlerStruct) HandlerFunc(handlerFunc http.HandlerFunc) *HandlerStruct

func (*HandlerStruct) Header

func (h *HandlerStruct) Header(name string, description string) *HandlerStruct

func (*HandlerStruct) Param

func (h *HandlerStruct) Param(name string, paramType string, description string) *HandlerStruct

func (*HandlerStruct) Use

func (h *HandlerStruct) Use(mfn middlewareFn, params ...interface{}) *HandlerStruct

type MethodStruct

type MethodStruct struct {
	MethodStruct string

	*HandlerStruct
	// contains filtered or unexported fields
}

func (*MethodStruct) Use

func (me *MethodStruct) Use(mfn middlewareFn, params ...interface{}) *MethodStruct

type ParamStruct

type ParamStruct struct {
	*HandlerStruct
	// contains filtered or unexported fields
}

func (*ParamStruct) Use

func (p *ParamStruct) Use(mfn middlewareFn, params ...interface{}) *ParamStruct

type RouteStruct

type RouteStruct struct {
	*HandlerStruct
	// contains filtered or unexported fields
}

func (*RouteStruct) Use

func (ro *RouteStruct) Use(mfn middlewareFn, params ...interface{}) *RouteStruct

type Router

type Router struct {
	DefaultHandler http.HandlerFunc // default HandlerStruct
	CaseSensitive  bool
	// contains filtered or unexported fields
}

Router ...

func New

func New() *Router

New creates a new Router.

func (*Router) Documentation

func (r *Router) Documentation(name string, description string) []byte

func (*Router) Handler

func (*Router) Handler(handlerFunc ...http.HandlerFunc) *HandlerStruct

func (*Router) Method

func (*Router) Method(methodName string, handlerOrParam ...interface{}) *MethodStruct

func (*Router) Namespace

func (r *Router) Namespace(namespace string) *Router

func (*Router) Param

func (r *Router) Param(handlerOrParamName ...interface{}) *ParamStruct

func (*Router) Route

func (r *Router) Route(path string, handlerOrMethod ...interface{}) *RouteStruct

Add RouteStruct.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Router) Use

func (r *Router) Use(mfn middlewareFn, params ...interface{}) *Router

Jump to

Keyboard shortcuts

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