bingobot blindly uses the shell's working dir to write logs and read configs. This change introduces the following changes: 1. start.sh now determines the project root's absolute path before reading the token file and starting the app. 2. the config package now determine's the executable's working directory (rather than assuming it's the same as the shell's) before loading configs. It also now exposes this for use in other packages. 3. the logging package now uses config.GetWorkingDir() to determine where to write logs.
105 lines
2.4 KiB
Go
105 lines
2.4 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type AppConfig struct {
|
|
LogFile string `yaml:"log_file"`
|
|
LogDir string `yaml:"log_dir"`
|
|
LogMaxSizeMB int `yaml:"log_max_size_mb"`
|
|
LogMaxBackups int `yaml:"log_max_backups"`
|
|
LogMaxAgeDays int `yaml:"log_max_age_days"`
|
|
LogCompression bool `yaml:"log_compression"`
|
|
LogAddSource bool `yaml:"log_add_source"`
|
|
|
|
/*
|
|
how long (in seconds) a user needs to be in vc in order to generate a
|
|
UserActive event
|
|
*/
|
|
VoiceActivityThresholdSeconds int `yaml:"voice_activity_threshold_seconds"`
|
|
|
|
/*
|
|
how long (in milliseconds) a voice activity timer sleeps at a time between
|
|
context cancellation checks.
|
|
|
|
a higher value means the function sleeps longer which could be
|
|
useful for some reason in the future
|
|
|
|
a higher value also means that the timer could take longer to cancel.
|
|
|
|
current recommended value is 1000ms.
|
|
*/
|
|
VoiceActivityTimerSleepIntervalMillis int `yaml:"voice_activity_timer_sleep_interval_millis"`
|
|
}
|
|
|
|
var config *AppConfig
|
|
var workingDir string
|
|
|
|
func init() {
|
|
setDefaults()
|
|
viper.Unmarshal(&config)
|
|
|
|
ex, err := os.Executable()
|
|
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to get path of executable (self): %v", err))
|
|
}
|
|
|
|
workingDir = filepath.Dir(ex)
|
|
}
|
|
|
|
func Get() *AppConfig {
|
|
return config
|
|
}
|
|
|
|
func GetWorkingDir() string {
|
|
return workingDir
|
|
}
|
|
|
|
func GetDefaultConfig() *AppConfig {
|
|
var config *AppConfig
|
|
setDefaults()
|
|
viper.Unmarshal(&config)
|
|
return config
|
|
}
|
|
|
|
func Init() error {
|
|
setDefaults()
|
|
|
|
// automatically populate config with any matching environment vars
|
|
viper.SetEnvPrefix("BB")
|
|
viper.AutomaticEnv()
|
|
|
|
viper.SetConfigName("config")
|
|
viper.SetConfigType("yaml")
|
|
viper.AddConfigPath(workingDir)
|
|
|
|
err := viper.ReadInConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("error reading configs: %s", err)
|
|
}
|
|
|
|
err = viper.Unmarshal(&config)
|
|
if err != nil {
|
|
return fmt.Errorf("error unmarshaling configs: %s", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func setDefaults() {
|
|
viper.SetDefault("LogFile", "bingobot.log")
|
|
viper.SetDefault("LogDir", "log")
|
|
viper.SetDefault("LogMaxSizeMB", 500)
|
|
viper.SetDefault("LogMaxBackups", 3)
|
|
viper.SetDefault("LogMaxAgeDays", 365)
|
|
viper.SetDefault("LogCompression", false)
|
|
viper.SetDefault("LogAddSource", true)
|
|
viper.SetDefault("VoiceActivityThresholdSeconds", 600)
|
|
viper.SetDefault("VoiceActivityTimerSleepIntervalMillis", 1000)
|
|
}
|