github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/newapp/core/templates/actions/app.go.tmpl (about)

     1  package actions
     2  
     3  import (
     4  	"github.com/gobuffalo/buffalo"
     5  	"github.com/gobuffalo/envy"
     6  	forcessl "github.com/gobuffalo/mw-forcessl"
     7  	paramlogger "github.com/gobuffalo/mw-paramlogger"
     8  	"github.com/unrolled/secure"
     9  )
    10  
    11  // ENV is used to help switch settings based on where the
    12  // application is being run. Default is "development".
    13  var ENV = envy.Get("GO_ENV", "development")
    14  var app *buffalo.App
    15  
    16  // App is where all routes and middleware for buffalo
    17  // should be defined. This is the nerve center of your
    18  // application.
    19  //
    20  // Routing, middleware, groups, etc... are declared TOP -> DOWN.
    21  // This means if you add a middleware to `app` *after* declaring a
    22  // group, that group will NOT have that new middleware. The same
    23  // is true of resource declarations as well.
    24  //
    25  // It also means that routes are checked in the order they are declared.
    26  // `ServeFiles` is a CATCH-ALL route, so it should always be
    27  // placed last in the route declarations, as it will ensure routes
    28  // declared after it will never be called.
    29  func App() *buffalo.App {
    30  	if app == nil {
    31  		app = buffalo.New(buffalo.Options{
    32  			Env:         ENV,
    33        SessionName: "_{{.opts.App.Name.File}}_session",
    34  		})
    35  
    36  		// Automatically redirect to SSL
    37  		app.Use(forceSSL())
    38  
    39  		// Log request parameters (filters apply).
    40  		app.Use(paramlogger.ParameterLogger)
    41  
    42  		app.GET("/", HomeHandler)
    43  	}
    44  
    45  	return app
    46  }
    47  
    48  // forceSSL will return a middleware that will redirect an incoming request
    49  // if it is not HTTPS. "http://example.com" => "https://example.com".
    50  // This middleware does **not** enable SSL. for your application. To do that
    51  // we recommend using a proxy: https://gobuffalo.io/en/docs/proxy
    52  // for more information: https://github.com/unrolled/secure/
    53  func forceSSL() buffalo.MiddlewareFunc {
    54  	return forcessl.Middleware(secure.Options{
    55  		SSLRedirect:     ENV == "production",
    56  		SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
    57  	})
    58  }