github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/fix/_fixtures/buffaloPre0_18api/actions/app.go (about)

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