github.com/mayra-cabrera/buffalo@v0.9.4-0.20170814145312-66d2e7772f11/generators/newapp/templates/actions/app.go.tmpl (about)

     1  package actions
     2  
     3  import (
     4    "github.com/gobuffalo/buffalo"
     5    "github.com/gobuffalo/buffalo/middleware"
     6    {{ if .withPop }}
     7    "{{.modelsPath}}"
     8    {{ end }}
     9    "github.com/gobuffalo/envy"
    10  
    11    {{ if .asWeb -}}
    12    "github.com/gobuffalo/buffalo/middleware/csrf"
    13    "github.com/gobuffalo/buffalo/middleware/i18n"
    14    "github.com/gobuffalo/packr"
    15    {{ end }}
    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  var app *buffalo.App
    22  {{ if .asWeb -}}
    23  var T *i18n.Translator
    24  {{ end }}
    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  func App() *buffalo.App {
    30    if app == nil {
    31      app = buffalo.Automatic(buffalo.Options{
    32        Env: ENV,
    33        SessionName: "_{{.name}}_session",
    34      })
    35      // Automatically save the session if the underlying
    36      // Handler does not return an error.
    37      app.Use(middleware.SessionSaver)
    38  
    39      {{ if .asAPI -}}
    40      // Set the request content type to JSON
    41      app.Use(middleware.SetContentType("application/json"))
    42      {{ end }}
    43  
    44      if ENV == "development" {
    45        app.Use(middleware.ParameterLogger)
    46      }
    47  
    48      {{ if .asWeb -}}
    49      if ENV != "test" {
    50        // Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
    51        // Remove to disable this.
    52        app.Use(csrf.Middleware)
    53      }
    54      {{ end }}
    55  
    56      {{ if .withPop }}
    57      // Wraps each request in a transaction.
    58      //  c.Value("tx").(*pop.PopTransaction)
    59      // Remove to disable this.
    60      app.Use(middleware.PopTransaction(models.DB))
    61      {{ end }}
    62  
    63      {{ if .asWeb -}}
    64      // Setup and use translations:
    65      var err error
    66      if T, err = i18n.New(packr.NewBox("../locales"), "en-US"); err != nil {
    67        app.Stop(err)
    68      }
    69      app.Use(T.Middleware())
    70      {{ end }}
    71  
    72      app.GET("/", HomeHandler)
    73  
    74      {{ if .asWeb -}}
    75      app.ServeFiles("/assets", packr.NewBox("../public/assets"))
    76      {{ end -}}
    77    }
    78  
    79    return app
    80  }