This commit is contained in:
John Guo
2021-10-13 22:28:49 +08:00
parent f887c9f44b
commit 920c97af79
14 changed files with 91 additions and 39 deletions

View File

@ -15,7 +15,7 @@ import (
type DefaultHandlerResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
Data interface{} `json:"data,omitempty"`
}
// MiddlewareHandlerResponse is the default middleware handling handler response object and its error.
@ -30,6 +30,7 @@ func MiddlewareHandlerResponse(r *Request) {
var (
err error
res interface{}
ctx = r.Context()
internalErr error
)
res, err = r.GetHandlerResponse()
@ -44,7 +45,7 @@ func MiddlewareHandlerResponse(r *Request) {
Data: nil,
})
if internalErr != nil {
intlog.Error(r.Context(), internalErr)
intlog.Error(ctx, internalErr)
}
return
}
@ -54,6 +55,6 @@ func MiddlewareHandlerResponse(r *Request) {
Data: res,
})
if internalErr != nil {
intlog.Error(r.Context(), internalErr)
intlog.Error(ctx, internalErr)
}
}

View File

@ -26,18 +26,23 @@ import (
// Request is the context object for a request.
type Request struct {
*http.Request
Server *Server // Server.
Cookie *Cookie // Cookie.
Session *gsession.Session // Session.
Response *Response // Corresponding Response of this request.
Router *Router // Matched Router for this request. Note that it's not available in HOOK handler.
EnterTime int64 // Request starting time in microseconds.
LeaveTime int64 // Request ending time in microseconds.
Middleware *middleware // Middleware manager.
StaticFile *staticFile // Static file object for static file serving.
Server *Server // Server.
Cookie *Cookie // Cookie.
Session *gsession.Session // Session.
Response *Response // Corresponding Response of this request.
Router *Router // Matched Router for this request. Note that it's not available in HOOK handler.
EnterTime int64 // Request starting time in microseconds.
LeaveTime int64 // Request ending time in microseconds.
Middleware *middleware // Middleware manager.
StaticFile *staticFile // Static file object for static file serving.
// =================================================================================================================
// Private attributes for internal usage purpose.
// =================================================================================================================
context context.Context // Custom context for internal usage purpose.
handlers []*handlerParsedItem // All matched handlers containing handler, hook and middleware for this request.
handlerResponse handlerResponse // Handler response object and its error value.
handlerResponse handlerResponse // Handler response object and its error value for Request/Response handler.
hasHookHandler bool // A bool marking whether there's hook handler in the handlers for performance purpose.
hasServeHandler bool // A bool marking whether there's serving handler in the handlers for performance purpose.
parsedQuery bool // A bool marking whether the GET parameters parsed.
@ -56,6 +61,7 @@ type Request struct {
isFileRequest bool // A bool marking whether current request is file serving.
viewObject *gview.View // Custom template view engine object for this response.
viewParams gview.Params // Custom template view variables for this response.
originUrlPath string // Original URL path that passed from client.
}
type handlerResponse struct {
@ -73,10 +79,11 @@ type staticFile struct {
// newRequest creates and returns a new request object.
func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
request := &Request{
Server: s,
Request: r,
Response: newResponse(s, w),
EnterTime: gtime.TimestampMilli(),
Server: s,
Request: r,
Response: newResponse(s, w),
EnterTime: gtime.TimestampMilli(),
originUrlPath: r.URL.Path,
}
request.Cookie = GetCookie(request)
request.Session = s.sessionManager.New(
@ -99,6 +106,17 @@ func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
if err != nil {
panic(err)
}
// Remove char '/' in the tail of URI.
if request.URL.Path != "/" {
for len(request.URL.Path) > 0 && request.URL.Path[len(request.URL.Path)-1] == '/' {
request.URL.Path = request.URL.Path[:len(request.URL.Path)-1]
}
}
// Default URI value if it's empty.
if request.URL.Path == "" {
request.URL.Path = "/"
}
return request
}

View File

@ -127,7 +127,7 @@ func (s *Server) Start() error {
// Swagger UI.
if s.config.SwaggerPath != "" {
swaggerui.InitSwaggerUI()
swaggerui.Init()
s.AddStaticPath(s.config.SwaggerPath, swaggerUIPackedPath)
s.BindHookHandler(s.config.SwaggerPath+"/*", HookBeforeServe, s.swaggerUI)
s.Logger().Debugf(

View File

@ -44,18 +44,6 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
// Remove char '/' in the tail of URI.
if r.URL.Path != "/" {
for len(r.URL.Path) > 0 && r.URL.Path[len(r.URL.Path)-1] == '/' {
r.URL.Path = r.URL.Path[:len(r.URL.Path)-1]
}
}
// Default URI value if it's empty.
if r.URL.Path == "" {
r.URL.Path = "/"
}
// Create a new request object.
request := newRequest(s, r, w)

View File

@ -9,6 +9,7 @@ package ghttp
import (
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"
"net/http"
)
const (
@ -21,7 +22,15 @@ func (s *Server) swaggerUI(r *Request) {
if s.config.OpenApiPath == "" {
return
}
if r.StaticFile != nil && r.StaticFile.File != nil && gfile.Basename(r.StaticFile.File.Name()) == "index.html" {
var (
indexFileName = `index.html`
)
if r.StaticFile != nil && r.StaticFile.File != nil && gfile.Basename(r.StaticFile.File.Name()) == indexFileName {
if gfile.Basename(r.URL.Path) != indexFileName && r.originUrlPath[len(r.originUrlPath)-1] != '/' {
r.Response.Header().Set("Location", r.originUrlPath+"/")
r.Response.WriteHeader(http.StatusMovedPermanently)
r.ExitAll()
}
r.Response.Write(gstr.Replace(
string(r.StaticFile.File.Content()),
swaggerUIDefaultURL,

File diff suppressed because one or more lines are too long

View File

@ -7,5 +7,5 @@
// Package swaggerui provides packed swagger ui static files using resource manager.
//
// Files from: https://github.com/swagger-api/swagger-ui
// Pack command: gf pack swagger-ui swaggerui.go -n=swaggerui -p=/goframe/swaggerui
// Pack command: gf pack swagger-ui swaggerui-packed.go -n=swaggerui -p=/goframe/swaggerui
package swaggerui