Environment (.env)
When it comes to creating a production-grade application, using the environment variable in the application is de facto.
Base on spf13/viper: Go configuration with fangs (github.com)
Why should we use the environment variable?
Suppose you have an application with many features and each feature needs to access the Database. You configured all the DB information like DBURL
, DBNAME
, USERNAME
and PASSWORD
in each feature.
There are a few major disadvantages to this approach, but there can be many.
Security Issue:
- You're entering all the information in the code. Now, all unauthorized person also has access to the DB.
- If you're using a code versioning tool
git
then the details of your DB will go public once you push the code.
Code Management:
- If you are changing a single variable then you have to change all the features. There is a high possibility that you'll miss one or two. 😌 been there
- You can categorize the environment variables like
PROD
,DEV
, orTEST
. Just prefix the variable with the environment.
At the start, it might look like some extra work, but this will reward you a lot in your project.
WARNING
Just don't forget to include your .env files in the .gitignore
.
It is time for some action.
Interface
go
type IENV interface {
Config() *ENVConfig
IsDev() bool
IsTest() bool
IsMock() bool
IsProd() bool
Bool(key string) bool
Int(key string) int
String(key string) string
All() map[string]string
}
Usage
1. Use from default ENV
go
// SomeFunc is a method of the `someService` type.
// It prints the hostname of the database configured in the ENV.
func (s someService) SomeFunc() {
// Retrieve the database hostname from the ENV configuration using the context
dbHost := s.ctx.ENV().Config().DBHost
// Print the database hostname
fmt.Println("ENV Database hostname: ", dbHost)
}
2. Create new ENV
2.1 Update .env file
env
CUSTOM_SOMETHING=custom_env
2.2 Add env key to constant file (consts/env.const.go)
go
const (
ENVCustomSomething = "CUSTOM_SOMETHING"
)
2.3 Use new ENV
go
// SomeFunc is a method of the `someService` type.
// It retrieves a custom environment variable called "ENVCustomSomething" and prints its value.
func (s someService) SomeFunc() {
// Retrieve the value of the custom environment variable "ENVCustomSomething"
somethingENV := s.ctx.ENV().String(consts.ENVCustomSomething)
// Print the value of the custom environment variable
fmt.Println("something value: ", somethingENV)
}
Default ENV Key
Application:
HOST
// Host for the applicationENV
// Environment (e.g.,dev
,prod
,mock
,test
)SERVICE
// Service name (e.g.,api
,seed
,cronjob
,migration
)
Logging:
LOG_LEVEL
// Log level (e.g.,debug
,info
,warn
,error
)LOG_HOST
// Host for logging service (graylog)LOG_PORT
// UDP Port for logging service (graylog)
Error Tracking:
SENTRY_DSN
// DSN for Sentry error tracking service
Database (Relational):
DB_DRIVER
// Database driver (e.g.,mysql
,postgres
,mssql
)DB_HOST
// Database hostDB_HOST
// Database hostDB_NAME
// Database nameDB_USER
// Database usernameDB_PASSWORD
// Database passwordDB_PORT
// Database port
Database (MongoDB):
DB_MONGO_HOST
// MongoDB hostDB_MONGO_NAME
// MongoDB database nameDB_MONGO_USERNAME
// MongoDB usernameDB_MONGO_PASSWORD
// MongoDB passwordDB_MONGO_PORT
// MongoDB port
Message Queue:
MQ_HOST
// Message Queue hostMQ_USER
// Message Queue usernameMQ_PASSWORD
// Message Queue passwordMQ_PORT
// Message Queue port
Cache Service (Redis):
CACHE_PORT
// Cache service portCACHE_HOST
// Cache service host
Amazon S3:
S3_ENDPOINT
// S3 endpointS3_ACCESS_KEY
// S3 access keyS3_SECRET_KEY
// S3 secret keyS3_BUCKET
// S3 bucket nameS3_HTTPS
// Use HTTPS for S3 (boolean value)S3_REGION
// S3 region
Email SMTP Server:
EMAIL_SERVER
// Email serverEMAIL_PORT
// Email server portEMAIL_USERNAME
// Email server usernameEMAIL_PASSWORD
// Email server passwordEMAIL_SENDER
// Email sender address
Windows Remote Management (WinRM):
WINRM_HOST
// Windows Remote Management (WinRM) hostWINRM_USER
// WinRM usernameWINRM_PASSWORD
// WinRM passwordWINRM_PORT
// WinRM port
ABCI:
ABCI_ENDPOINT
// ABCI endpoint
Decentralized Identifiers (DID):
DID_METHOD_DEFAULT
// Default DID methodDID_KEY_TYPE_DEFAULT
// Default DID key type