2024-04-23 07:15:38 -07:00
|
|
|
package services
|
2022-04-02 12:05:32 -07:00
|
|
|
|
|
|
|
import (
|
2022-06-30 14:54:58 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-04-02 12:05:32 -07:00
|
|
|
"log"
|
2022-06-30 14:54:58 -07:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2022-04-17 07:25:49 -07:00
|
|
|
|
2022-04-02 12:05:32 -07:00
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-07-14 09:59:55 -07:00
|
|
|
ServerAddress = "SERVER_ADDRESS"
|
2022-07-13 15:52:25 -07:00
|
|
|
|
2022-07-14 09:59:55 -07:00
|
|
|
Sql_Connection_String = "SQL_CONNECTION_STRING"
|
2022-04-02 12:05:32 -07:00
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
FEATURE_ENABLE_REDDIT_BACKEND = "FEATURE_ENABLE_REDDIT_BACKEND"
|
2022-07-13 15:52:25 -07:00
|
|
|
REDDIT_PULL_TOP = "REDDIT_PULL_TOP"
|
|
|
|
REDDIT_PULL_HOT = "REDDIT_PULL_HOT"
|
|
|
|
REDDIT_PULL_NSFW = "REDDIT_PULL_NSFW"
|
2022-04-17 07:25:49 -07:00
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
FEATURE_ENABLE_YOUTUBE_BACKEND = "FEATURE_ENABLE_YOUTUBE_BACKEND"
|
2022-07-13 15:52:25 -07:00
|
|
|
YOUTUBE_DEBUG = "YOUTUBE_DEBUG"
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
FEATURE_ENABLE_TWITCH_BACKEND = "FEATURE_ENABLE_TWITCH_BACKEND"
|
2022-07-13 15:52:25 -07:00
|
|
|
TWITCH_CLIENT_ID = "TWITCH_CLIENT_ID"
|
|
|
|
TWITCH_CLIENT_SECRET = "TWITCH_CLIENT_SECRET"
|
|
|
|
TWITCH_MONITOR_CLIPS = "TWITCH_MONITOR_CLIPS"
|
|
|
|
TWITCH_MONITOR_VOD = "TWITCH_MONITOR_VOD"
|
2022-06-30 14:54:58 -07:00
|
|
|
|
|
|
|
FEATURE_ENABLE_FFXIV_BACKEND = "FEATURE_ENABLE_FFXIV_BACKEND"
|
2022-04-02 12:05:32 -07:00
|
|
|
)
|
|
|
|
|
2024-04-26 16:05:17 -07:00
|
|
|
type Configs struct {
|
|
|
|
ServerAddress string
|
|
|
|
|
|
|
|
RedditEnabled bool
|
|
|
|
RedditPullTop bool
|
|
|
|
RedditPullHot bool
|
|
|
|
RedditPullNsfw bool
|
|
|
|
|
|
|
|
YoutubeEnabled bool
|
|
|
|
YoutubeDebug bool
|
|
|
|
|
|
|
|
TwitchEnabled bool
|
|
|
|
TwitchClientId string
|
|
|
|
TwitchClientSecret string
|
|
|
|
TwitchMonitorClips bool
|
|
|
|
TwitchMonitorVOD bool
|
|
|
|
|
|
|
|
FfxivEnabled bool
|
|
|
|
}
|
|
|
|
|
2022-07-13 15:52:25 -07:00
|
|
|
type ConfigClient struct{}
|
2022-04-02 12:05:32 -07:00
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
func NewConfig() ConfigClient {
|
2022-07-13 15:52:25 -07:00
|
|
|
c := ConfigClient{}
|
|
|
|
c.RefreshEnv()
|
2022-04-02 12:05:32 -07:00
|
|
|
|
2022-07-13 15:52:25 -07:00
|
|
|
return c
|
2022-04-02 12:05:32 -07:00
|
|
|
}
|
|
|
|
|
2024-04-26 16:05:17 -07:00
|
|
|
func GetEnvConfig() Configs {
|
|
|
|
return Configs{
|
|
|
|
ServerAddress: os.Getenv(ServerAddress),
|
|
|
|
|
|
|
|
RedditEnabled: processBoolConfig(os.Getenv(FEATURE_ENABLE_REDDIT_BACKEND)),
|
|
|
|
RedditPullTop: processBoolConfig(os.Getenv(REDDIT_PULL_TOP)),
|
|
|
|
RedditPullHot: processBoolConfig(os.Getenv(REDDIT_PULL_HOT)),
|
|
|
|
RedditPullNsfw: processBoolConfig(os.Getenv(REDDIT_PULL_NSFW)),
|
|
|
|
|
|
|
|
YoutubeEnabled: processBoolConfig(os.Getenv(FEATURE_ENABLE_YOUTUBE_BACKEND)),
|
|
|
|
YoutubeDebug: processBoolConfig(os.Getenv(YOUTUBE_DEBUG)),
|
|
|
|
|
|
|
|
TwitchEnabled: processBoolConfig(os.Getenv(FEATURE_ENABLE_TWITCH_BACKEND)),
|
|
|
|
TwitchClientId: os.Getenv(TWITCH_CLIENT_ID),
|
|
|
|
TwitchClientSecret: os.Getenv(TWITCH_CLIENT_SECRET),
|
|
|
|
TwitchMonitorClips: processBoolConfig(TWITCH_MONITOR_CLIPS),
|
|
|
|
TwitchMonitorVOD: processBoolConfig(os.Getenv(TWITCH_MONITOR_VOD)),
|
|
|
|
|
|
|
|
FfxivEnabled: processBoolConfig(os.Getenv(FEATURE_ENABLE_FFXIV_BACKEND)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will parse a string and convert it to a bool.
|
|
|
|
// If it runs into any errors, it will default to false
|
|
|
|
func processBoolConfig(value string) bool {
|
|
|
|
b, err := strconv.ParseBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2022-04-17 07:25:49 -07:00
|
|
|
func (cc *ConfigClient) GetConfig(key string) string {
|
2022-04-02 12:05:32 -07:00
|
|
|
res, filled := os.LookupEnv(key)
|
|
|
|
if !filled {
|
|
|
|
log.Printf("Missing the a value for '%v'. Could generate errors.", key)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:49:17 -08:00
|
|
|
// Looks for a value in the env and will panic if it does not exist.
|
|
|
|
func (c ConfigClient) MustGetString(key string) string {
|
|
|
|
res, filled := os.LookupEnv(key)
|
|
|
|
if !filled {
|
|
|
|
msg := fmt.Sprintf("No value was found for '%v'", key)
|
|
|
|
panic(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
func (cc *ConfigClient) GetFeature(flag string) (bool, error) {
|
|
|
|
cc.RefreshEnv()
|
|
|
|
|
|
|
|
res, filled := os.LookupEnv(flag)
|
|
|
|
if !filled {
|
|
|
|
errorMessage := fmt.Sprintf("'%v' was not found", flag)
|
|
|
|
return false, errors.New(errorMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := strconv.ParseBool(res)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2022-04-02 12:05:32 -07:00
|
|
|
// Use this when your ConfigClient has been opened for awhile and you want to ensure you have the most recent env changes.
|
2022-04-17 07:25:49 -07:00
|
|
|
func (cc *ConfigClient) RefreshEnv() {
|
2022-07-13 15:52:25 -07:00
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 12:05:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadEnvFile() {
|
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|