router

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorHandlerFunc = func(ctx *Context, ex *exception.Exception) {
	ctx.Response.WriteHeader(ex.Code)
	ctx.Request.Header.Set("Content-Type", "text/plain; charset=utf-8")
	ctx.Request.Header.Set("X-Content-Type-Options", "nosniff")
	_, err := ctx.Response.Write(util.StringToByteArray(ex.Error()))
	if err != nil {
		ctx.Logger.Warn(err.Error())
	}
	ctx.Response.Flush()
}

错误处理方法

View Source
var NotFoundHandlerFunc = func(ctx *Context) {
	ctx.Response.WriteHeader(http.StatusNotFound)
	ctx.Request.Header.Set("Content-Type", "text/plain; charset=utf-8")
	ctx.Request.Header.Set("X-Content-Type-Options", "nosniff")
	_, err := ctx.Response.Write(util.StringToByteArray(http.StatusText(http.StatusNotFound)))
	if err != nil {
		ctx.Logger.Warn(err.Error())
	}
	ctx.Response.Flush()
}

404处理方法

Functions

func Delete

func Delete(path string, fun RouterHandlerFunc)

func Get

func Get(path string, fun RouterHandlerFunc)
func Head(path string, fun RouterHandlerFunc)

func Options

func Options(path string, fun RouterHandlerFunc)

func Patch

func Patch(path string, fun RouterHandlerFunc)

func Post

func Post(path string, fun RouterHandlerFunc)

func Put

func Put(path string, fun RouterHandlerFunc)

func Trace

func Trace(path string, fun RouterHandlerFunc)

func WebSocket

func WebSocket(path string, fun RouterHandlerFunc)

websocket 接口

Types

type Context

type Context struct {
	Request   *http.Request
	Response  *Response
	Websocket *websocket.Conn
	Store     map[string]interface{}
	Logger    *zap.Logger
	Path      string
	Method    string
}

请求上下文

func NewContext

func NewContext(r *http.Request, w http.ResponseWriter, logger *zap.Logger) *Context

type Response

type Response struct {
	Writer    http.ResponseWriter
	Status    int
	Size      int64
	Committed bool
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse(w http.ResponseWriter, logger *zap.Logger) (r *Response)

func (*Response) Flush

func (r *Response) Flush()

func (*Response) Header

func (r *Response) Header() http.Header

func (*Response) Write

func (r *Response) Write(b []byte) (n int, err error)

func (*Response) WriteHeader

func (r *Response) WriteHeader(code int)

type RouterHandlerFunc

type RouterHandlerFunc func(*Context) (interface{}, *exception.Exception)

func Wrapper

func Wrapper(path string) RouterHandlerFunc

Jump to

Keyboard shortcuts

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