2024-05-11 09:56:19 -07:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Configs struct {
|
2024-07-07 07:57:23 -07:00
|
|
|
ServerAddress string
|
|
|
|
JwtSecret string
|
|
|
|
ApiServerAddress string
|
2024-05-11 09:56:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func New() Configs {
|
|
|
|
refreshEnv()
|
|
|
|
c := getEnvConfig()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEnvConfig() Configs {
|
|
|
|
return Configs{
|
2024-07-07 07:57:23 -07:00
|
|
|
ServerAddress: os.Getenv("ServerAddress"),
|
|
|
|
JwtSecret: os.Getenv("JwtSecret"),
|
|
|
|
ApiServerAddress: os.Getenv("ApiServerAddress"),
|
2024-05-11 09:56:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|