Skip to content

Commit

Permalink
添加 recovery ,修复一下错误
Browse files Browse the repository at this point in the history
  • Loading branch information
broqiang committed Apr 20, 2019
1 parent cbc0bba commit 1ca7f5b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 9 deletions.
33 changes: 32 additions & 1 deletion src/bro/bro.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package bro

import "net/http"
import (
"net/http"
)

// PanicHandler 处理 panic 恢复的 Handler
type PanicHandler func(http.ResponseWriter, *http.Request, interface{})

// Engine 主引擎
type Engine struct {
Expand All @@ -15,6 +20,15 @@ type Engine struct {
RedirectFixedPath bool

NotFound http.Handler

PanicHandler PanicHandler
}

// Default 默认的初始化,注册了 Recovery 中间件
func Default() *Engine {
engine := New()

return engine
}

// New 初始化引擎
Expand All @@ -35,7 +49,24 @@ func New() *Engine {
return &engine
}

func (engine *Engine) recv(w http.ResponseWriter, r *http.Request) {
if rec := recover(); rec != nil {
SystemLogPanic(rec)

if engine.PanicHandler != nil {
engine.PanicHandler(w, r, rec)
return
}

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
}
}

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

defer engine.recv(w, r)

path := r.URL.Path

if root := engine.trees[r.Method]; root != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/bro/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *Context) Next() {

// HandlerName 获取注册的 Handler 名称
func (c *Context) HandlerName() string {
return nameOfFunction(c.handlers.Last())
return NameOfFunction(c.handlers.Last())
}

// Handler 获取注册的 Handler, 最后一个是最终的 Handler ,前面的都是中间件
Expand Down
11 changes: 9 additions & 2 deletions src/bro/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func SystemLogError(err error) {
}
}

// SystemLogPanic 记录 Panic 时候的错误日志
func SystemLogPanic(err interface{}) {
SystemLogf("[Panic] %v", err)
}

// SetSystemLogWriter 定义日志输出到哪个 Writer
func SetSystemLogWriter(writer io.Writer) {
sl.SetOutput(writer)
Expand All @@ -30,9 +35,11 @@ func SetSystemLogWriter(writer io.Writer) {
// SystemLogRoute 注册路由的日志
func SystemLogRoute(httpMethod, absolutePath string, handlers Handlers) {
handlerNumbers := len(handlers)
handlerName := nameOfFunction(handlers.Last())
handlerName := NameOfFunction(handlers.Last())

SystemLogf("register router. method: %s, handlerName: %s ,total handlers: %d\n", httpMethod, handlerName, handlerNumbers)
sl.Output(2,
fmt.Sprintf(" register router:\n\tMethod: %s, \n\tPath: %s \n\tHandlerName: %s \n\tTotal handlers: %d\n\n",
httpMethod, absolutePath, handlerName, handlerNumbers))
}

// SystemLogln 写入系统日志(非访问日志),每一条一行
Expand Down
1 change: 0 additions & 1 deletion src/bro/middleware.go

This file was deleted.

16 changes: 16 additions & 0 deletions src/bro/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ type (
}
)

// Group 定义路由分组
func (group *RouterGroup) Group(relativePath string, handlers ...Handler) *RouterGroup {
return &RouterGroup{
Handlers: group.combineHandlers(handlers),
path: JoinPath(group.path, relativePath),
engine: group.engine,
}
}

// Use 使用中间件
func (group *RouterGroup) Use(handlers ...Handler) {
group.Handlers = group.combineHandlers(handlers)
}

// GET 注册 GET 方式的路由
func (group *RouterGroup) GET(relativePath string, handlers ...Handler) {
group.handle("GET", relativePath, handlers)
Expand Down Expand Up @@ -96,6 +110,8 @@ func (group *RouterGroup) handle(method, relativePath string, handlers Handlers)
path := JoinPath(group.path, relativePath)
handlers = group.combineHandlers(handlers)

SystemLogRoute(method, path, handlers)

root := group.engine.trees[method]
if root == nil {
root = new(node)
Expand Down
9 changes: 5 additions & 4 deletions src/bro/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@ func JoinPath(absolutePath, relativePath string) string {

finalPath := path.Join(absolutePath, relativePath)

if lastChar(finalPath) != '/' && lastChar(relativePath) == '/' {
if LastChar(finalPath) != '/' && LastChar(relativePath) == '/' {
finalPath += "/"
}

return finalPath

}

// 获取字符串的最后一个字符
func lastChar(str string) uint8 {
// LastChar 获取字符串的最后一个字符
func LastChar(str string) uint8 {
if str == "" {
return 0
}

return str[len(str)-1]
}

func nameOfFunction(f interface{}) string {
// NameOfFunction 获取函数的名称
func NameOfFunction(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}

Expand Down

0 comments on commit 1ca7f5b

Please sign in to comment.