Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Commit fb99f6f

Browse files
committed
fix controller clone
1 parent 7103803 commit fb99f6f

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

routes.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

utron.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ func findConfigFile(dir string, name string) (file string, err error) {
171171
}
172172

173173
// AddController registers a controller, and middlewares if any is provided.
174-
func (a *App) AddController(ctrl Controller, middlewares ...interface{}) {
175-
_ = a.router.Add(ctrl, middlewares...)
174+
func (a *App) AddController(ctrlfn func() Controller, middlewares ...interface{}) {
175+
_ = a.router.Add(ctrlfn, middlewares...)
176176
}
177177

178178
// Set is for assigning a value to *App components. The following can be set:
@@ -213,8 +213,8 @@ func RegisterModels(models ...interface{}) {
213213
}
214214

215215
// RegisterController registers a controller in the global utron App.
216-
func RegisterController(ctrl Controller, middlewares ...interface{}) {
217-
_ = baseApp.router.Add(ctrl, middlewares...)
216+
func RegisterController(ctrlfn func() Controller, middlewares ...interface{}) {
217+
_ = baseApp.router.Add(ctrlfn, middlewares...)
218218
}
219219

220220
// ServeHTTP serves request using global utron App.

0 commit comments

Comments
 (0)