eweb

package module
v0.0.0-...-3dc7bed Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2022 License: MIT Imports: 24 Imported by: 0

README

eweb

Documentation

Index

Constants

View Source
const (
	MIMEJSON  = "application/json; charset=utf-8"
	MIMEPlain = "text/plain; charset=utf-8"
	MIMEHTML  = "text/html; charset=utf-8"
)
View Source
const (
	DevMode  = "Dev"
	ProdMode = "Prod"
	TestMode = "Test"
)
View Source
const AuthUserKey = "user"

AuthUserKey is the cookie name for user credential in basic auth.

View Source
const EnvMode = "EWEB_MODE"

Variables

View Source
var DevPrintRouteFunc func(httpMethod, absPath string, handlerName string, num int)

Functions

func DELETE

func DELETE(handlers ...HandleFunc) methodOption

func EWEBLogger

func EWEBLogger(log *elog.Log)

func GET

func GET(handlers ...HandleFunc) methodOption
func HEAD(handlers ...HandleFunc) methodOption

func Handle

func Handle(method string, handlers ...HandleFunc) methodOption

func MethodColor

func MethodColor(method string) string

func Mode

func Mode() string

func OPTIONS

func OPTIONS(handlers ...HandleFunc) methodOption

func PATCH

func PATCH(handlers ...HandleFunc) methodOption

func POST

func POST(handlers ...HandleFunc) methodOption

func PUT

func PUT(handlers ...HandleFunc) methodOption

func ResetColor

func ResetColor() string

func SetMode

func SetMode(value string)

func SkipPath

func SkipPath(paths ...string)

func StatusCodeColor

func StatusCodeColor(code int) string

func Use

func Use(handlers ...HandleFunc) methodOption

Types

type Accounts

type Accounts map[string]string

Accounts defines a key/value for user/pass list of authorization logins.

type Context

type Context struct {
	Writer ResponseWriter
	Req    *http.Request

	// request info
	Path   string
	Params Params

	Keys map[string]interface{}
	// contains filtered or unexported fields
}

func (*Context) Abort

func (c *Context) Abort()

func (*Context) AbortWithStatus

func (c *Context) AbortWithStatus(code int)

func (*Context) AddHeader

func (c *Context) AddHeader(key, value string)

func (*Context) ContentType

func (c *Context) ContentType() string

func (*Context) Cookie

func (c *Context) Cookie(name string) (string, error)

func (*Context) Copy

func (c *Context) Copy() *Context

func (*Context) Data

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

func (*Context) DefaultPostForm

func (c *Context) DefaultPostForm(key, defaultValue string) string

func (*Context) DefaultQuery

func (c *Context) DefaultQuery(key, defaultValue string) string

func (*Context) DelHeader

func (c *Context) DelHeader(key string)

func (*Context) Fail

func (c *Context) Fail(code int, errStr string)

func (*Context) File

func (c *Context) File(path string)

响应客户端一个文件

func (*Context) FileAttachment

func (c *Context) FileAttachment(path, name string)

func (*Context) FormFile

func (c *Context) FormFile(name string) (*multipart.FileHeader, error)

func (*Context) Forward

func (c *Context) Forward(location string)

func (*Context) Get

func (c *Context) Get(key string) (value interface{}, ok bool)

func (*Context) GetB

func (c *Context) GetB(key string) (s bool)

func (*Context) GetDuration

func (c *Context) GetDuration(key string) (d time.Duration)

func (*Context) GetF64

func (c *Context) GetF64(key string) (f float64)

func (*Context) GetI

func (c *Context) GetI(key string) (i int)

func (*Context) GetI64

func (c *Context) GetI64(key string) (i int64)

func (*Context) GetMSSliceS

func (c *Context) GetMSSliceS(key string) (m map[string][]string)

func (*Context) GetMapI

func (c *Context) GetMapI(key string) (s map[int]interface{})

func (*Context) GetMapS

func (c *Context) GetMapS(key string) (m map[string]interface{})

func (*Context) GetMapSS

func (c *Context) GetMapSS(key string) (m map[string]string)

func (*Context) GetPostForm

func (c *Context) GetPostForm(key string) (string, bool)

func (*Context) GetPostFormArray

func (c *Context) GetPostFormArray(key string) ([]string, bool)

func (*Context) GetQuery

func (c *Context) GetQuery(key string) (string, bool)

