2020-10-16 19:12:53 +02:00
|
|
|
package config
|
2020-10-08 18:21:27 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2020-10-09 10:27:20 +02:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2020-10-08 18:21:27 +02:00
|
|
|
)
|
|
|
|
|
2020-10-19 14:55:20 +02:00
|
|
|
const (
|
|
|
|
DefaultServerRoot = "http://localhost:8000"
|
|
|
|
DefaultPort = 8000
|
|
|
|
)
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// Configuration is the app configuration stored in a json file.
|
2020-10-08 18:21:27 +02:00
|
|
|
type Configuration struct {
|
2020-12-07 17:04:35 +01:00
|
|
|
ServerRoot string `json:"serverRoot" mapstructure:"serverRoot"`
|
|
|
|
Port int `json:"port" mapstructure:"port"`
|
|
|
|
DBType string `json:"dbtype" mapstructure:"dbtype"`
|
|
|
|
DBConfigString string `json:"dbconfig" mapstructure:"dbconfig"`
|
|
|
|
UseSSL bool `json:"useSSL" mapstructure:"useSSL"`
|
|
|
|
WebPath string `json:"webpath" mapstructure:"webpath"`
|
|
|
|
FilesPath string `json:"filespath" mapstructure:"filespath"`
|
|
|
|
Telemetry bool `json:"telemetry" mapstructure:"telemetry"`
|
|
|
|
WebhookUpdate []string `json:"webhook_update" mapstructure:"webhook_update"`
|
|
|
|
Secret string `json:"secret" mapstructure:"secret"`
|
|
|
|
SessionExpireTime int64 `json:"session_expire_time" mapstructure:"session_expire_time"`
|
|
|
|
SessionRefreshTime int64 `json:"session_refresh_time" mapstructure:"session_refresh_time"`
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// ReadConfigFile read the configuration from the filesystem.
|
2020-10-16 19:12:53 +02:00
|
|
|
func ReadConfigFile() (*Configuration, error) {
|
2020-10-09 10:27:20 +02:00
|
|
|
viper.SetConfigName("config") // name of config file (without extension)
|
|
|
|
viper.SetConfigType("json") // REQUIRED if the config file does not have the extension in the name
|
|
|
|
viper.AddConfigPath(".") // optionally look for config in the working directory
|
2020-10-19 14:55:20 +02:00
|
|
|
viper.SetDefault("ServerRoot", DefaultServerRoot)
|
|
|
|
viper.SetDefault("Port", DefaultPort)
|
2020-10-09 10:27:20 +02:00
|
|
|
viper.SetDefault("DBType", "sqlite3")
|
|
|
|
viper.SetDefault("DBConfigString", "./octo.db")
|
|
|
|
viper.SetDefault("WebPath", "./pack")
|
|
|
|
viper.SetDefault("FilesPath", "./files")
|
2020-10-29 21:09:02 +01:00
|
|
|
viper.SetDefault("WebhookUpdate", nil)
|
2020-12-07 17:04:35 +01:00
|
|
|
viper.SetDefault("SessionExpireTime", 60*60*24*30) // 30 days session lifetime
|
|
|
|
viper.SetDefault("SessionRefreshTime", 60*60*5) // 5 minutes session refresh
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-09 10:27:20 +02:00
|
|
|
err := viper.ReadInConfig() // Find and read the config file
|
|
|
|
if err != nil { // Handle errors reading the config file
|
|
|
|
return nil, err
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 10:27:20 +02:00
|
|
|
configuration := Configuration{}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-09 10:28:39 +02:00
|
|
|
err = viper.Unmarshal(&configuration)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-08 18:21:27 +02:00
|
|
|
|
|
|
|
log.Println("readConfigFile")
|
|
|
|
log.Printf("%+v", configuration)
|
|
|
|
|
2020-10-09 10:27:20 +02:00
|
|
|
return &configuration, nil
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|