50 lines
845 B
Go
50 lines
845 B
Go
|
package config
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"os"
|
||
|
|
||
|
"github.com/joho/godotenv"
|
||
|
)
|
||
|
|
||
|
type Configs struct {
|
||
|
ServerAddress string
|
||
|
JwtSecret string
|
||
|
AdminSecret string
|
||
|
}
|
||
|
|
||
|
func New() Configs {
|
||
|
refreshEnv()
|
||
|
c := getEnvConfig()
|
||
|
return c
|
||
|
}
|
||
|
|
||
|
func getEnvConfig() Configs {
|
||
|
return Configs{
|
||
|
ServerAddress: os.Getenv("ServerAddress"),
|
||
|
JwtSecret: os.Getenv("JwtSecret"),
|
||
|
AdminSecret: os.Getenv("AdminSecret"),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Use this when your ConfigClient has been opened for awhile and you want to ensure you have the most recent env changes.
|
||
|
func refreshEnv() {
|
||
|
// Check to see if we have the env file on the system
|
||
|
_, err := os.Stat(".env")
|
||
|
|
||
|
// We have the file, load it.
|
||
|
if err == nil {
|
||
|
_, err := os.Open(".env")
|
||
|
if err == nil {
|
||
|
loadEnvFile()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func loadEnvFile() {
|
||
|
err := godotenv.Load()
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
}
|