github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/newapp/core/templates/actions/app.go.tmpl (about)

     1  package actions
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/gobuffalo/buffalo"
     7  	"github.com/gobuffalo/envy"
     8  	"github.com/gobuffalo/middleware/forcessl"
     9  	"github.com/gobuffalo/middleware/paramlogger"
    10  	"github.com/unrolled/secure"
    11  )
    12  
    13  // ENV is used to help switch settings based on where the
    14  // application is being run. Default is "development".
    15  var ENV = envy.Get("GO_ENV", "development")
    16  
    17  var (
    18  	app     *buffalo.App
    19  	appOnce sync.Once
    20  )
    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 ensure routes
    34  // declared after it will never be called.
    35  func App() *buffalo.App {
    36  	appOnce.Do(func() {
    37  		app = buffalo.New(buffalo.Options{
    38  			Env:         ENV,
    39  			SessionName: "_{{.opts.App.Name.File}}_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  		app.GET("/", HomeHandler)
    49  	})
    50  
    51  	return app
    52  }
    53  
    54  // forceSSL will return a middleware that will redirect an incoming request
    55  // if it is not HTTPS. "http://example.com" => "https://example.com".
    56  // This middleware does **not** enable SSL. for your application. To do that
    57  // we recommend using a proxy: https://gobuffalo.io/en/docs/proxy
    58  // for more information: https://github.com/unrolled/secure/
    59  func forceSSL() buffalo.MiddlewareFunc {
    60  	return forcessl.Middleware(secure.Options{
    61  		SSLRedirect:     ENV == "production",
    62  		SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
    63  	})
    64  }