handler := handlers[i]
switch t := handler.(type) {
case http.HandlerFunc:
+ if _, ok := resp.(context.ResponseWriter); !ok {
+ resp = context.NewResponse(resp)
+ }
t(resp, req)
if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
return
}
case func(http.ResponseWriter, *http.Request):
+ if _, ok := resp.(context.ResponseWriter); !ok {
+ resp = context.NewResponse(resp)
+ }
t(resp, req)
if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
return
return
}
case func(http.Handler) http.Handler:
- var next = http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
+ next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
if len(handlers) > i+1 {
next = Wrap(handlers[i+1:]...)
}
// Bind binding an obj to a handler
func Bind(obj interface{}) http.HandlerFunc {
- var tp = reflect.TypeOf(obj)
+ tp := reflect.TypeOf(obj)
if tp.Kind() == reflect.Ptr {
tp = tp.Elem()
}
panic("Only structs are allowed to bind")
}
return Wrap(func(ctx *context.Context) {
- var theObj = reflect.New(tp).Interface() // create a new form obj for every request but not use obj directly
+ theObj := reflect.New(tp).Interface() // create a new form obj for every request but not use obj directly
binding.Bind(ctx.Req, theObj)
SetForm(ctx, theObj)
middleware.AssignForm(theObj, ctx.Data)
// Group mounts a sub-Router along a `pattern` string.
func (r *Route) Group(pattern string, fn func(), middlewares ...interface{}) {
- var previousGroupPrefix = r.curGroupPrefix
- var previousMiddlewares = r.curMiddlewares
+ previousGroupPrefix := r.curGroupPrefix
+ previousMiddlewares := r.curMiddlewares
r.curGroupPrefix += pattern
r.curMiddlewares = append(r.curMiddlewares, middlewares...)
// Mount attaches another Route along ./pattern/*
func (r *Route) Mount(pattern string, subR *Route) {
- var middlewares = make([]interface{}, len(r.curMiddlewares))
+ middlewares := make([]interface{}, len(r.curMiddlewares))
copy(middlewares, r.curMiddlewares)
subR.Use(middlewares...)
r.R.Mount(r.getPattern(pattern), subR.R)
// Any delegate requests for all methods
func (r *Route) Any(pattern string, h ...interface{}) {
- var middlewares = r.getMiddlewares(h)
+ middlewares := r.getMiddlewares(h)
r.R.HandleFunc(r.getPattern(pattern), Wrap(middlewares...))
}
func (r *Route) Route(pattern, methods string, h ...interface{}) {
p := r.getPattern(pattern)
ms := strings.Split(methods, ",")
- var middlewares = r.getMiddlewares(h)
+ middlewares := r.getMiddlewares(h)
for _, method := range ms {
r.R.MethodFunc(strings.TrimSpace(method), p, Wrap(middlewares...))
}
// Delete delegate delete method
func (r *Route) Delete(pattern string, h ...interface{}) {
- var middlewares = r.getMiddlewares(h)
+ middlewares := r.getMiddlewares(h)
r.R.Delete(r.getPattern(pattern), Wrap(middlewares...))
}
func (r *Route) getMiddlewares(h []interface{}) []interface{} {
- var middlewares = make([]interface{}, len(r.curMiddlewares), len(r.curMiddlewares)+len(h))
+ middlewares := make([]interface{}, len(r.curMiddlewares), len(r.curMiddlewares)+len(h))
copy(middlewares, r.curMiddlewares)
middlewares = append(middlewares, h...)
return middlewares
// Get delegate get method
func (r *Route) Get(pattern string, h ...interface{}) {
- var middlewares = r.getMiddlewares(h)
+ middlewares := r.getMiddlewares(h)
r.R.Get(r.getPattern(pattern), Wrap(middlewares...))
}
// Options delegate options method
func (r *Route) Options(pattern string, h ...interface{}) {
- var middlewares = r.getMiddlewares(h)
+ middlewares := r.getMiddlewares(h)
r.R.Options(r.getPattern(pattern), Wrap(middlewares...))
}
// GetOptions delegate get and options method
func (r *Route) GetOptions(pattern string, h ...interface{}) {
- var middlewares = r.getMiddlewares(h)
+ middlewares := r.getMiddlewares(h)
r.R.Get(r.getPattern(pattern), Wrap(middlewares...))
r.R.Options(r.getPattern(pattern), Wrap(middlewares...))
}
// PostOptions delegate post and options method
func (r *Route) PostOptions(pattern string, h ...interface{}) {
- var middlewares = r.getMiddlewares(h)
+ middlewares := r.getMiddlewares(h)
r.R.Post(r.getPattern(pattern), Wrap(middlewares...))
r.R.Options(r.getPattern(pattern), Wrap(middlewares...))
}
// Head delegate head method
func (r *Route) Head(pattern string, h ...interface{}) {
- var middlewares = r.getMiddlewares(h)
+ middlewares := r.getMiddlewares(h)
r.R.Head(r.getPattern(pattern), Wrap(middlewares...))
}
// Post delegate post method
func (r *Route) Post(pattern string, h ...interface{}) {
- var middlewares = r.getMiddlewares(h)
+ middlewares := r.getMiddlewares(h)
r.R.Post(r.getPattern(pattern), Wrap(middlewares...))
}
// Put delegate put method
func (r *Route) Put(pattern string, h ...interface{}) {
- var middlewares = r.getMiddlewares(h)
+ middlewares := r.getMiddlewares(h)
r.R.Put(r.getPattern(pattern), Wrap(middlewares...))
}
// Patch delegate patch method
func (r *Route) Patch(pattern string, h ...interface{}) {
- var middlewares = r.getMiddlewares(h)
+ middlewares := r.getMiddlewares(h)
r.R.Patch(r.getPattern(pattern), Wrap(middlewares...))
}