func (*Context) GetQueryArray

func (c *Context) GetQueryArray(key string) ([]string, bool)

func (*Context) GetS

func (c *Context) GetS(key string) (s string)

func (*Context) GetSliceS

func (c *Context) GetSliceS(key string) (s []string)

func (*Context) GetTime

func (c *Context) GetTime(key string) (t time.Time)

func (*Context) GetU

func (c *Context) GetU(key string) (u uint)

func (*Context) GetU64

func (c *Context) GetU64(key string) (u uint64)

func (*Context) HTML

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

func (*Context) Header

func (c *Context) Header(key string) string

func (*Context) HeaderKeys

func (c *Context) HeaderKeys() (keys []string)

func (*Context) Headers

func (c *Context) Headers() http.Header

func (*Context) IsAbort

func (c *Context) IsAbort() bool

衍生执行流控制方法

func (*Context) JSON

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

func (*Context) MultipartForm

func (c *Context) MultipartForm() (*multipart.Form, error)

multipart

func (*Context) MustGet

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

func (*Context) Next

func (c *Context) Next()

基本执行流控制方法

func (*Context) Param

func (c *Context) Param(key string) string

Param is called to take the value of path param

func (*Context) PostForm

func (c *Context) PostForm(key string) string

func (*Context) PostFormArray

func (c *Context) PostFormArray(key string) []string

func (*Context) Query

func (c *Context) Query(key string) string

func (*Context) QueryArray

func (c *Context) QueryArray(key string) []string

func (*Context) Redirect

func (c *Context) Redirect(code int, location string)

重定向

func (*Context) Render

func (c *Context) Render(code int, r render.Render)

func (*Context) SaveUploadedFile

func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error

func (*Context) Set

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

执行流中的数据交互

func (*Context) SetCookie

func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)

Cookie 处理

func (*Context) SetHeader

func (c *Context) SetHeader(key, value string)

func (*Context) Status

func (c *Context) Status(code int)

func (*Context) String

func (c *Context) String(code int, format string, values ...interface{})

func (*Context) Success

func (c *Context) Success(msg string)

type Engine

type Engine struct {
	*RouterGroup

	// 默认为 false,如果为 true,则使用 url.RawPath 查找参数
	UseRawPath bool

	// 如果为 true,则 path 会被解码。
	// 如果 UseRawPath 为 false,则 UnescapePathValues 为 true,url.Path 会被拿去查找参数
	// 如果 UseRawPath 为 true,则 UnescapePathValues 为 false,url.RawPath 会被拿去查找参数
	UnescapePathValues bool

	// 如果当前路由无法匹配,但存在带有(不带)结尾斜杠的路径的 handlerFunc,则启用自动重定向。
	// 例如,如果请求了/foo/但路由只存在于/foo中,客户端将被重定向到/foo。
	// GET请求的状态码将被修改为 301,所有其他请求方法的状态码将被修改为 307。
	RedirectTrailingSlash bool

	RemoveExtraSlash bool

	MaxMultipartMemory int64

	HandleMethodNotAllowed bool
	// contains filtered or unexported fields
}

func Default

func Default() *Engine

func New

func New() *Engine

New 返回一个引擎对象,引擎对象中管理着一片`方法树林`和一棵`分组树` 方法树林最多有9棵方法树,对应了 RESTFul API 的9种方法 分组树仅有一棵,以引擎为根结点。 New returns an Engine object, which manages a `method forest` and a `grouping tree`. The method forest has a maximum of 9 method trees, corresponding to the 9 RESTFul API methods, and only one grouping tree, with the engine as the root node.

func (*Engine) HandleContext

func (engine *Engine) HandleContext(c *Context)

func (*Engine) LoadHTMLGlob

func (engine *Engine) LoadHTMLGlob(pattern string)

func (*Engine) NoMethod

func (engine *Engine) NoMethod(handlers ...HandleFunc)

func (*Engine) NoRoute

func (engine *Engine) NoRoute(handlers ...HandleFunc)

404 相关

func (*Engine) Run

func (engine *Engine) Run(addr ...string) (err error)

Run 用于启动http服务,需要接收一个端口号 如果没有传入端口号,默认使用8080端口 Run is used to start the HTTP service and needs a port number. The port 8080 is used by default if no port number is passed in.

func (*Engine) ServeHTTP

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

