diff --git a/app/app.go b/app/app.go index 0324ef9..70615b5 100644 --- a/app/app.go +++ b/app/app.go @@ -66,6 +66,7 @@ func startHttpSrv() { fmt.Println(fmt.Sprintf("[ORANGE] \033[0;33m http server shutdown %v \033[0m ", err)) logger.Critical("http server shutdown %v", err) + // http服务关闭后等待业务处理完成后关闭应用 exitWaitHandler.wg.Wait() logger.Critical("app server shutdown") @@ -101,17 +102,15 @@ func handlerFunc(node routerNode) func(writer http.ResponseWriter, request *http ctx.responseBody.Reset() ctx.OrangeInput = orangerequest.NewInput(request, maxBodySize) - routeFuncDo := func(next HandlerFunc) HandlerFunc { + routeFuncDo := func() HandlerFunc { return func(c *Context) error { - node.appHandler(c) - return next(c) + return node.appHandler(c) } } // 中间件依次调用 - var middleHandlerFunc HandlerFunc = routeFuncDo(func(c *Context) error { - return nil - }) + var middleHandlerFunc HandlerFunc = routeFuncDo() + for _, item := range node.middlewares { middleFunc := item.Func() middleHandlerFunc = middleFunc(middleHandlerFunc) diff --git a/app/context.go b/app/context.go index e42b844..697579b 100644 --- a/app/context.go +++ b/app/context.go @@ -36,11 +36,19 @@ func (c *Context) SetRW(w http.ResponseWriter, r *http.Request) { c.request = r } +func (c *Context) HttpError(error string, code int) { + http.Error(c.response, error, code) +} + func (c *Context) ResponseWrite(b []byte) error { _, err := c.responseBody.Write(b) return err } +func (c *Context) ResponseWriteHeader(code int) { + c.response.WriteHeader(code) +} + func (c *Context) ResponseHeader() http.Header { return c.response.Header() } diff --git a/app/shutdown.go b/app/shutdown.go index c8095e0..13cc5d9 100644 --- a/app/shutdown.go +++ b/app/shutdown.go @@ -60,7 +60,7 @@ func ListenStop(stopSig chan StopSignal){ -// listenShutDownSign 监听推出信号 +// listenShutDownSign 监听退出信号 func listenShutDownSign(ctx context.Context, httpSrv *http.Server){ exitSig := []os.Signal{