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