package main import ( "log" "os" "strconv" "github.com/joho/godotenv" ) const ( SECURE = "SECURE" PASSWORD_HASH = "PASSWORD_HASH" ) type EnvVars struct { PasswordHash []byte // hashed passwd of the admin/only user. Secure bool // whether or not the frontend is served via HTTPs. } func LoadEnv() EnvVars { var env EnvVars var err error godotenv.Load() env.PasswordHash = []byte(os.Getenv(PASSWORD_HASH)) secureVar, found := os.LookupEnv(SECURE) if found { env.Secure, err = strconv.ParseBool(secureVar) if err != nil { panic("Invalid HTTPS value: Should be either 'true' or 'false'") } } else { env.Secure = false // assume false by default } return env } // reads and saves a single key to the .env func SaveKeyToEnv(key string, value string) { env, err := godotenv.Read(".env") if err != nil && !os.IsNotExist(err) { log.Fatal("Failed to read .env: " + err.Error()) } env[key] = value if err := godotenv.Write(env, ".env"); err != nil { log.Fatal(err) } }