http相关方法封装
This commit is contained in:
parent
5f72cf0155
commit
d98761b49c
11
app/app.go
11
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)
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ func ListenStop(stopSig chan StopSignal){
|
|||
|
||||
|
||||
|
||||
// listenShutDownSign 监听推出信号
|
||||
// listenShutDownSign 监听退出信号
|
||||
func listenShutDownSign(ctx context.Context, httpSrv *http.Server){
|
||||
|
||||
exitSig := []os.Signal{
|
||||
|
|
Loading…
Reference in New Issue