smile

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2020 License: MIT Imports: 19 Imported by: 0

README

超轻量web框架

  • 服务器引擎

    基于go官方http包,进行再封装,支持Gzip压缩

    • 静态文件服务器

      • 可以指定任意资源目录输出目录内文件
      • 自定义默认文件
      • 在Red Hat 4.4.7 1核1G配置下支持5000并发文件请求
    • 动态逻辑处理服务器

      • 支持多种HTTP请求方式(GET、POST、PUT、DELETE)
      • 支持websocket处理
  • 路由配置

    • restful风格路由自动加载
      • 支持以Get|GET|Post|POST|Put|PUT|Delete|DELETE为前缀的HandleFunc自动注册为对应请求方式的资源路径处理器
      • 支持单独注册路径及对应处理器HandleFunc
      • 支持注册404处理方法
      • 支持注册中间件方法
  • 日志

    • 支持终端打印请求日志

Documentation

Overview

Package smile This software is licensed under the MIT License. You can get more info in license file.

Index

Constants

View Source
const (
	ModeDEBUG   = "debug"
	ModeTESTING = "testing"
	ModePRO     = "production"
)

定义了一些模式和HOOK 方便调测

View Source
const (
	MethodGet    = "GET"
	MethodPost   = "POST"
	MethodWs     = "WS"
	MethodPut    = "PUT"
	MethodDelete = "DELETE"
)

定义部分请求类型及其匹配式

View Source
const (
	StyleHump    = "hump"
	StyleConnect = "connector"
)

定义自动生成路由的风格

View Source
const (
	MaxFileSize = 5 << 20
)

默认文件上传大小限制

Variables

View Source
var (
	CustomFileSize int64
)

用户自定上传大小限制

View Source
var DefaultFile = "index.html"

DefaultFile 文件服务器默认输出文件

Functions

func DoCustomInit

func DoCustomInit()

DoCustomInit 显式提供可调用api 调用时机

func GetInitState

func GetInitState() bool

GetInitState 获取初始化操作状态

func InitFuncPush

func InitFuncPush(f InitFunc)

InitFuncPush 将函数注入到框架启动初始化函数列表中

func LogOFF

func LogOFF()

LogOFF 关闭日志

func LogON

func LogON()

LogON 开启日志

func Mode

func Mode() string

Mode 返回当前模式

func SetDEBUG

func SetDEBUG()

SetDEBUG 开发模式

func SetDebugger

func SetDebugger(fnc Debugger)

SetDebugger 由外部注入一个处理error的函数 会替换默认函数

func SetInitGoroutine

func SetInitGoroutine()

SetInitGoroutine 设置初始化时采用协程

func SetInitSync

func SetInitSync()

SetInitSync 设置初始化时不采用协程

func SetPRODUCTION

func SetPRODUCTION()

SetPRODUCTION 生产模式

func SetRecovery

func SetRecovery(fnc Recovery)

SetRecovery 由外部注入一个recover函数 会替换默认函数

func SetTESTING

func SetTESTING()

SetTESTING 测试模式

Types

type Context added in v0.1.2

type Context struct {
	ResponseWriter
	Request *http.Request
	// contains filtered or unexported fields
}

Context 一个复合结构,将writer和 request保存到一起,方便被调用 实现了一些便捷方法 从而缩短获取数据的路径长度

func (*Context) Abort added in v0.1.2

func (c *Context) Abort() bool

Abort 调用中断执行 后续注册函数将不再执行

func (*Context) Close added in v0.1.2

func (c *Context) Close()

Close 请求响应结束后的一些操作

func (*Context) Err added in v0.1.2

func (c *Context) Err() []error

Err 返回context中保存的错误

func (*Context) GetClientIP added in v0.1.2

func (c *Context) GetClientIP() string

GetClientIP 获取请求的IP地址 从请求头中截取

func (*Context) GetCookie added in v0.1.2

func (c *Context) GetCookie(key string) (*http.Cookie, error)

GetCookie 从请求携带的cookie中取值

func (*Context) GetFormFile added in v0.1.2

func (c *Context) GetFormFile(key string) (multipart.File, *multipart.FileHeader, error)

GetFormFile 根据键名获取上传文件

