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

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