github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/newapp/api/templates/actions/app.go.tmpl (about) 1 package actions 2 3 import ( 4 "github.com/gobuffalo/envy" 5 "github.com/gobuffalo/buffalo" 6 forcessl "github.com/gobuffalo/mw-forcessl" 7 paramlogger "github.com/gobuffalo/mw-paramlogger" 8 "github.com/unrolled/secure" 9 i18n "github.com/gobuffalo/mw-i18n" 10 11 {{ if .opts.App.WithPop }} 12 "{{.opts.App.ModelsPkg}}" 13 "github.com/gobuffalo/buffalo-pop/v2/pop/popmw" 14 {{ end -}} 15 16 "github.com/rs/cors" 17 "github.com/gobuffalo/x/sessions" 18 contenttype "github.com/gobuffalo/mw-contenttype" 19 ) 20 21 // ENV is used to help switch settings based on where the 22 // application is being run. Default is "development". 23 var ENV = envy.Get("GO_ENV", "development") 24 var app *buffalo.App 25 var T *i18n.Translator 26 27 // App is where all routes and middleware for buffalo 28 // should be defined. This is the nerve center of your 29 // application. 30 // 31 // Routing, middleware, groups, etc... are declared TOP -> DOWN. 32 // This means if you add a middleware to `app` *after* declaring a 33 // group, that group will NOT have that new middleware. The same 34 // is true of resource declarations as well. 35 // 36 // It also means that routes are checked in the order they are declared. 37 // `ServeFiles` is a CATCH-ALL route, so it should always be 38 // placed last in the route declarations, as it will prevent routes 39 // declared after it to never be called. 40 func App() *buffalo.App { 41 if app == nil { 42 app = buffalo.New(buffalo.Options{ 43 Env: ENV, 44 SessionStore: sessions.Null{}, 45 PreWares: []buffalo.PreWare{ 46 cors.Default().Handler, 47 }, 48 SessionName: "_{{.opts.App.Name.File}}_session", 49 }) 50 51 // Automatically redirect to SSL 52 app.Use(forceSSL()) 53 54 // Log request parameters (filters apply). 55 app.Use(paramlogger.ParameterLogger) 56 57 // Set the request content type to JSON 58 app.Use(contenttype.Set("application/json")) 59 60 {{ if .opts.App.WithPop }} 61 // Wraps each request in a transaction. 62 // c.Value("tx").(*pop.Connection) 63 // Remove to disable this. 64 app.Use(popmw.Transaction(models.DB)) 65 {{ end }} 66 67 app.GET("/", HomeHandler) 68 } 69 70 return app 71 } 72 73 // translations will load locale files, set up the translator `actions.T`, 74 // and will return a middleware to use to load the correct locale for each 75 // request. 76 // for more information: https://gobuffalo.io/en/docs/localization 77 func translations() buffalo.MiddlewareFunc { 78 var err error 79 if T, err = i18n.New(packr.New("app:locales", "../locales"), "en-US"); err != nil { 80 app.Stop(err) 81 } 82 return T.Middleware() 83 } 84 85 // forceSSL will return a middleware that will redirect an incoming request 86 // if it is not HTTPS. "http://example.com" => "https://example.com". 87 // This middleware does **not** enable SSL. for your application. To do that 88 // we recommend using a proxy: https://gobuffalo.io/en/docs/proxy 89 // for more information: https://github.com/unrolled/secure/ 90 func forceSSL() buffalo.MiddlewareFunc { 91 return forcessl.Middleware(secure.Options{ 92 SSLRedirect: ENV == "production", 93 SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, 94 }) 95 }