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

Create new controller per request #68

Merged
merged 2 commits into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type route struct {
//
// utron uses the alice package to chain middlewares, this means all alice compatible middleware
// works out of the box
func (r *Router) Add(ctrl Controller, middlewares ...interface{}) error {
func (r *Router) Add(ctrlfn func() Controller, middlewares ...interface{}) error {
var (

// routes is a slice of all routes associated
Expand All @@ -80,7 +80,7 @@ func (r *Router) Add(ctrl Controller, middlewares ...interface{}) error {
)

baseCtr := reflect.ValueOf(&BaseController{})
ctrlVal := reflect.ValueOf(ctrl)
ctrlVal := reflect.ValueOf(ctrlfn())

bTyp := baseCtr.Type()
cTyp := ctrlVal.Type()
Expand Down Expand Up @@ -190,7 +190,7 @@ func (r *Router) Add(ctrl Controller, middlewares ...interface{}) error {
// use routes from the configuration file first
for _, rFile := range r.routes {
if rFile.ctrl == v.ctrl && rFile.fn == v.fn {
if err := r.add(rFile, ctrl, middlewares...); err != nil {
if err := r.add(rFile, ctrlfn, middlewares...); err != nil {
return err
}
found = true
Expand All @@ -201,7 +201,7 @@ func (r *Router) Add(ctrl Controller, middlewares ...interface{}) error {
if !found {
for _, rFile := range routes.inCtrl {
if rFile.fn == v.fn {
if err := r.add(rFile, ctrl, middlewares...); err != nil {
if err := r.add(rFile, ctrlfn, middlewares...); err != nil {
return err
}
found = true
Expand All @@ -211,7 +211,7 @@ func (r *Router) Add(ctrl Controller, middlewares ...interface{}) error {

// resolve to sandard when everything else never matched
if !found {
if err := r.add(v, ctrl, middlewares...); err != nil {
if err := r.add(v, ctrlfn, middlewares...); err != nil {
return err
}
}
Expand Down Expand Up @@ -310,7 +310,7 @@ func (m *middleware) ToHandler(ctx *Context) func(http.Handler) http.Handler {

// add registers controller ctrl, using activeRoute. If middlewares are provided, utron uses
// alice package to chain middlewares.
func (r *Router) add(activeRoute *route, ctrl Controller, middlewares ...interface{}) error {
func (r *Router) add(activeRoute *route, ctrlfn func() Controller, middlewares ...interface{}) error {
var m []*middleware
if len(middlewares) > 0 {
for _, v := range middlewares {
Expand All @@ -337,15 +337,15 @@ func (r *Router) add(activeRoute *route, ctrl Controller, middlewares ...interfa
ctx := NewContext(w, req)
r.prepareContext(ctx)
chain := chainMiddleware(ctx, m...)
chain.ThenFunc(r.wrapController(ctx, activeRoute.fn, ctrl)).ServeHTTP(w, req)
chain.ThenFunc(r.wrapController(ctx, activeRoute.fn, ctrlfn())).ServeHTTP(w, req)
}).Methods(activeRoute.methods...)
return nil
}
r.HandleFunc(activeRoute.pattern, func(w http.ResponseWriter, req *http.Request) {
ctx := NewContext(w, req)
r.prepareContext(ctx)
chain := chainMiddleware(ctx, m...)
chain.ThenFunc(r.wrapController(ctx, activeRoute.fn, ctrl)).ServeHTTP(w, req)
chain.ThenFunc(r.wrapController(ctx, activeRoute.fn, ctrlfn())).ServeHTTP(w, req)
})

return nil
Expand Down Expand Up @@ -391,7 +391,7 @@ func (r *Router) handleController(ctx *Context, fn string, ctrl Controller) {
_ = ctx.Commit()
return
}
err := ctrl.Render()
err := ctx.Commit()
if err != nil {
logThis.Errors(err)
}
Expand Down
8 changes: 4 additions & 4 deletions utron.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ func findConfigFile(dir string, name string) (file string, err error) {
}

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

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

// RegisterController registers a controller in the global utron App.
func RegisterController(ctrl Controller, middlewares ...interface{}) {
_ = baseApp.router.Add(ctrl, middlewares...)
func RegisterController(ctrlfn func() Controller, middlewares ...interface{}) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we improve this a little bit. This is going to break the API.

Maybe wrap the ctrl to a func that returns a new instance of ctrl everytime it is called?

This will keep everything working and fix the issue altogether.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how i can do it) What do you mean?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it myself, based on your changes. 😄

_ = baseApp.router.Add(ctrlfn, middlewares...)
}

// ServeHTTP serves request using global utron App.
Expand Down