github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/fix/_fixtures/buffaloPre0_18web/actions/app.go (about) 1 package actions 2 3 import ( 4 "coke/models" 5 6 "github.com/gobuffalo/buffalo" 7 "github.com/gobuffalo/envy" 8 forcessl "github.com/gobuffalo/mw-forcessl" 9 paramlogger "github.com/gobuffalo/mw-paramlogger" 10 "github.com/unrolled/secure" 11 12 "github.com/gobuffalo/buffalo-pop/v2/pop/popmw" 13 csrf "github.com/gobuffalo/mw-csrf" 14 i18n "github.com/gobuffalo/mw-i18n" 15 "github.com/gobuffalo/packr/v2" 16 ) 17 18 // ENV is used to help switch settings based on where the 19 // application is being run. Default is "development". 20 var ENV = envy.Get("GO_ENV", "development") 21 22 var ( 23 app *buffalo.App 24 T *i18n.Translator 25 ) 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 SessionName: "_coke_session", 45 }) 46 47 // Automatically redirect to SSL 48 app.Use(forceSSL()) 49 50 // Log request parameters (filters apply). 51 app.Use(paramlogger.ParameterLogger) 52 53 // Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF) 54 // Remove to disable this. 55 app.Use(csrf.New) 56 57 // Wraps each request in a transaction. 58 // c.Value("tx").(*pop.Connection) 59 // Remove to disable this. 60 app.Use(popmw.Transaction(models.DB)) 61 62 // Setup and use translations: 63 app.Use(translations()) 64 65 app.GET("/", HomeHandler) 66 67 app.ServeFiles("/", assetsBox) // serve files from the public directory 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 }