httprouter

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2019 License: MIT, MIT Imports: 4 Imported by: 0

README

Httprouter httprouter 路由

基于 github.com/julienschmidt/httprouter 实现的路由

使用方式

Router:=httprouter.New()

//响应GET请求
Router.GET("/getpath").
    Use(getmiddlewares...).
    HandlerFunc(getaction)

//响应POST请求
Router.POST("/postpath").
    Use(postmiddlewares...).
    HandlerFunc(poataction)

//响应PUT请求
Router.PUT("/putpath").
    Use(putmiddlewares...).
    HandlerFunc(putaction)

//响应DELETE请求
Router.DELETE("/deletepath").
    Use(deletemiddlewares...).
    HandlerFunc(deleteaction)

//响应PATCH请求
Router.PATCH("/patchpath").
    Use(patchmiddlewares...).
    HandlerFunc(patchaction)

//响应OPTIONS请求
Router.OPTIONS("/optionspath").
    Use(optionsmiddlewares...).
    HandlerFunc(optionsaction)

//响应HEAD请求
Router.HEAD("/deletepath").
    Use(headmiddlewares...).
    HandlerFunc(headaction)

//响应所有请求
Router.ALL("/allpath").
    Use(allmiddlewares...).
    HandlerFunc(allaction)

//子路由
subrouter:=httprouter.New()
//被截取的参数会保存在路由参数的filepath里
Router.StripPrefix("/sub).
     Handler(subrouter)

//获取路径参数
Router.GET("/get/:id").
    HandleFunc(func(w http.ResponseWriter, r *http.Request){
        params=router.GetParams(r)
        id:=params.Get("id)
    })

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetParams

func GetParams(r *http.Request) *router.Params

GetParams get router params from request.

func SetParams

func SetParams(r *http.Request, params httprouter.Params)

SetParams set router to request.

Types

type Router

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

Router router main struct.

func New

func New() *Router

New create new router.

func (*Router) ALL

func (r *Router) ALL(path string) *middleware.App

ALL return app which will response to all method and path.

func (*Router) DELETE

func (r *Router) DELETE(path string) *middleware.App

DELETE return app which will response to DELETE method and path.

func (*Router) GET

func (r *Router) GET(path string) *middleware.App

GET return app which will response to GET method and path.

func (*Router) HEAD

func (r *Router) HEAD(path string) *middleware.App

HEAD return app which will response to HEAD method and path.

func (*Router) Handle

func (r *Router) Handle(method, path string) *middleware.App

Handle return app which will response to given method and path.

func (*Router) OPTIONS

func (r *Router) OPTIONS(path string) *middleware.App

OPTIONS return app which will response to HEAD method and path. Request called to path which any handle by OPTIONS method will return 404 instead of 405 error due to httprouter.

func (*Router) PATCH

func (r *Router) PATCH(path string) *middleware.App

PATCH return app which will response to PATCH method and path.

func (*Router) POST

func (r *Router) POST(path string) *middleware.App

POST return app which will response to POST method and path.

func (*Router) PUT

func (r *Router) PUT(path string) *middleware.App

PUT return app which will response to PUT method and path.

func (*Router) ServeHTTP

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

ServeHTTP serve router as http.handler.

func (*Router) StripPrefix

func (r *Router) StripPrefix(path string) *middleware.App

StripPrefix strip request prefix and server as a middleware app

Jump to

Keyboard shortcuts

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