2018-11-26 21:17:33 +01:00
|
|
|
// Vikunja is a todo-list application to facilitate your life.
|
|
|
|
// Copyright 2018 Vikunja and contributors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2018-10-31 13:42:38 +01:00
|
|
|
package config
|
2018-06-10 11:11:41 +02:00
|
|
|
|
|
|
|
import (
|
2018-09-08 13:29:35 +02:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
2019-07-06 22:12:26 +02:00
|
|
|
"log"
|
2018-11-02 11:01:28 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-09-08 13:29:35 +02:00
|
|
|
"strings"
|
2019-07-06 22:12:26 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Key is used as a config key
|
|
|
|
type Key string
|
|
|
|
|
|
|
|
// These constants hold all config value keys
|
|
|
|
const (
|
|
|
|
ServiceJWTSecret Key = `service.JWTSecret`
|
|
|
|
ServiceInterface Key = `service.interface`
|
|
|
|
ServiceFrontendurl Key = `service.frontendurl`
|
|
|
|
ServiceEnableCaldav Key = `service.enablecaldav`
|
|
|
|
ServiceRootpath Key = `service.rootpath`
|
|
|
|
ServicePageCount Key = `service.pagecount`
|
|
|
|
ServiceEnableMetrics Key = `service.enablemetrics`
|
2019-07-16 00:54:38 +02:00
|
|
|
ServiceMotd Key = `service.motd`
|
2019-07-06 22:12:26 +02:00
|
|
|
|
|
|
|
DatabaseType Key = `database.type`
|
|
|
|
DatabaseHost Key = `database.host`
|
|
|
|
DatabaseUser Key = `database.user`
|
|
|
|
DatabasePassword Key = `database.password`
|
|
|
|
DatabaseDatabase Key = `database.database`
|
|
|
|
DatabasePath Key = `database.path`
|
|
|
|
DatabaseMaxOpenConnections Key = `database.maxopenconnections`
|
|
|
|
DatabaseMaxIdleConnections Key = `database.maxidleconnections`
|
|
|
|
DatabaseMaxConnectionLifetime Key = `database.maxconnectionlifetime`
|
|
|
|
|
|
|
|
CacheEnabled Key = `cache.enabled`
|
|
|
|
CacheType Key = `cache.type`
|
|
|
|
CacheMaxElementSize Key = `cache.maxelementsize`
|
|
|
|
|
|
|
|
MailerEnabled Key = `mailer.enabled`
|
|
|
|
MailerHost Key = `mailer.host`
|
|
|
|
MailerPort Key = `mailer.port`
|
|
|
|
MailerUsername Key = `mailer.username`
|
|
|
|
MailerPassword Key = `mailer.password`
|
|
|
|
MailerSkipTLSVerify Key = `mailer.skiptlsverify`
|
|
|
|
MailerFromEmail Key = `mailer.fromemail`
|
|
|
|
MailerQueuelength Key = `mailer.queuelength`
|
|
|
|
MailerQueueTimeout Key = `mailer.queuetimeout`
|
|
|
|
|
|
|
|
RedisEnabled Key = `redis.enabled`
|
|
|
|
RedisHost Key = `redis.host`
|
|
|
|
RedisPassword Key = `redis.password`
|
|
|
|
RedisDB Key = `redis.db`
|
|
|
|
|
|
|
|
LogEnabled Key = `log.enabled`
|
|
|
|
LogErrors Key = `log.errors`
|
|
|
|
LogStandard Key = `log.standard`
|
|
|
|
LogDatabase Key = `log.database`
|
|
|
|
LogHTTP Key = `log.echo`
|
|
|
|
LogEcho Key = `log.echo`
|
|
|
|
LogPath Key = `log.path`
|
2018-06-10 11:11:41 +02:00
|
|
|
)
|
|
|
|
|
2019-07-06 22:12:26 +02:00
|
|
|
// GetString returns a string config value
|
|
|
|
func (k Key) GetString() string {
|
|
|
|
return viper.GetString(string(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBool returns a bool config value
|
|
|
|
func (k Key) GetBool() bool {
|
|
|
|
return viper.GetBool(string(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInt returns an int config value
|
|
|
|
func (k Key) GetInt() int {
|
|
|
|
return viper.GetInt(string(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDuration returns a duration config value
|
|
|
|
func (k Key) GetDuration() time.Duration {
|
|
|
|
return viper.GetDuration(string(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets a value
|
|
|
|
func (k Key) Set(i interface{}) {
|
|
|
|
viper.Set(string(k), i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sets the default config value
|
|
|
|
func (k Key) setDefault(i interface{}) {
|
|
|
|
viper.SetDefault(string(k), i)
|
|
|
|
}
|
|
|
|
|
2018-09-08 13:31:13 +02:00
|
|
|
// InitConfig initializes the config, sets defaults etc.
|
2019-03-24 18:15:44 +01:00
|
|
|
func InitConfig() {
|
2018-09-08 13:29:35 +02:00
|
|
|
|
|
|
|
// Set defaults
|
|
|
|
// Service config
|
|
|
|
random, err := random(32)
|
|
|
|
if err != nil {
|
2019-07-06 22:12:26 +02:00
|
|
|
log.Fatal(err.Error())
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
2018-10-27 11:33:28 +02:00
|
|
|
// Service
|
2019-07-06 22:12:26 +02:00
|
|
|
ServiceJWTSecret.setDefault(random)
|
|
|
|
ServiceInterface.setDefault(":3456")
|
|
|
|
ServiceFrontendurl.setDefault("")
|
|
|
|
ServiceEnableCaldav.setDefault(true)
|
2018-12-18 17:01:46 +01:00
|
|
|
|
2018-11-02 11:01:28 +01:00
|
|
|
ex, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
exPath := filepath.Dir(ex)
|
2019-07-06 22:12:26 +02:00
|
|
|
ServiceRootpath.setDefault(exPath)
|
|
|
|
ServicePageCount.setDefault(50)
|
|
|
|
ServiceEnableMetrics.setDefault(false)
|
2019-07-16 00:54:38 +02:00
|
|
|
ServiceMotd.setDefault("")
|
|
|
|
|
2018-09-08 13:29:35 +02:00
|
|
|
// Database
|
2019-07-06 22:12:26 +02:00
|
|
|
DatabaseType.setDefault("sqlite")
|
|
|
|
DatabaseHost.setDefault("localhost")
|
|
|
|
DatabaseUser.setDefault("vikunja")
|
|
|
|
DatabasePassword.setDefault("")
|
|
|
|
DatabaseDatabase.setDefault("vikunja")
|
|
|
|
DatabasePath.setDefault("./vikunja.db")
|
|
|
|
DatabaseMaxOpenConnections.setDefault(100)
|
|
|
|
DatabaseMaxIdleConnections.setDefault(50)
|
|
|
|
DatabaseMaxConnectionLifetime.setDefault(10000)
|
2019-05-25 07:49:52 +02:00
|
|
|
|
2018-09-13 19:53:03 +02:00
|
|
|
// Cacher
|
2019-07-06 22:12:26 +02:00
|
|
|
CacheEnabled.setDefault(false)
|
|
|
|
CacheType.setDefault("memory")
|
|
|
|
CacheMaxElementSize.setDefault(1000)
|
2018-10-27 11:33:28 +02:00
|
|
|
// Mailer
|
2019-07-06 22:12:26 +02:00
|
|
|
MailerEnabled.setDefault(false)
|
|
|
|
MailerHost.setDefault("")
|
|
|
|
MailerPort.setDefault("587")
|
|
|
|
MailerUsername.setDefault("user")
|
|
|
|
MailerPassword.setDefault("")
|
|
|
|
MailerSkipTLSVerify.setDefault(false)
|
|
|
|
MailerFromEmail.setDefault("mail@vikunja")
|
|
|
|
MailerQueuelength.setDefault(100)
|
|
|
|
MailerQueueTimeout.setDefault(30)
|
2018-12-12 23:50:35 +01:00
|
|
|
// Redis
|
2019-07-06 22:12:26 +02:00
|
|
|
RedisEnabled.setDefault(false)
|
|
|
|
RedisHost.setDefault("localhost:6379")
|
|
|
|
RedisPassword.setDefault("")
|
|
|
|
RedisDB.setDefault(0)
|
2019-01-25 12:40:54 +01:00
|
|
|
// Logger
|
2019-07-06 22:12:26 +02:00
|
|
|
LogEnabled.setDefault(true)
|
|
|
|
LogErrors.setDefault("stdout")
|
|
|
|
LogStandard.setDefault("stdout")
|
|
|
|
LogDatabase.setDefault(false)
|
|
|
|
LogHTTP.setDefault("stdout")
|
|
|
|
LogEcho.setDefault("off")
|
|
|
|
LogPath.setDefault(ServiceRootpath.GetString() + "/logs")
|
2018-09-08 13:29:35 +02:00
|
|
|
|
|
|
|
// Init checking for environment variables
|
|
|
|
viper.SetEnvPrefix("vikunja")
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
// Load the config file
|
2019-07-06 22:12:26 +02:00
|
|
|
viper.AddConfigPath(ServiceRootpath.GetString())
|
2019-01-20 18:13:21 +01:00
|
|
|
viper.AddConfigPath("/etc/vikunja/")
|
|
|
|
viper.AddConfigPath("~/.config/vikunja")
|
2018-09-08 13:29:35 +02:00
|
|
|
viper.AddConfigPath(".")
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
err = viper.ReadInConfig()
|
|
|
|
if err != nil {
|
2019-07-06 22:12:26 +02:00
|
|
|
log.Println(err.Error())
|
|
|
|
log.Println("Using defaults.")
|
2018-09-08 13:29:35 +02:00
|
|
|
}
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
2018-09-08 13:29:35 +02:00
|
|
|
func random(length int) (string, error) {
|
|
|
|
b := make([]byte, length)
|
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%X", b), nil
|
|
|
|
}
|