github.com/rotblauer/buffalo@v0.7.1-0.20170112214545-7aa55ef80dd3/examples/json-crud/actions/app.go (about)

     1  package actions
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/gobuffalo/buffalo"
     7  	"github.com/gobuffalo/buffalo/examples/json-crud/models"
     8  	"github.com/gobuffalo/buffalo/middleware"
     9  	"github.com/markbates/going/defaults"
    10  )
    11  
    12  // ENV is used to help switch settings based on where the
    13  // application is being run. Default is "development".
    14  var ENV = defaults.String(os.Getenv("GO_ENV"), "development")
    15  var app *buffalo.App
    16  
    17  // App is where all routes and middleware for buffalo
    18  // should be defined. This is the nerve center of your
    19  // application.
    20  func App() *buffalo.App {
    21  	if app == nil {
    22  		app = buffalo.Automatic(buffalo.Options{
    23  			Env: ENV,
    24  		})
    25  
    26  		app.Use(middleware.SetContentType("application/json"))
    27  		app.Use(middleware.PopTransaction(models.DB))
    28  
    29  		app.Use(findUserMW)
    30  		app.GET("/users", UsersList)
    31  		app.GET("/users/{user_id}", UsersShow)
    32  		app.POST("/users", UsersCreate)
    33  		app.PUT("/users/{user_id}", UsersUpdate)
    34  		app.DELETE("/users/{user_id}", UsersDelete)
    35  	}
    36  
    37  	return app
    38  }