In this chapter, you'll learn how to manage configuration with Buffalo.
Environment variables are a good way to separate environment specific values, or secrets, from your application code base (as described in The Twelve Factor app). It can help define behavior that is based on the context of the app (as requiring SSL on production) and isolate secrets from your code base. This way, all developers don't have to know the productions keys to sensitive services, such as a bank API, and they can use sandbox API keys.
The following variables are used by Buffalo:
||The "environment" that Buffalo is run in|
||The Go compiler to use|
||Where Buffalo looks for plugins|
||Which address to bind the server to|
||Which port to bind the server to|
||The "URL" of the application (i.e. what end users type in)|
||A salt used for securing sessions|
You still can provide your own variables, and retrieve them from within your application. The envy package makes it easy!
import "github.com/gobuffalo/envy" // [...] // Get MYSECRET env variable, default to empty string if it's not set var MYSECRET = envy.Get("MYSECRET", "") // Get REQUIREDSECRET env variable, return an error if it's not set REQUIREDSECRET, err := envy.MustGet("REQUIREDSECRET")
Buffalo ships with
.env support (since buffalo >= 0.10.3), meaning buffalo will load
.env files into environment variables once the application starts. To do it, Buffalo uses
envy.Load which will look for
.env file at the root of your app.
If you're not familiar with how a
.env file looks, here is an example:
SENDGRID_API_KEY=ABCCOQ7GFRVCW0ODHPFQ3FTP5SLL1Q SENDGRID_EMAILfirstname.lastname@example.org APP_DEBUG=true APP_LOG_LEVEL=debug APP_URL=https://myapp.com
Generated apps (with buffalo >= 0.10.3) will also create a default
.env file in your application root. This file will be watched by Buffalo for changes, but will be ignored by git (added in the
.gitignore). This is a good way to prevent developers to push credentials by mistake.