github.com/jasonish/buffalo@v0.8.2-0.20170413145823-bacbdd415f1b/examples/html-crud/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-crud/models"
     9  	"github.com/gobuffalo/buffalo/middleware"
    10  	"github.com/gobuffalo/packr"
    11  	"github.com/markbates/going/defaults"
    12  )
    13  
    14  // ENV is used to help switch settings based on where the
    15  // application is being run. Default is "development".
    16  var ENV = defaults.String(os.Getenv("GO_ENV"), "development")
    17  var app *buffalo.App
    18  
    19  // App is where all routes and middleware for buffalo
    20  // should be defined. This is the nerve center of your
    21  // application.
    22  func App() *buffalo.App {
    23  	if app == nil {
    24  		app = buffalo.Automatic(buffalo.Options{
    25  			Env: ENV,
    26  		})
    27  
    28  		app.ServeFiles("/assets", packr.NewBox("../assets"))
    29  		app.Use(middleware.PopTransaction(models.DB))
    30  		app.GET("/", func(c buffalo.Context) error {
    31  			return c.Redirect(http.StatusPermanentRedirect, "/users")
    32  		})
    33  
    34  		g := app.Group("/users")
    35  		g.Use(findUserMW)
    36  		g.GET("/", UsersList)
    37  		g.GET("/new", UsersNew)
    38  		g.GET("/{user_id}", UsersShow)
    39  		g.GET("/{user_id}/edit", UsersEdit)
    40  		g.POST("/", UsersCreate)
    41  		g.PUT("/{user_id}", UsersUpdate)
    42  		g.DELETE("/{user_id}", UsersDelete)
    43  	}
    44  
    45  	return app
    46  }