github.com/lenfree/buffalo@v0.7.3-0.20170207163156-891616ea4064/examples/html-resource/actions/app.go (about) 1 package actions 2 3 import ( 4 "net/http" 5 "os" 6 7 "github.com/gobuffalo/buffalo" 8 "github.com/gobuffalo/buffalo/examples/html-resource/models" 9 "github.com/gobuffalo/buffalo/middleware" 10 "github.com/markbates/going/defaults" 11 ) 12 13 // ENV is used to help switch settings based on where the 14 // application is being run. Default is "development". 15 var ENV = defaults.String(os.Getenv("GO_ENV"), "development") 16 var app *buffalo.App 17 18 // App is where all routes and middleware for buffalo 19 // should be defined. This is the nerve center of your 20 // application. 21 func App() *buffalo.App { 22 if app == nil { 23 app = buffalo.Automatic(buffalo.Options{ 24 Env: ENV, 25 }) 26 27 app.ServeFiles("/assets", assetsPath()) 28 app.Use(middleware.PopTransaction(models.DB)) 29 app.GET("/", func(c buffalo.Context) error { 30 return c.Redirect(http.StatusPermanentRedirect, "/users") 31 }) 32 33 g := app.Resource("/users", &UsersResource{}) 34 g.Use(findUserMW("user_id")) 35 36 g = app.Resource("/people", &UsersResource{}) 37 g.Use(findUserMW("person_id")) 38 } 39 40 return app 41 }