github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/build/_fixtures/coke/actions/app.go (about)

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