43 lines
725 B
Go
43 lines
725 B
Go
|
package config
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func (c *Config) ReadEnv() (err error) {
|
||
|
|
||
|
// General Configuration
|
||
|
|
||
|
stringVar(&c.BaseUrl, "BASE_URL")
|
||
|
stringVar(&c.SavePath, "SAVE_PATH")
|
||
|
intVar(&c.Port, "PORT")
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func stringVar(x *string, key string) {
|
||
|
if v := os.Getenv(key); v != "" {
|
||
|
*x = v
|
||
|
}
|
||
|
}
|
||
|
func float64Var(x *float64, key string) (err error) {
|
||
|
if v := os.Getenv(key); v != "" {
|
||
|
*x, err = strconv.ParseFloat(v, 64)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
func intVar(x *int, key string) (err error) {
|
||
|
if v := os.Getenv(key); v != "" {
|
||
|
*x, err = strconv.Atoi(v)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
func durationVar(x *time.Duration, key string) (err error) {
|
||
|
if v := os.Getenv(key); v != "" {
|
||
|
*x, err = time.ParseDuration(v)
|
||
|
}
|
||
|
return
|
||
|
}
|