2019-08-03 15:54:12 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
2019-09-14 22:53:28 +08:00
|
|
|
import (
|
2020-04-28 15:04:07 +08:00
|
|
|
"github.com/gogf/gf/errors/gerror"
|
2019-09-14 22:53:28 +08:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/gogf/gf/util/gutil"
|
|
|
|
)
|
2019-08-03 15:54:12 +08:00
|
|
|
|
2019-12-03 17:16:52 +08:00
|
|
|
// Middleware is the plugin for request workflow management.
|
2019-08-06 20:40:04 +08:00
|
|
|
type Middleware struct {
|
2019-12-04 10:03:03 +08:00
|
|
|
served bool // Is the request served, which is used for checking response status 404.
|
|
|
|
request *Request // The request object pointer.
|
|
|
|
handlerIndex int // Index number for executing sequence purpose for handler items.
|
|
|
|
handlerMDIndex int // Index number for executing sequence purpose for bound middleware of handler item.
|
2019-08-03 15:54:12 +08:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:26:50 +08:00
|
|
|
// Next calls the next workflow handler.
|
2020-04-06 22:31:45 +08:00
|
|
|
// It's an important function controlling the workflow of the server request execution.
|
2019-08-06 20:40:04 +08:00
|
|
|
func (m *Middleware) Next() {
|
2019-12-04 10:03:03 +08:00
|
|
|
var item *handlerParsedItem
|
|
|
|
var loop = true
|
2019-09-14 22:53:28 +08:00
|
|
|
for loop {
|
2019-10-09 15:26:50 +08:00
|
|
|
// Check whether the request is exited.
|
2019-12-04 10:03:03 +08:00
|
|
|
if m.request.IsExited() || m.handlerIndex >= len(m.request.handlers) {
|
2019-10-09 15:26:50 +08:00
|
|
|
break
|
2019-08-03 15:54:12 +08:00
|
|
|
}
|
2019-12-04 10:03:03 +08:00
|
|
|
item = m.request.handlers[m.handlerIndex]
|
2019-10-09 15:26:50 +08:00
|
|
|
// Filter the HOOK handlers, which are designed to be called in another standalone procedure.
|
2019-08-03 15:54:12 +08:00
|
|
|
if item.handler.itemType == gHANDLER_TYPE_HOOK {
|
2019-12-04 10:03:03 +08:00
|
|
|
m.handlerIndex++
|
2019-08-03 15:54:12 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-12-03 17:16:52 +08:00
|
|
|
// Current router switching.
|
2019-08-03 15:54:12 +08:00
|
|
|
m.request.Router = item.handler.router
|
2019-10-09 15:26:50 +08:00
|
|
|
|
2020-02-16 22:39:12 +08:00
|
|
|
// Router values switching.
|
|
|
|
m.request.routerMap = item.values
|
|
|
|
|
2019-09-14 22:53:28 +08:00
|
|
|
gutil.TryCatch(func() {
|
2019-12-04 10:03:03 +08:00
|
|
|
// Execute bound middleware array of the item if it's not empty.
|
|
|
|
if m.handlerMDIndex < len(item.handler.middleware) {
|
|
|
|
md := item.handler.middleware[m.handlerMDIndex]
|
|
|
|
m.handlerMDIndex++
|
|
|
|
niceCallFunc(func() {
|
|
|
|
md(m.request)
|
|
|
|
})
|
|
|
|
loop = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.handlerIndex++
|
|
|
|
|
2019-09-14 22:53:28 +08:00
|
|
|
switch item.handler.itemType {
|
2019-12-04 10:03:03 +08:00
|
|
|
// Service controller.
|
2019-09-14 22:53:28 +08:00
|
|
|
case gHANDLER_TYPE_CONTROLLER:
|
|
|
|
m.served = true
|
|
|
|
if m.request.IsExited() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c := reflect.New(item.handler.ctrlInfo.reflect)
|
2019-08-03 15:54:12 +08:00
|
|
|
niceCallFunc(func() {
|
2019-09-14 22:53:28 +08:00
|
|
|
c.MethodByName("Init").Call([]reflect.Value{reflect.ValueOf(m.request)})
|
2019-08-03 15:54:12 +08:00
|
|
|
})
|
2019-09-14 22:53:28 +08:00
|
|
|
if !m.request.IsExited() {
|
|
|
|
niceCallFunc(func() {
|
|
|
|
c.MethodByName(item.handler.ctrlInfo.name).Call(nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !m.request.IsExited() {
|
|
|
|
niceCallFunc(func() {
|
|
|
|
c.MethodByName("Shut").Call(nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-04 10:03:03 +08:00
|
|
|
// Service object.
|
2019-09-14 22:53:28 +08:00
|
|
|
case gHANDLER_TYPE_OBJECT:
|
|
|
|
m.served = true
|
|
|
|
if m.request.IsExited() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if item.handler.initFunc != nil {
|
|
|
|
niceCallFunc(func() {
|
|
|
|
item.handler.initFunc(m.request)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !m.request.IsExited() {
|
|
|
|
niceCallFunc(func() {
|
|
|
|
item.handler.itemFunc(m.request)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !m.request.IsExited() && item.handler.shutFunc != nil {
|
|
|
|
niceCallFunc(func() {
|
|
|
|
item.handler.shutFunc(m.request)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-04 10:03:03 +08:00
|
|
|
// Service handler.
|
2019-09-14 22:53:28 +08:00
|
|
|
case gHANDLER_TYPE_HANDLER:
|
|
|
|
m.served = true
|
|
|
|
if m.request.IsExited() {
|
|
|
|
break
|
|
|
|
}
|
2019-08-03 15:54:12 +08:00
|
|
|
niceCallFunc(func() {
|
|
|
|
item.handler.itemFunc(m.request)
|
|
|
|
})
|
2019-09-14 22:53:28 +08:00
|
|
|
|
2019-12-04 10:03:03 +08:00
|
|
|
// Global middleware array.
|
2019-09-14 22:53:28 +08:00
|
|
|
case gHANDLER_TYPE_MIDDLEWARE:
|
2019-08-03 15:54:12 +08:00
|
|
|
niceCallFunc(func() {
|
2019-09-14 22:53:28 +08:00
|
|
|
item.handler.itemFunc(m.request)
|
2019-08-03 15:54:12 +08:00
|
|
|
})
|
2019-10-09 15:26:50 +08:00
|
|
|
// It does not continue calling next middleware after another middleware done.
|
|
|
|
// There should be a "Next" function to be called in the middleware in order to manage the workflow.
|
2019-09-14 22:53:28 +08:00
|
|
|
loop = false
|
2019-08-03 15:54:12 +08:00
|
|
|
}
|
2019-09-14 22:53:28 +08:00
|
|
|
}, func(exception interface{}) {
|
2020-04-28 15:04:07 +08:00
|
|
|
if e, ok := exception.(gerror.ApiStack); ok {
|
2020-04-29 00:14:29 +08:00
|
|
|
// It's already an error that has stack info.
|
2020-04-28 15:04:07 +08:00
|
|
|
m.request.error = e.(error)
|
|
|
|
} else {
|
2020-04-29 00:14:29 +08:00
|
|
|
// Create a new error with stack info.
|
|
|
|
// Note that there's a skip pointing the start stacktrace
|
|
|
|
// of the real error point.
|
2020-04-28 15:04:07 +08:00
|
|
|
m.request.error = gerror.NewfSkip(1, "%v", exception)
|
|
|
|
}
|
2019-09-14 22:53:28 +08:00
|
|
|
m.request.Response.WriteStatus(http.StatusInternalServerError, exception)
|
2020-03-04 22:52:56 +08:00
|
|
|
loop = false
|
2019-09-14 22:53:28 +08:00
|
|
|
})
|
2019-08-03 15:54:12 +08:00
|
|
|
}
|
2019-10-11 22:54:25 +08:00
|
|
|
// Check the http status code after all handler and middleware done.
|
2019-12-04 10:03:03 +08:00
|
|
|
if m.request.IsExited() || m.handlerIndex >= len(m.request.handlers) {
|
|
|
|
if m.request.Response.Status == 0 {
|
|
|
|
if m.request.Middleware.served {
|
|
|
|
m.request.Response.WriteHeader(http.StatusOK)
|
|
|
|
} else {
|
|
|
|
m.request.Response.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
2019-10-09 15:26:50 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-03 15:54:12 +08:00
|
|
|
}
|