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-12-12 23:50:35 +01:00
|
|
|
"code.vikunja.io/api/pkg/log"
|
2018-09-08 13:29:35 +02:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"github.com/spf13/viper"
|
2018-11-02 11:01:28 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-09-08 13:29:35 +02:00
|
|
|
"strings"
|
2018-06-10 11:11:41 +02:00
|
|
|
)
|
|
|
|
|
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 {
|
2018-12-12 23:50:35 +01:00
|
|
|
log.Log.Fatal(err.Error())
|
2018-06-10 11:11:41 +02:00
|
|
|
}
|
|
|
|
|
2018-10-27 11:33:28 +02:00
|
|
|
// Service
|
2018-09-08 13:29:35 +02:00
|
|
|
viper.SetDefault("service.JWTSecret", random)
|
|
|
|
viper.SetDefault("service.interface", ":3456")
|
2018-10-27 11:33:28 +02:00
|
|
|
viper.SetDefault("service.frontendurl", "")
|
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)
|
|
|
|
viper.SetDefault("service.rootpath", exPath)
|
2018-11-09 11:30:17 +01:00
|
|
|
viper.SetDefault("service.pagecount", 50)
|
2018-12-12 23:50:35 +01:00
|
|
|
viper.SetDefault("service.enablemetrics", false)
|
2018-09-08 13:29:35 +02:00
|
|
|
// Database
|
|
|
|
viper.SetDefault("database.type", "sqlite")
|
|
|
|
viper.SetDefault("database.host", "localhost")
|
|
|
|
viper.SetDefault("database.user", "vikunja")
|
|
|
|
viper.SetDefault("database.password", "")
|
|
|
|
viper.SetDefault("database.database", "vikunja")
|
|
|
|
viper.SetDefault("database.path", "./vikunja.db")
|
2018-10-11 18:39:53 +02:00
|
|
|
viper.SetDefault("database.openconnections", 100)
|
2018-09-13 19:53:03 +02:00
|
|
|
// Cacher
|
|
|
|
viper.SetDefault("cache.enabled", false)
|
|
|
|
viper.SetDefault("cache.type", "memory")
|
|
|
|
viper.SetDefault("cache.maxelementsize", 1000)
|
2018-10-27 11:33:28 +02:00
|
|
|
// Mailer
|
2018-12-19 22:05:25 +01:00
|
|
|
viper.SetDefault("mailer.enabled", false)
|
2018-10-27 11:33:28 +02:00
|
|
|
viper.SetDefault("mailer.host", "")
|
|
|
|
viper.SetDefault("mailer.port", "587")
|
|
|
|
viper.SetDefault("mailer.user", "user")
|
|
|
|
viper.SetDefault("mailer.password", "")
|
|
|
|
viper.SetDefault("mailer.skiptlsverify", false)
|
|
|
|
viper.SetDefault("mailer.fromemail", "mail@vikunja")
|
|
|
|
viper.SetDefault("mailer.queuelength", 100)
|
|
|
|
viper.SetDefault("mailer.queuetimeout", 30)
|
2018-12-12 23:50:35 +01:00
|
|
|
// Redis
|
|
|
|
viper.SetDefault("redis.enabled", false)
|
|
|
|
viper.SetDefault("redis.host", "localhost:6379")
|
|
|
|
viper.SetDefault("redis.password", "")
|
|
|
|
viper.SetDefault("redis.db", 0)
|
2019-01-25 12:40:54 +01:00
|
|
|
// Logger
|
|
|
|
viper.SetDefault("log.enabled", true)
|
|
|
|
viper.SetDefault("log.errors", "stdout")
|
|
|
|
viper.SetDefault("log.standard", "stdout")
|
|
|
|
viper.SetDefault("log.database", "off")
|
|
|
|
viper.SetDefault("log.http", "stdout")
|
|
|
|
viper.SetDefault("log.echo", "off")
|
|
|
|
viper.SetDefault("log.path", viper.GetString("service.rootpath")+"/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
|
2018-12-18 17:01:46 +01:00
|
|
|
viper.AddConfigPath(viper.GetString("service.rootpath"))
|
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 {
|
2018-12-12 23:50:35 +01:00
|
|
|
log.Log.Info(err)
|
|
|
|
log.Log.Info("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
|
|
|
|
}
|