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

     1  package actions
     2  
     3  import (
     4  	"net/http"
     5  	"sync"
     6  
     7  	"github.com/markbates/coke/locales"
     8  	"github.com/markbates/coke/public"
     9  
    10  	"github.com/gobuffalo/buffalo"
    11  	"github.com/gobuffalo/envy"
    12  	"github.com/gobuffalo/middleware/csrf"
    13  	"github.com/gobuffalo/middleware/forcessl"
    14  	"github.com/gobuffalo/middleware/i18n"
    15  	"github.com/gobuffalo/middleware/paramlogger"
    16  	"github.com/unrolled/secure"
    17  )
    18  
    19  // ENV is used to help switch settings based on where the
    20  // application is being run. Default is "development".
    21  var ENV = envy.Get("GO_ENV", "development")
    22  
    23  var (
    24  	app     *buffalo.App
    25  	appOnce sync.Once
    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  	appOnce.Do(func() {
    44  		app = buffalo.New(buffalo.Options{
    45  			Env:         ENV,
    46  			SessionName: "_resource_session",
    47  		})
    48  
    49  		// Automatically redirect to SSL
    50  		app.Use(forceSSL())
    51  
    52  		// Log request parameters (filters apply).
    53  		app.Use(paramlogger.ParameterLogger)
    54  
    55  		// Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
    56  		// Remove to disable this.
    57  		app.Use(csrf.New)
    58  
    59  		// Setup and use translations:
    60  		app.Use(translations())
    61  
    62  		app.GET("/", HomeHandler)
    63  
    64  		app.Resource("/widgets", WidgetsResource{})
    65  		app.ServeFiles("/", http.FS(public.FS())) // serve files from the public directory
    66  	})
    67  
    68  	return app
    69  }
    70  
    71  // translations will load locale files, set up the translator `actions.T`,
    72  // and will return a middleware to use to load the correct locale for each
    73  // request.
    74  // for more information: https://gobuffalo.io/en/docs/localization
    75  func translations() buffalo.MiddlewareFunc {
    76  	var err error
    77  	if T, err = i18n.New(locales.FS(), "en-US"); err != nil {
    78  		app.Stop(err)
    79  	}
    80  	return T.Middleware()
    81  }
    82  
    83  // forceSSL will return a middleware that will redirect an incoming request
    84  // if it is not HTTPS. "http://example.com" => "https://example.com".
    85  // This middleware does **not** enable SSL. for your application. To do that
    86  // we recommend using a proxy: https://gobuffalo.io/en/docs/proxy
    87  // for more information: https://github.com/unrolled/secure/
    88  func forceSSL() buffalo.MiddlewareFunc {
    89  	return forcessl.Middleware(secure.Options{
    90  		SSLRedirect:     ENV == "production",
    91  		SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
    92  	})
    93  }