func (*Context) GetHeader added in v0.1.2

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

GetHeader 获取请求的header 返回http.Header

func (*Context) GetHost added in v0.1.2

func (c *Context) GetHost() string

GetHost 获取请求host

func (*Context) GetMethod added in v0.1.2

func (c *Context) GetMethod() string

GetMethod 获取请求的方法 GET/POST

func (*Context) GetMultipartFormFile added in v0.1.2

func (c *Context) GetMultipartFormFile(key string) []*multipart.FileHeader

GetMultipartFormFile 根据键名冲form-data类型中取得上传文件头信息

func (*Context) GetMultipartFormParam added in v0.1.2

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

GetMultipartFormParam 根据键名从form-data类型中取值

func (*Context) GetPath added in v0.1.2

func (c *Context) GetPath() string

GetPath 获取请求的Path

func (*Context) GetPostParam added in v0.1.2

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

GetPostParam 根据键名从post表单中取值

func (*Context) GetProto added in v0.1.2

func (c *Context) GetProto() string

GetProto 获取请求的传输协议 HTTP1.1 / HTTP2

func (*Context) GetQueryParam added in v0.1.2

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

GetQueryParam 根据键名从url参数中取值

func (*Context) GetQueryString added in v0.1.2

func (c *Context) GetQueryString() string

GetQueryString 获取请求的url参数

func (*Context) GetRawBody added in v0.1.2

func (c *Context) GetRawBody() string

GetRawBody 获取请求中的body体 当传输类型为 urlencoded时 可用

func (*Context) GetScheme added in v0.1.2

func (c *Context) GetScheme() string

GetScheme 获取请求Scheme

func (*Context) GetURL added in v0.1.2

func (c *Context) GetURL() string

GetURL 获取请求的URL

func (*Context) GetUserAgent added in v0.1.2

func (c *Context) GetUserAgent() string

GetUserAgent 获取请求的代理头 user-agent

func (*Context) Next added in v0.1.2

func (c *Context) Next() error

Next 执行注册请求

func (*Context) Redirect added in v0.1.2

func (c *Context) Redirect(url string)

Redirect 302跳转到指定地址

func (*Context) SetCookie added in v0.1.2

func (c *Context) SetCookie(cookie *http.Cookie)

SetCookie 设置cookie

func (*Context) SetHeader added in v0.1.2

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

SetHeader 设置header

type Debugger

type Debugger func(*Context, error)

Debugger 定义外部panic处理函数,可用于覆盖默认值

type Engine

type Engine struct {
	RouteGroup *RouteGroup

	Logger ILogger
	Gzip   bool
	//debug
	Errors []error
	// contains filtered or unexported fields
}

Engine 一个服务器引擎

func Default

func Default() *Engine

Default 生成一个默认配置的服务器 有动态引擎和websocket引擎

func NewEngine

func NewEngine(config ...string) *Engine

NewEngine 获取一个具有全部处理引擎的服务器

func (*Engine) GetErrors

func (e *Engine) GetErrors() []error

GetErrors 获取引擎中的错误

func (*Engine) GzipOff

func (e *Engine) GzipOff()

GzipOff 关闭Gzip

func (*Engine) GzipOn

func (e *Engine) GzipOn()

GzipOn 开启Gzip

func (*Engine) Run

func (e *Engine) Run(port string) (err error)

Run 启动一个HttpServer

func (*Engine) RunTLS

func (e *Engine) RunTLS(port, cert, key string) (err error)

RunTLS 启动一个HttpsServer

func (*Engine) ServeHTTP

func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP 有请求的时候 把请求处理以后 储存到结构中

func (*Engine) SetLoger

func (e *Engine) SetLoger(l ILogger)

SetLoger 注册一个logger

func (*Engine) SetRouteGroup

func (e *Engine) SetRouteGroup(r *RouteGroup)

SetRouteGroup 注册一个路由组

type HandlerFunc

type HandlerFunc func(*Context) error

HandlerFunc 定一个业务执行方法

type IEngine

type IEngine interface {
	Init(*Context) IEngine  //初始化引擎
	Handle() error          //执行方法
	Check(interface{}) bool //判断是否属于引擎处理请求
	GetType() string        //获取引擎结构类型
}

