github.com/bketelsen/buffalo@v0.9.5/generators/newapp/templates/actions/app.go.tmpl (about) 1 package actions 2 3 import ( 4 "github.com/gobuffalo/envy" 5 "github.com/gobuffalo/buffalo" 6 "github.com/gobuffalo/buffalo/middleware" 7 "github.com/gobuffalo/buffalo/middleware/ssl" 8 "github.com/unrolled/secure" 9 10 {{ if .withPop }} 11 "{{.modelsPath}}" 12 {{ end -}} 13 14 {{ if .asWeb -}} 15 "github.com/gobuffalo/buffalo/middleware/csrf" 16 "github.com/gobuffalo/buffalo/middleware/i18n" 17 "github.com/gobuffalo/packr" 18 {{ end -}} 19 20 {{ if .asAPI -}} 21 "github.com/gobuffalo/x/sessions" 22 {{ end -}} 23 ) 24 25 // ENV is used to help switch settings based on where the 26 // application is being run. Default is "development". 27 var ENV = envy.Get("GO_ENV", "development") 28 var app *buffalo.App 29 {{ if .asWeb -}} 30 var T *i18n.Translator 31 {{ end }} 32 33 // App is where all routes and middleware for buffalo 34 // should be defined. This is the nerve center of your 35 // application. 36 func App() *buffalo.App { 37 if app == nil { 38 app = buffalo.New(buffalo.Options{ 39 Env: ENV, 40 {{ if .asAPI -}} 41 SessionStore: sessions.Null{}, 42 {{ end -}} 43 SessionName: "_{{.name}}_session", 44 }) 45 // Automatically redirect to SSL 46 app.Use(ssl.ForceSSL(secure.Options{ 47 SSLRedirect: ENV == "production", 48 SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, 49 })) 50 51 {{ if .asAPI -}} 52 // Set the request content type to JSON 53 app.Use(middleware.SetContentType("application/json")) 54 {{ end }} 55 56 if ENV == "development" { 57 app.Use(middleware.ParameterLogger) 58 } 59 60 {{ if .asWeb -}} 61 // Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF) 62 // Remove to disable this. 63 app.Use(csrf.New) 64 {{ end }} 65 66 {{ if .withPop }} 67 // Wraps each request in a transaction. 68 // c.Value("tx").(*pop.PopTransaction) 69 // Remove to disable this. 70 app.Use(middleware.PopTransaction(models.DB)) 71 {{ end }} 72 73 {{ if .asWeb -}} 74 // Setup and use translations: 75 var err error 76 if T, err = i18n.New(packr.NewBox("../locales"), "en-US"); err != nil { 77 app.Stop(err) 78 } 79 app.Use(T.Middleware()) 80 {{ end }} 81 82 app.GET("/", HomeHandler) 83 84 {{ if .asWeb -}} 85 app.ServeFiles("/assets", assetsBox) 86 {{ end -}} 87 } 88 89 return app 90 }