github.com/orderbynull/buffalo@v0.11.1/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 .opts.WithPop }}
    11    "{{.opts.ModelsPkg}}"
    12    {{ end -}}
    13  
    14    {{ if .opts.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 .opts.AsAPI -}}
    21    "github.com/rs/cors"
    22    "github.com/gobuffalo/x/sessions"
    23    {{ end -}}
    24  )
    25  
    26  // ENV is used to help switch settings based on where the
    27  // application is being run. Default is "development".
    28  var ENV = envy.Get("GO_ENV", "development")
    29  var app *buffalo.App
    30  {{ if .opts.AsWeb -}}
    31  var T *i18n.Translator
    32  {{ end }}
    33  
    34  // App is where all routes and middleware for buffalo
    35  // should be defined. This is the nerve center of your
    36  // application.
    37  func App() *buffalo.App {
    38    if app == nil {
    39      app = buffalo.New(buffalo.Options{
    40        Env: ENV,
    41        {{ if .opts.AsAPI -}}
    42        SessionStore: sessions.Null{},
    43        PreWares: []buffalo.PreWare{
    44          cors.Default().Handler,
    45        },
    46        {{ end -}}
    47        SessionName: "_{{.opts.Name.File}}_session",
    48      })
    49      // Automatically redirect to SSL
    50      app.Use(ssl.ForceSSL(secure.Options{
    51        SSLRedirect:     ENV == "production",
    52        SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
    53      }))
    54  
    55      {{ if .opts.AsAPI -}}
    56      // Set the request content type to JSON
    57      app.Use(middleware.SetContentType("application/json"))
    58      {{ end }}
    59  
    60      if ENV == "development" {
    61        app.Use(middleware.ParameterLogger)
    62      }
    63  
    64      {{ if .opts.AsWeb -}}
    65      // Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
    66      // Remove to disable this.
    67      app.Use(csrf.New)
    68      {{ end }}
    69  
    70      {{ if .opts.WithPop }}
    71      // Wraps each request in a transaction.
    72      //  c.Value("tx").(*pop.PopTransaction)
    73      // Remove to disable this.
    74      app.Use(middleware.PopTransaction(models.DB))
    75      {{ end }}
    76  
    77      {{ if .opts.AsWeb -}}
    78      // Setup and use translations:
    79      var err error
    80      if T, err = i18n.New(packr.NewBox("../locales"), "en-US"); err != nil {
    81        app.Stop(err)
    82      }
    83      app.Use(T.Middleware())
    84      {{ end }}
    85  
    86      app.GET("/", HomeHandler)
    87  
    88      {{ if .opts.AsWeb -}}
    89      app.ServeFiles("/", assetsBox) // serve files from the public directory
    90      {{ end -}}
    91    }
    92  
    93    return app
    94  }