mirror of
https://github.com/kataras/iris.git
synced 2026-01-25 04:45:57 +00:00
Fix iris run main.go not worked properly on some editors. Add notes for next version. Read HISTORY.md
This commit is contained in:
108
http.go
108
http.go
@@ -189,9 +189,15 @@ func StatusText(code int) string {
|
||||
return statusText[code]
|
||||
}
|
||||
|
||||
// errHandler returns na error with message: 'Passed argument is not func(*Context) neither an object which implements the iris.Handler with Serve(ctx *Context)
|
||||
// errHandler returns na error with message: 'Passed argument is not func(*Context) neither an object which implements the iris.Default.Handler with Serve(ctx *Context)
|
||||
// It seems to be a +type Points to: +pointer.'
|
||||
var errHandler = errors.New("Passed argument is not func(*Context) neither an object which implements the iris.Handler with Serve(ctx *Context)\n It seems to be a %T Points to: %v.")
|
||||
var errHandler = errors.New(`
|
||||
Passed argument is not an iris.Handler (or func(*iris.Context)) neither one of these types:
|
||||
- http.Handler
|
||||
- func(w http.ResponseWriter, r *http.Request)
|
||||
- func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
|
||||
---------------------------------------------------------------------
|
||||
It seems to be a %T points to: %v`)
|
||||
|
||||
type (
|
||||
// Handler the main Iris Handler interface.
|
||||
@@ -217,37 +223,103 @@ func (h HandlerFunc) Serve(ctx *Context) {
|
||||
}
|
||||
|
||||
// ToNativeHandler converts an iris handler to http.Handler
|
||||
func ToNativeHandler(station *Framework, h Handler) http.Handler {
|
||||
func ToNativeHandler(s *Framework, h Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := station.AcquireCtx(w, r)
|
||||
ctx := s.AcquireCtx(w, r)
|
||||
h.Serve(ctx)
|
||||
station.ReleaseCtx(ctx)
|
||||
s.ReleaseCtx(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
// ToHandler converts an http.Handler/HandlerFunc to iris.Handler(Func)
|
||||
// ToHandler converts different type styles of handlers that you
|
||||
// used to use (usually with third-party net/http middleware) to an iris.HandlerFunc.
|
||||
//
|
||||
// Supported types:
|
||||
// - .ToHandler(h http.Handler)
|
||||
// - .ToHandler(func(w http.ResponseWriter, r *http.Request))
|
||||
// - .ToHandler(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc))
|
||||
func ToHandler(handler interface{}) HandlerFunc {
|
||||
|
||||
//this is not the best way to do it, but I dont have any options right now.
|
||||
switch handler.(type) {
|
||||
case HandlerFunc:
|
||||
//it's already an iris handler
|
||||
return handler.(HandlerFunc)
|
||||
case http.Handler:
|
||||
//it's http.Handler
|
||||
h := handler.(http.Handler)
|
||||
return func(ctx *Context) {
|
||||
h.ServeHTTP(ctx.ResponseWriter, ctx.Request)
|
||||
{
|
||||
//
|
||||
//it's already an iris handler
|
||||
//
|
||||
return handler.(HandlerFunc)
|
||||
}
|
||||
|
||||
case http.Handler:
|
||||
//
|
||||
// handlerFunc.ServeHTTP(w,r)
|
||||
//
|
||||
{
|
||||
h := handler.(http.Handler)
|
||||
return func(ctx *Context) {
|
||||
h.ServeHTTP(ctx.ResponseWriter, ctx.Request)
|
||||
}
|
||||
}
|
||||
|
||||
case func(http.ResponseWriter, *http.Request):
|
||||
return ToHandler(http.HandlerFunc(handler.(func(http.ResponseWriter, *http.Request))))
|
||||
//for func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc): READ iris-conrib/middleware/README.md for details
|
||||
{
|
||||
//
|
||||
// handlerFunc(w,r)
|
||||
//
|
||||
return ToHandler(http.HandlerFunc(handler.(func(http.ResponseWriter, *http.Request))))
|
||||
}
|
||||
|
||||
case func(http.ResponseWriter, *http.Request, http.HandlerFunc):
|
||||
{
|
||||
//
|
||||
// handlerFunc(w,r, http.HandlerFunc)
|
||||
//
|
||||
return toHandlerNextHTTPHandlerFunc(handler.(func(http.ResponseWriter, *http.Request, http.HandlerFunc)))
|
||||
}
|
||||
|
||||
default:
|
||||
panic(errHandler.Format(handler, handler))
|
||||
{
|
||||
//
|
||||
// No valid handler passed
|
||||
//
|
||||
panic(errHandler.Format(handler, handler))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func toHandlerNextHTTPHandlerFunc(h func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)) HandlerFunc {
|
||||
return HandlerFunc(func(ctx *Context) {
|
||||
// take the next handler in route's chain
|
||||
nextIrisHandler := ctx.NextHandler()
|
||||
if nextIrisHandler != nil {
|
||||
executed := false // we need to watch this in order to StopExecution from all next handlers
|
||||
// if this next handler is not executed by the third-party net/http next-style middleware.
|
||||
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
nextIrisHandler.Serve(ctx)
|
||||
executed = true
|
||||
})
|
||||
|
||||
h(ctx.ResponseWriter, ctx.Request, nextHandler)
|
||||
|
||||
// after third-party middleware's job:
|
||||
if executed {
|
||||
// if next is executed then increment the ctx.Pos manually
|
||||
// in order to the next handler not to be executed twice.
|
||||
ctx.Pos++
|
||||
} else {
|
||||
// otherwise StopExecution from all next handlers.
|
||||
ctx.StopExecution()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// if not next handler found then this is not a 'valid' middleware but
|
||||
// some middleware may don't care about next,
|
||||
// so we just execute the handler with an empty net.
|
||||
h(ctx.ResponseWriter, ctx.Request, http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||
})
|
||||
}
|
||||
|
||||
// convertToHandlers just make []HandlerFunc to []Handler, although HandlerFunc and Handler are the same
|
||||
// we need this on some cases we explicit want a interface Handler, it is useless for users.
|
||||
func convertToHandlers(handlersFn []HandlerFunc) []Handler {
|
||||
|
||||
Reference in New Issue
Block a user