mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
9922265454
Former-commit-id: c563c4f1ffa98705829e14b189a6976c3a6aa898
51 lines
970 B
Go
51 lines
970 B
Go
package environment
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Available environments example.
|
|
const (
|
|
PROD Env = "production"
|
|
DEV Env = "development"
|
|
)
|
|
|
|
// Env is the environment type.
|
|
type Env string
|
|
|
|
// String just returns the string representation of the Env.
|
|
func (e Env) String() string {
|
|
return string(e)
|
|
}
|
|
|
|
// ReadEnv returns the environment of the system environment variable of "key".
|
|
// Returns the "def" if not found.
|
|
// Reports a panic message if the environment variable found
|
|
// but the Env is unknown.
|
|
func ReadEnv(key string, def Env) Env {
|
|
v := Getenv(key, def.String())
|
|
if v == "" {
|
|
return def
|
|
}
|
|
|
|
env := Env(strings.ToLower(v))
|
|
switch env {
|
|
case PROD, DEV: // allowed.
|
|
default:
|
|
panic("unexpected environment " + v)
|
|
}
|
|
|
|
return env
|
|
}
|
|
|
|
// Getenv returns the value of a system environment variable "key".
|
|
// Defaults to "def" if not found.
|
|
func Getenv(key string, def string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
|
|
return def
|
|
}
|