goshorly/utils/env.go

92 lines
1.6 KiB
Go
Raw Normal View History

2021-12-07 19:23:27 +00:00
package utils
import (
2022-01-25 16:25:23 +00:00
"fmt"
2021-12-07 19:23:27 +00:00
"log"
"os"
2022-03-24 08:08:12 +00:00
"github.com/joho/godotenv"
2021-12-07 19:23:27 +00:00
)
2021-12-08 13:11:35 +00:00
var (
HOST string
PORT string
HTTPS string
PROXY bool
URL string
REDIS_URI string
ESTATS string
2021-12-08 13:11:35 +00:00
)
2021-12-07 19:23:27 +00:00
2022-03-24 08:08:12 +00:00
func Init_env_file() {
err := godotenv.Load()
if err != nil {
fmt.Println("INFO: ", "Error loading .env file using environment variables")
}
}
2021-12-08 10:04:28 +00:00
func Init_env_vars() {
2022-01-25 16:25:23 +00:00
fmt.Println("-- Initializing environment variables... --")
2021-12-08 13:11:35 +00:00
2021-12-07 19:23:27 +00:00
UHOST, err := os.LookupEnv("HOST")
if !err {
log.Fatal("HOST enviroment variable not found, please set it!")
}
HOST = UHOST
2022-01-25 16:25:23 +00:00
fmt.Println("HOST: ", HOST)
2021-12-07 19:23:27 +00:00
UHTTPS, _ := os.LookupEnv("HTTPS")
if UHTTPS != "true" {
HTTPS = "http"
} else {
HTTPS = "https"
}
2022-01-25 16:25:23 +00:00
fmt.Println("Proto: ", HTTPS)
2021-12-07 19:23:27 +00:00
UPROXY, _ := os.LookupEnv("PROXY")
if UPROXY != "true" {
PROXY = false
} else {
PROXY = true
}
2022-01-25 16:25:23 +00:00
fmt.Println("Own reverse proxy: ", PROXY)
2021-12-07 19:23:27 +00:00
UPORT, err := os.LookupEnv("PORT")
if !err {
PORT = "3000"
} else {
PORT = UPORT
}
2022-01-25 16:25:23 +00:00
fmt.Println("Port: ", PORT)
2021-12-07 19:23:27 +00:00
2022-01-25 16:25:23 +00:00
UREDIS_URI, err := os.LookupEnv("REDIS_URI")
if !err {
REDIS_URI = "redis:6379"
} else {
REDIS_URI = UREDIS_URI
2021-12-08 13:11:35 +00:00
}
2022-01-25 16:25:23 +00:00
fmt.Println("Redis URI: ", REDIS_URI)
2021-12-08 13:11:35 +00:00
UESTATS, _ := os.LookupEnv("ENABLE_STATS")
if UESTATS != "false" {
ESTATS = "true"
} else {
ESTATS = "false"
}
2022-01-25 16:25:23 +00:00
fmt.Println("Stats enabled: ", ESTATS)
2021-12-07 19:23:27 +00:00
create_string()
}
func create_string() {
if !PROXY {
URL = HTTPS + "://" + HOST + ":" + PORT + "/"
} else {
URL = HTTPS + "://" + HOST + "/"
}
2022-01-25 16:25:23 +00:00
fmt.Println("URL: ", URL)
fmt.Println("-- Environment variables initialized! / Starting Webserver --")
2021-12-07 19:23:27 +00:00
}