github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/newapp/web/templates/actions/app.go.tmpl (about) 1 package actions 2 3 import ( 4 "net/http" 5 "sync" 6 7 "{{ .opts.App.PackagePkg }}/locales" 8 {{ if .opts.App.WithPop -}} 9 "{{ .opts.App.ModelsPkg }}" 10 {{ end -}} 11 "{{ .opts.App.PackagePkg }}/public" 12 13 "github.com/gobuffalo/buffalo" 14 {{ if .opts.App.WithPop -}} 15 "github.com/gobuffalo/buffalo-pop/v3/pop/popmw" 16 {{ end -}} 17 "github.com/gobuffalo/envy" 18 "github.com/gobuffalo/middleware/csrf" 19 "github.com/gobuffalo/middleware/forcessl" 20 "github.com/gobuffalo/middleware/i18n" 21 "github.com/gobuffalo/middleware/paramlogger" 22 "github.com/unrolled/secure" 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 29 var ( 30 app *buffalo.App 31 appOnce sync.Once 32 T *i18n.Translator 33 ) 34 35 // App is where all routes and middleware for buffalo 36 // should be defined. This is the nerve center of your 37 // application. 38 // 39 // Routing, middleware, groups, etc... are declared TOP -> DOWN. 40 // This means if you add a middleware to `app` *after* declaring a 41 // group, that group will NOT have that new middleware. The same 42 // is true of resource declarations as well. 43 // 44 // It also means that routes are checked in the order they are declared. 45 // `ServeFiles` is a CATCH-ALL route, so it should always be 46 // placed last in the route declarations, as it will prevent routes 47 // declared after it to never be called. 48 func App() *buffalo.App { 49 appOnce.Do(func() { 50 app = buffalo.New(buffalo.Options{ 51 Env: ENV, 52 SessionName: "_{{.opts.App.Name.File}}_session", 53 }) 54 55 // Automatically redirect to SSL 56 app.Use(forceSSL()) 57 58 // Log request parameters (filters apply). 59 app.Use(paramlogger.ParameterLogger) 60 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 65 {{ if .opts.App.WithPop -}} 66 // Wraps each request in a transaction. 67 // c.Value("tx").(*pop.Connection) 68 // Remove to disable this. 69 app.Use(popmw.Transaction(models.DB)) 70 {{ end -}} 71 72 // Setup and use translations: 73 app.Use(translations()) 74 75 app.GET("/", HomeHandler) 76 77 app.ServeFiles("/", http.FS(public.FS())) // serve files from the public directory 78 }) 79 80 return app 81 } 82 83 // translations will load locale files, set up the translator `actions.T`, 84 // and will return a middleware to use to load the correct locale for each 85 // request. 86 // for more information: https://gobuffalo.io/en/docs/localization 87 func translations() buffalo.MiddlewareFunc { 88 var err error 89 if T, err = i18n.New(locales.FS(), "en-US"); err != nil { 90 app.Stop(err) 91 } 92 return T.Middleware() 93 } 94 95 // forceSSL will return a middleware that will redirect an incoming request 96 // if it is not HTTPS. "http://example.com" => "https://example.com". 97 // This middleware does **not** enable SSL. for your application. To do that 98 // we recommend using a proxy: https://gobuffalo.io/en/docs/proxy 99 // for more information: https://github.com/unrolled/secure/ 100 func forceSSL() buffalo.MiddlewareFunc { 101 return forcessl.Middleware(secure.Options{ 102 SSLRedirect: ENV == "production", 103 SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, 104 }) 105 }