pepper

package module
v0.6.5-beta Latest Latest
Warning

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

Go to latest
Published: May 1, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

README

关于Pepper

Pepper 是用go语言编写的轻量的HTTP框架

开始使用

Documentation

Index

Constants

View Source
const (
	METHOD_ALL     = "ALL"
	METHOD_GET     = "GET"
	METHOD_POST    = "POST"
	METHOD_HEAD    = "HEAD"
	METHOD_PUT     = "PUT"
	METHOD_DELETE  = "DELETE"
	METHOD_CONNECT = "CONNECT"
	METHOD_OPTIONS = "OPTIONS"
	METHOD_TRACE   = "TRACE"
	METHOD_PATCH   = "PATCH"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorPages

type ErrorPages struct {
	Static struct {
		NotFound            string
		Forbidden           string
		InternalServerError string
		Other               map[int]string
	}
	NotFound            HandlerFunc
	Forbidden           HandlerFunc
	InternalServerError HandlerFunc
	Other               map[int]HandlerFunc
}

func NewErrorPages

func NewErrorPages() *ErrorPages

func (*ErrorPages) SendPage

func (e *ErrorPages) SendPage(code int, res Response, req *Request) error

func (*ErrorPages) Set

func (e *ErrorPages) Set(code int, file string)

func (*ErrorPages) SetHandler

func (e *ErrorPages) SetHandler(code int, handler HandlerFunc)

type FuncMap

type FuncMap map[string]interface{}

type Group

type Group struct {
	Trie *TrieNode
}

func NewGroup

func NewGroup() *Group

func (*Group) All

func (g *Group) All(u string, h HandlerFunc)

快捷创建请求处理器

func (*Group) Get

func (g *Group) Get(u string, h HandlerFunc)

func (*Group) NewHandler

func (g *Group) NewHandler(method string, uri string, handler HandlerFunc)

创建对应请求处理器

func (*Group) Post

func (g *Group) Post(u string, h HandlerFunc)

func (*Group) Use

func (g *Group) Use(mw ...MiddlewareFunc)

func (*Group) UseGroup

func (g *Group) UseGroup(uri string, group *Group)

type HandlerFunc

type HandlerFunc func(res Response, req *Request)

处理函数

type HandlerPool

type HandlerPool struct {
	All     *HandlerFunc
	Get     *HandlerFunc
	Post    *HandlerFunc
	Head    *HandlerFunc
	Put     *HandlerFunc
	Delete  *HandlerFunc
	Connect *HandlerFunc
	Options *HandlerFunc
	Trace   *HandlerFunc
	Patch   *HandlerFunc
}

处理程序池

func (*HandlerPool) GetHandlerFunc

func (h *HandlerPool) GetHandlerFunc(method string) *HandlerFunc

获取对应处理函数

func (*HandlerPool) NewHandler

func (h *HandlerPool) NewHandler(method string, handler HandlerFunc)

创建一种请求方式的处理器

type MiddlewareFunc

type MiddlewareFunc func(p *Pepper, res Response, req *Request) bool

type Pepper

type Pepper struct {
	// 调试模式
	DebugMode bool

	KeyFile string // 密钥文件路径 填写后会自动开启 HTTPS
	CrtFile string // 证书文件路径 填写后会自动开启 HTTPS

	// 错误页
	ErrorPages *ErrorPages
	// contains filtered or unexported fields
}

func NewPepper

func NewPepper() *Pepper

创建 Pepper 对象

func (*Pepper) All

func (p *Pepper) All(u string, h HandlerFunc)

快捷创建请求处理器

func (*Pepper) Get

func (p *Pepper) Get(u string, h HandlerFunc)

func (*Pepper) Listen

func (p *Pepper) Listen(addr string) error

运行服务器

func (*Pepper) NewHandler

func (p *Pepper) NewHandler(method string, uri string, handler HandlerFunc)

创建对应请求处理器

func (*Pepper) Post

func (p *Pepper) Post(u string, h HandlerFunc)

func (*Pepper) Static

func (p *Pepper) Static(uri string, dir string)

设置静态文件

func (*Pepper) Use

func (p *Pepper) Use(mw ...MiddlewareFunc)

func (*Pepper) UseGroup

func (p *Pepper) UseGroup(uri string, group *Group)

type Request

type Request struct {
	Req           *http.Request
	Body          io.ReadCloser
	Method        string
	Path          string
	TrimPath      string
	Cookie        string
	Proto         string
	Host          string
	RemoteAddr    string
	ContentLength int64
	// contains filtered or unexported fields
}

请求信息

func (*Request) GetCookie

func (r *Request) GetCookie(key string) (cookie *http.Cookie, err error)

func (*Request) GetCookieValue

func (r *Request) GetCookieValue(key string) string

func (*Request) GetForm

func (r *Request) GetForm() map[string][]string

func (*Request) GetFormStringValue

func (r *Request) GetFormStringValue(key string) string

func (*Request) GetFormValue

func (r *Request) GetFormValue(key string) ([]string, bool)

func (*Request) GetHeader

func (r *Request) GetHeader(key string) string

func (*Request) GetJson

func (r *Request) GetJson(i interface{}) error

func (*Request) Query

func (r *Request) Query(key string) string

func (*Request) Scan

func (r *Request) Scan(i interface{}) (err error)

type Response

type Response struct {
	Resp       http.ResponseWriter
	ErrorPages *ErrorPages
	Code       int
	// contains filtered or unexported fields
}

响应接口

func (*Response) Json

func (r *Response) Json(v interface{}) (err error)

发送json格式数据

func (*Response) MustJson

func (r *Response) MustJson(code int, v interface{})

不返回错误信息的Json函数

func (*Response) Redirect

func (r *Response) Redirect(url string)

重定向

func (*Response) RemoveCookie

func (r *Response) RemoveCookie(name string)

func (*Response) SendErrorPage

func (r *Response) SendErrorPage(code int) error

发送错误页面并返回错误码

func (*Response) SetCookie

func (r *Response) SetCookie(opt *http.Cookie)

设置cookie

func (*Response) SetHeader

func (r *Response) SetHeader(key, value string)

设置响应头

func (*Response) SetStatusCode

func (r *Response) SetStatusCode(code int)

设置状态码

func (*Response) Template

func (r *Response) Template(file string, tpl interface{}, fc FuncMap) error

封装的 "html/template"

func (*Response) Write

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

发送二进制内容

func (*Response) WriteFile

func (r *Response) WriteFile(file string, bufferSize int) (err error)

发送文件

func (*Response) WriteReader

func (r *Response) WriteReader(reader io.Reader, bufferSize int) error

通过 io.Reader 接口发送数据

func (*Response) WriteString

func (r *Response) WriteString(str string) (int, error)

发送字符串

type TrieNode

type TrieNode struct {
	Trusteeship bool

	// 子节点
	Next map[string]*TrieNode
	// 此节点的上一个节点
	Prev *TrieNode

	Middleware []*MiddlewareFunc
	// contains filtered or unexported fields
}

字典树

func (*TrieNode) CallHandler

func (t *TrieNode) CallHandler(res Response, req *Request)

调用当前节点的对应处理的函数

func (*TrieNode) GetHandlerFunc

func (t *TrieNode) GetHandlerFunc(method string, uri string) *HandlerFunc

获取对应处理函数

func (*TrieNode) Insert

func (t *TrieNode) Insert(method string, uri string, handler *HandlerFunc)

添加处理函数到节点

func (*TrieNode) InsertByGroup

func (t *TrieNode) InsertByGroup(uri string, group *Group)

func (*TrieNode) Search

func (t *TrieNode) Search(uri string) *HandlerPool

通过URI查找树并返回 处理池 (*HandlePool)

func (*TrieNode) SearchNode

func (t *TrieNode) SearchNode(uri string, callback func(*TrieNode)) *TrieNode

通过URI查找字典树 返回对应节点 如果没有找到则返回nil

Directories

Path Synopsis
internal
log

Jump to

Keyboard shortcuts

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