engine

package module
v0.0.0-...-50f78d8 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2019 License: MIT Imports: 16 Imported by: 0

README

engine

通过临摹gin, 实现一个简单的web框架

Documentation

Index

Constants

View Source
const (
	AbortIndex = math.MaxInt8 / 2
)

Variables

This section is empty.

Functions

func Validate

func Validate(c *Context, obj interface{}) error

Types

type Context

type Context struct {
	Req    *http.Request
	Writer http.ResponseWriter
	Keys   map[string]interface{}
	Errors ErrorMsgs
	Params httprouter.Params
	// contains filtered or unexported fields
}

context is the most important part of engine. it allow us to pass variables between middleware, manage the flow, validate the JSON of a request and render a JSON response for example.

func (*Context) Abort

func (c *Context) Abort(code int)

Forces the system to do not continue calling the pending handlers. For example, the first handler checks if the request is authorized. If it's not , context.Abort(401) shold be called. The rest of pending handlers would never be called for that request.

func (*Context) Data

func (c *Context) Data(code int, data []byte)

Writes some data into the body stream and updates status code

func (*Context) EnsureBody

func (c *Context) EnsureBody(item interface{}) bool

Like ParseBody() but this method also writes a 400 error if the json is not valid.

func (*Context) Error

func (c *Context) Error(err error, meta interface{})

Attaches an error to the current context. the error is pushed to a list of errors. It's a good idea to call Error for each error occurred during the resolution of a request. A middleware can be used to collect all the errors and push them to a database together, print a log, or append it in the HTTP response.

func (*Context) ErrorLogger

func (c *Context) ErrorLogger() HandlerFunc

func (*Context) Fail

func (c *Context) Fail(code int, err error)

Fail is the same than Abort plus an error message. Calling `context.Fail(500, err)` is equivalent to: ``` context.Error("Operation aborted", err) context.Abort(500) ```

func (*Context) Get

func (c *Context) Get(key string) interface{}

Returns the value for the given key. It panics if the value doesn't dexist.

func (*Context) HTML

func (c *Context) HTML(code int, name string, data interface{})

Renders the html template specified by his file name. It also update the http code and set the Content-Type as "application/html"

func (*Context) JSON

func (c *Context) JSON(code int, obj interface{})

Serializes the given struct as a JSON into the response body in a fast and efficient way. It also sets the Content-Type as "application/json"

func (*Context) Next

func (c *Context) Next()

Next should be used only in the middleware. It executes the pending handlers in the chain inside the calling handler.

func (*Context) ParseBody

func (c *Context) ParseBody(item interface{}) error

Parses the body content as a JSON input. It decodes the json payload into the struct specified as a pointer.

func (*Context) Set

func (c *Context) Set(key string, value interface{})

Sets a new pair key/value just for the specified context It also lazy initializes the hashmap

func (*Context) String

func (c *Context) String(code int, msg string)

Writes the given string into the response body and set the Content-Type to "application/plain"

func (*Context) XML

func (c *Context) XML(code int, obj interface{})

Serializes the given struct as XML into the response body in a fast and efficient way It also sets the Content-Type as "application/json"

type Engine

type Engine struct {
	*RouterGroup

	HTMLTemplates *template.Template
	// contains filtered or unexported fields
}

Represents the web framework, it wrappers the blazing fast httprouter multiplexer and a list of global middleware

func Default

func Default() *Engine

Return a Engine instance with the Logger and Recover middleware

func New

func New() *Engine

Return a new Blank Engine without any middleware attached the most basic configuration

func (*Engine) LoadHTMLTemplate

func (engine *Engine) LoadHTMLTemplate(pattern string)

func (*Engine) NotFound404

func (engine *Engine) NotFound404(handlers ...HandlerFunc)

Add handlers for NotFound, It return 404 code by default

func (*Engine) Run

func (engine *Engine) Run(addr string)

func (*Engine) ServeHTTP

func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHttp makes the router implement the http.Handler interface

type ErrorMsg

type ErrorMsg struct {
	Err  string      `json:"error"`
	Meta interface{} `json:"meta"`
}

used internally to collect a error occurred during a http request

type ErrorMsgs

type ErrorMsgs []ErrorMsg

func (ErrorMsgs) String

func (e ErrorMsgs) String() string

type H

type H map[string]interface{}

type HandlerFunc

type HandlerFunc = func(*Context)

func Logger

func Logger() HandlerFunc

func Recovery

func Recovery() HandlerFunc

Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.

type RouterGroup

type RouterGroup struct {
	Handlers []HandlerFunc
	// contains filtered or unexported fields
}

used internally to configure router, a RouterGroup is associated with a prefix and an array of handlers(middleware)

func (*RouterGroup) DELETE

func (group *RouterGroup) DELETE(path string, handlers ...HandlerFunc)

DELETE is a shortcut for router.Handle("DELETE", path, handle)

func (*RouterGroup) GET

func (group *RouterGroup) GET(path string, handlers ...HandlerFunc)

GET is a shortcut for router.Handle("GET", path, handle)

func (*RouterGroup) Group

func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *RouterGroup

Creates a new router group. You should create add all the routes that share that have common middleware or same path prefix. For example, all the routes that use a common middleware for authorization could be grouped.

func (*RouterGroup) Handle

func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc)

Handle registers a new request handler and middleware with the given path and method. The laster handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*RouterGroup) PATCH

func (group *RouterGroup) PATCH(path string, handlers ...HandlerFunc)

PATCH is a shortcut for router.Handle("PATCH", path, handle)

func (*RouterGroup) POST

func (group *RouterGroup) POST(p string, handlers ...HandlerFunc)

POST is the shortcut for router.Handle("POST", path, handle)

func (*RouterGroup) PUT

func (group *RouterGroup) PUT(path string, handlers ...HandlerFunc)

PUT is a shortcut for router.Handle("PUT", path, handle)

func (*RouterGroup) Use

func (group *RouterGroup) Use(middleware ...HandlerFunc)

Adds middleware to the group

Jump to

Keyboard shortcuts

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