func (*Engine) SetFuncMap

func (engine *Engine) SetFuncMap(funcMap template.FuncMap)

模板相关

func (*Engine) Use

func (engine *Engine) Use(middleware ...HandleFunc) IRoutes

type H

type H map[string]interface{}

type HandleChain

type HandleChain []HandleFunc

HandleChain defines a Array of HandlerFunc.

func (HandleChain) Last

func (c HandleChain) Last() HandleFunc

Last return the last handler in the handler chain.

type HandleFunc

type HandleFunc func(*Context)

HandleFunc defines the handler used by middlerware or route.

func BasicAuth

func BasicAuth(account Accounts) HandleFunc

func BasicAuthForRealm

func BasicAuthForRealm(account Accounts, realm string) HandleFunc

func CORS

func CORS(origin ...string) HandleFunc

CORS returns a middleware that sets the cross-domain response header.

func Logger

func Logger() HandleFunc

func LoggerWithWriter

func LoggerWithWriter(w io.Writer) HandleFunc

func Recovery

func Recovery() HandleFunc

type IRouter

type IRouter interface {
	IRoutes
	Group(string, ...HandleFunc) *RouterGroup
}

type IRoutes

type IRoutes interface {
	Use(...HandleFunc) IRoutes
	Handle(string, string, ...HandleFunc) IRoutes
	GET(string, ...HandleFunc) IRoutes
	POST(string, ...HandleFunc) IRoutes
	PUT(string, ...HandleFunc) IRoutes
	DELETE(string, ...HandleFunc) IRoutes
	OPTIONS(string, ...HandleFunc) IRoutes
	HEAD(string, ...HandleFunc) IRoutes
	PATCH(string, ...HandleFunc) IRoutes
}

type Param

type Param struct {
	Key   string
	Value string
}

type Params

type Params []Param

func (Params) ByName

func (ps Params) ByName(name string) (va string)

func (Params) Get

func (ps Params) Get(key string) (string, bool)

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher
	http.Hijacker
	http.CloseNotifier

	Status() int

	Size() int

	Written() bool

	WriteString(string) (int, error)

	WriteHeaderNow()

	Pusher() http.Pusher
}

type RouterGroup

type RouterGroup struct {
	Middleware HandleChain // 附属于该分组的中间件
	// contains filtered or unexported fields
}

func (*RouterGroup) DELETE

func (g *RouterGroup) DELETE(path string, handlers ...HandleFunc) IRoutes

func (*RouterGroup) GET

func (g *RouterGroup) GET(path string, handlers ...HandleFunc) IRoutes

func (*RouterGroup) Group

func (g *RouterGroup) Group(prefix string, handlers ...HandleFunc) *RouterGroup

func (*RouterGroup) HEAD

func (g *RouterGroup) HEAD(path string, handlers ...HandleFunc) IRoutes

func (*RouterGroup) Handle

func (g *RouterGroup) Handle(method, relPath string, handlers ...HandleFunc) IRoutes

func (*RouterGroup) OPTIONS

func (g *RouterGroup) OPTIONS(path string, handlers ...HandleFunc) IRoutes

func (*RouterGroup) PATCH

func (g *RouterGroup) PATCH(path string, handlers ...HandleFunc) IRoutes

func (*RouterGroup) POST

func (g *RouterGroup) POST(path string, handlers ...HandleFunc) IRoutes

func (*RouterGroup) PUT

func (g *RouterGroup) PUT(path string, handlers ...HandleFunc) IRoutes

func (*RouterGroup) Route

func (g *RouterGroup) Route(path string, options ...methodOption) IRoutes

Route is a options design pattern style interface. It provides a shortcut to many handlers which is same path but different request method.

example:

g.Route("/login",
	Use(Logger),
	GET(indexHandler),
	POST(loginHandler),
)

equals:

g.Use(Logger)
g.GET("/login", indexHandler)
g.POST("/login", loginHandler)

Attention! if you use the Method Use(), it will register the middlerware on group instead of route, just like the example, that will act on all the group's routes. You can write like `GET(indexHandler, Logger)` when you wanna act on special route.

func (*RouterGroup) Static

func (group *RouterGroup) Static(relPath string, root string)

serve static files

func (*RouterGroup) Use

func (group *RouterGroup) Use(middleware ...HandleFunc) IRoutes

===================== === IRoutes 方法集 === =====================

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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