Fix private URL var name, add helper for retrieving various environment variables

This commit is contained in:
2023-12-24 22:07:44 -06:00
parent 37604e1eeb
commit 7e762ff2aa
2 changed files with 16 additions and 10 deletions

View File

@@ -5,6 +5,7 @@ import (
"io"
"math/rand"
"net/http"
"os"
"runtime"
"strconv"
"strings"
@@ -177,3 +178,12 @@ func (nt NaiveTime) String() string {
}
return fmt.Sprintf("%d:%02d%s", hour, nt.Minutes, meridiem)
}
func GetFirstEnv(key ...string) string {
for _, k := range key {
if v := os.Getenv(k); v != "" {
return v
}
}
return ""
}

16
main.go
View File

@@ -59,12 +59,9 @@ func init() {
ctx = context.Background()
// Try to grab the environment variable, or default to development
env := os.Getenv("ENVIRONMENT")
env := GetFirstEnv("ENVIRONMENT", "RAILWAY_ENVIRONMENT")
if env == "" {
env = os.Getenv("RAILWAY_ENVIRONMENT")
if env == "" {
env = "development"
}
env = "development"
}
// Use the custom console writer if we're in development
@@ -85,13 +82,12 @@ func init() {
func main() {
// Setup redis
redisUrl := os.Getenv("REDIS_URL")
redisUrl := GetFirstEnv("REDIS_URL", "REDIS_PRIVATE_URL")
if redisUrl == "" {
redisUrl = os.Getenv("PRIVATE_REDIS_URL")
if redisUrl == "" {
log.Fatal().Msg("REDIS_URL is not set (public or private)")
}
log.Fatal().Msg("REDIS_URL/REDIS_PRIVATE_URL not set")
}
// Parse URL and create client
options, err := redis.ParseURL(redisUrl)
if err != nil {
log.Fatal().Err(err).Msg("Cannot parse redis url")