0 Star 0 Fork 41

winting/lessgo

forked from andeyalee/lessgo 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
middleware.go 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
package lessgo
import (
"fmt"
)
// 一旦注册,不可再更改
type MiddlewareObj struct {
Name string // 全局唯一
Description string
MiddlewareFunc
}
// 登记全局中间件
var middlewareMap = map[string]MiddlewareObj{}
func init() {
RegMiddleware("检查网站是否开启", "", checkServer())
RegMiddleware("自动匹配home页面", "", checkHome())
RegMiddleware("运行时请求日志", "", RequestLogger())
RegMiddleware("异常恢复", "", Recover())
}
// 必须在init()中调用
func RegMiddleware(name, description string, middleware interface{}) error {
if _, ok := middlewareMap[name]; ok {
err := fmt.Errorf("RegisterMiddlewareFunc called twice for middleware %v.", name)
DefLessgo.Logger().Error("%v", err)
return err
}
middlewareMap[name] = MiddlewareObj{
Name: name,
Description: description,
MiddlewareFunc: WrapMiddleware(middleware),
}
return nil
}
func MiddlewareMap() map[string]MiddlewareObj {
return middlewareMap
}
func middlewareCheck(middlewareNames []string) error {
var errstring string
for _, m := range middlewareNames {
_, ok := middlewareMap[m]
if !ok {
errstring += " \"" + m + "\""
}
}
if len(errstring) == 0 {
return nil
}
return fmt.Errorf("Specified below middlewares does not exist: %v\n", errstring)
}
func getMiddlewares(names []string) []MiddlewareFunc {
mws := make([]MiddlewareFunc, len(names))
for i, mw := range names {
mws[i] = middlewareMap[mw].MiddlewareFunc
}
return mws
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/winting/lessgo.git
git@gitee.com:winting/lessgo.git
winting
lessgo
lessgo
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385