@@ -60,7 +60,7 @@ type route struct {
6060//
6161// utron uses the alice package to chain middlewares, this means all alice compatible middleware
6262// works out of the box
63- func (r * Router ) Add (ctrl Controller , middlewares ... interface {}) error {
63+ func (r * Router ) Add (ctrlfn func () Controller , middlewares ... interface {}) error {
6464 var (
6565
6666 // routes is a slice of all routes associated
@@ -80,7 +80,7 @@ func (r *Router) Add(ctrl Controller, middlewares ...interface{}) error {
8080 )
8181
8282 baseCtr := reflect .ValueOf (& BaseController {})
83- ctrlVal := reflect .ValueOf (ctrl )
83+ ctrlVal := reflect .ValueOf (ctrlfn () )
8484
8585 bTyp := baseCtr .Type ()
8686 cTyp := ctrlVal .Type ()
@@ -190,7 +190,7 @@ func (r *Router) Add(ctrl Controller, middlewares ...interface{}) error {
190190 // use routes from the configuration file first
191191 for _ , rFile := range r .routes {
192192 if rFile .ctrl == v .ctrl && rFile .fn == v .fn {
193- if err := r .add (rFile , ctrl , middlewares ... ); err != nil {
193+ if err := r .add (rFile , ctrlfn , middlewares ... ); err != nil {
194194 return err
195195 }
196196 found = true
@@ -201,7 +201,7 @@ func (r *Router) Add(ctrl Controller, middlewares ...interface{}) error {
201201 if ! found {
202202 for _ , rFile := range routes .inCtrl {
203203 if rFile .fn == v .fn {
204- if err := r .add (rFile , ctrl , middlewares ... ); err != nil {
204+ if err := r .add (rFile , ctrlfn , middlewares ... ); err != nil {
205205 return err
206206 }
207207 found = true
@@ -211,7 +211,7 @@ func (r *Router) Add(ctrl Controller, middlewares ...interface{}) error {
211211
212212 // resolve to sandard when everything else never matched
213213 if ! found {
214- if err := r .add (v , ctrl , middlewares ... ); err != nil {
214+ if err := r .add (v , ctrlfn , middlewares ... ); err != nil {
215215 return err
216216 }
217217 }
@@ -310,7 +310,7 @@ func (m *middleware) ToHandler(ctx *Context) func(http.Handler) http.Handler {
310310
311311// add registers controller ctrl, using activeRoute. If middlewares are provided, utron uses
312312// alice package to chain middlewares.
313- func (r * Router ) add (activeRoute * route , ctrl Controller , middlewares ... interface {}) error {
313+ func (r * Router ) add (activeRoute * route , ctrlfn func () Controller , middlewares ... interface {}) error {
314314 var m []* middleware
315315 if len (middlewares ) > 0 {
316316 for _ , v := range middlewares {
@@ -337,15 +337,15 @@ func (r *Router) add(activeRoute *route, ctrl Controller, middlewares ...interfa
337337 ctx := NewContext (w , req )
338338 r .prepareContext (ctx )
339339 chain := chainMiddleware (ctx , m ... )
340- chain .ThenFunc (r .wrapController (ctx , activeRoute .fn , ctrl )).ServeHTTP (w , req )
340+ chain .ThenFunc (r .wrapController (ctx , activeRoute .fn , ctrlfn () )).ServeHTTP (w , req )
341341 }).Methods (activeRoute .methods ... )
342342 return nil
343343 }
344344 r .HandleFunc (activeRoute .pattern , func (w http.ResponseWriter , req * http.Request ) {
345345 ctx := NewContext (w , req )
346346 r .prepareContext (ctx )
347347 chain := chainMiddleware (ctx , m ... )
348- chain .ThenFunc (r .wrapController (ctx , activeRoute .fn , ctrl )).ServeHTTP (w , req )
348+ chain .ThenFunc (r .wrapController (ctx , activeRoute .fn , ctrlfn () )).ServeHTTP (w , req )
349349 })
350350
351351 return nil
@@ -384,7 +384,7 @@ func (r *Router) handleController(ctx *Context, fn string, ctrl Controller) {
384384
385385 // execute the method
386386 // TODO: better error handling?
387- if x := ita .New (ctrl ).Call (fn , ctx ); x .Error () != nil {
387+ if x := ita .New (ctrl ).Call (fn ); x .Error () != nil {
388388 ctx .Set (http .StatusInternalServerError )
389389 _ , _ = ctx .Write ([]byte (x .Error ().Error ()))
390390 ctx .TextPlain ()
0 commit comments