IEngine 一个引擎接口

type ILogger

type ILogger interface {
	Write(io.Writer, string) (int, error)
	Log(*Context) error
}

ILogger 日志处理

type InitFunc

type InitFunc func()

InitFunc 初始化函数类型

type Logger

type Logger struct {
	Writer io.Writer
	// contains filtered or unexported fields
}

Logger 实现一个日志结构

func (*Logger) Log

func (l *Logger) Log(c *Context) error

Log 方法 整理需要log的数据拼接后进行输出

func (*Logger) TermOff

func (l *Logger) TermOff()

TermOff 关闭终端输出(数据染色取消)

func (*Logger) TermOn

func (l *Logger) TermOn()

TermOn 开启终端输出(数据染色)

func (*Logger) Write

func (l *Logger) Write(w io.Writer, s string) (int, error)

logger写方法 向一个io内写入数据

type Recovery

type Recovery func(*Context, interface{}) error

Recovery 定义外部panic处理函数,可用于覆盖默认值

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Hijacker
	http.Flusher
	DataSize() int
	Status() int
	WriteString(string) (int, error)
	Done()
}

ResponseWriter 定义一个writer接口 该接口可用于http、weibsocket的响应操作

type RouteGroup

type RouteGroup struct {
	GET    map[string]HandlerFunc
	POST   map[string]HandlerFunc
	WS     map[string]HandlerFunc
	PUT    map[string]HandlerFunc
	DELETE map[string]HandlerFunc
	// contains filtered or unexported fields
}

RouteGroup 路由列表

func NewRouteGroup

func NewRouteGroup() *RouteGroup

NewRouteGroup 生成一个新的路由列表

func (*RouteGroup) FillRoutes

func (rg *RouteGroup) FillRoutes(method string, prefix string, c interface{})

FillRoutes 填充路由基础方法

func (*RouteGroup) FormatRoutes

func (rg *RouteGroup) FormatRoutes() (rsAssign []string, rsAuto []string)

FormatRoutes 返回格式化的路由信息 每个路由信息为一个string

func (*RouteGroup) Get

func (rg *RouteGroup) Get(method string, path string) (HandlerFunc, error)

Get 根据请求方法 获取一个注册的方法

func (*RouteGroup) PrefixFillRoutes

func (rg *RouteGroup) PrefixFillRoutes(prefix string, c interface{})

PrefixFillRoutes 前缀匹配规则 填充路由 暂时只支持GET、POST、WS 将一个Controller结构下的方法按照方法名称注册到routeGroup中

func (*RouteGroup) Set

func (rg *RouteGroup) Set(method string, path string, handler HandlerFunc)

Set 注册一个路由

func (*RouteGroup) SetDEL

func (rg *RouteGroup) SetDEL(path string, handler HandlerFunc)

SetDEL 注册一个PUT方法可用的路由

func (*RouteGroup) SetGET

func (rg *RouteGroup) SetGET(path string, handler HandlerFunc)

SetGET 注册一个GET方法请求到的路由

func (*RouteGroup) SetMiddleware

func (rg *RouteGroup) SetMiddleware(fn HandlerFunc)

SetMiddleware 注册中间件

func (*RouteGroup) SetPOST

func (rg *RouteGroup) SetPOST(path string, handler HandlerFunc)

SetPOST 注册一个POST方法可用的路由

func (*RouteGroup) SetPUT

func (rg *RouteGroup) SetPUT(path string, handler HandlerFunc)

SetPUT 注册一个PUT方法可用的路由

func (*RouteGroup) SetPathStyleConnector

func (rg *RouteGroup) SetPathStyleConnector()

SetPathStyleConnector 设置路径风格为连字符格式 将驼峰转为连接符风格

func (*RouteGroup) SetPathStyleHump

func (rg *RouteGroup) SetPathStyleHump()

SetPathStyleHump 设置路径风格为驼峰 即区分大小写

func (*RouteGroup) SetRoute404

func (rg *RouteGroup) SetRoute404(fn HandlerFunc)

SetRoute404 注册404回调方法

func (*RouteGroup) SetWS

func (rg *RouteGroup) SetWS(path string, handler HandlerFunc)

SetWS 注册一个websocket路由

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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