From 4ae9fdeffaf87e1d07408bd6a9ae85df45c1fcae Mon Sep 17 00:00:00 2001 From: konrad Date: Mon, 1 Jul 2019 23:04:57 +0200 Subject: [PATCH 1/8] Started using keys as const values --- pkg/config/config.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/config/config.go b/pkg/config/config.go index f17ca1326..c16c95834 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -26,6 +26,9 @@ import ( "strings" ) +// Key is used as a config key +type Key string + // InitConfig initializes the config, sets defaults etc. func InitConfig() { -- 2.45.1 From 31e2b72103294ae611883d920cff165643b3fbec Mon Sep 17 00:00:00 2001 From: konrad Date: Mon, 1 Jul 2019 23:05:48 +0200 Subject: [PATCH 2/8] Started adding cons --- pkg/config/config.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/config/config.go b/pkg/config/config.go index c16c95834..68a320e8e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -29,6 +29,10 @@ import ( // Key is used as a config key type Key string +const ( + ServiceJWTSecret Key = `service.JWTSecret` +) + // InitConfig initializes the config, sets defaults etc. func InitConfig() { -- 2.45.1 From bdc68b6149f20ea0de0b8031de88bac4915a8692 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 6 Jul 2019 17:09:18 +0200 Subject: [PATCH 3/8] Started adding consts for all the things --- pkg/config/config.go | 73 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 68a320e8e..27673f0fe 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -29,10 +29,73 @@ import ( // Key is used as a config key type Key string +// These constants hold all config value keys const ( - ServiceJWTSecret Key = `service.JWTSecret` + ServiceJWTSecret Key = `service.JWTSecret` + ServiceInterface Key = `service.interface` + ServiceFrontendurl Key = `service.frontendurl` + ServiceEnableCaldav Key = `service.enablecaldav` + ServiceRootpath Key = `` + ServicePageCount Key = `` + ServiceEnableMetrics Key = `` + + DatabaseType Key = `` + DatabaseHost Key = `` + DatabaseUser Key = `` + DatabasePassword Key = `` + DatabaseDatabase Key = `` + DatabasePath Key = `` + DatabaseMaxOpenConnections Key = `` + DatabaseMaxIdleConnections Key = `` + DatabaseMaxConnectionLifetime Key = `` + + CacheEnabled Key = `` + CacheType Key = `` + CacheMaxElementSize Key = `` + + MailerEnabled Key = `` + MailerHost Key = `` + MailerPort Key = `` + MailerUser Key = `` + MailerPassword Key = `` + MailerSkipTLSVerify Key = `` + MailerFromEmail Key = `` + MailerQueuelength Key = `` + MailerQueueTimeout Key = `` + + RedisEnabled Key = `` + RedisHost Key = `` + RedisPassword Key = `` + RedisDB Key = `` + + LogEnabled Key = `` + LogErrors Key = `` + LogStandard Key = `` + LogHttp Key = `` + LogEcho Key = `` + LogPath Key = `` ) +// 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 int64 config value +func (k Key) GetInt() int64 { + return viper.GetInt64(string(k)) +} + +// sets the default config value +func (k Key) setDefault(i interface{}) { + viper.SetDefault(string(k), i) +} + // InitConfig initializes the config, sets defaults etc. func InitConfig() { @@ -44,10 +107,10 @@ func InitConfig() { } // Service - viper.SetDefault("service.JWTSecret", random) - viper.SetDefault("service.interface", ":3456") - viper.SetDefault("service.frontendurl", "") - viper.SetDefault("service.enablecaldav", true) + ServiceJWTSecret.setDefault(random) + ServiceInterface.setDefault(":3456") + ServiceFrontendurl.setDefault("") + ServiceEnableCaldav.setDefault(true) ex, err := os.Executable() if err != nil { -- 2.45.1 From 40c5eebb4786352cccf25e0d4b648113ab1fa742 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 6 Jul 2019 17:20:55 +0200 Subject: [PATCH 4/8] Default setting with consts --- pkg/config/config.go | 141 ++++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 70 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 27673f0fe..182b7b5ba 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -35,45 +35,46 @@ const ( ServiceInterface Key = `service.interface` ServiceFrontendurl Key = `service.frontendurl` ServiceEnableCaldav Key = `service.enablecaldav` - ServiceRootpath Key = `` - ServicePageCount Key = `` - ServiceEnableMetrics Key = `` + ServiceRootpath Key = `service.rootpath` + ServicePageCount Key = `service.pagecount` + ServiceEnableMetrics Key = `service.enablemetrics` - DatabaseType Key = `` - DatabaseHost Key = `` - DatabaseUser Key = `` - DatabasePassword Key = `` - DatabaseDatabase Key = `` - DatabasePath Key = `` - DatabaseMaxOpenConnections Key = `` - DatabaseMaxIdleConnections Key = `` - DatabaseMaxConnectionLifetime Key = `` + 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 = `` - CacheType Key = `` - CacheMaxElementSize Key = `` + CacheEnabled Key = `cache.enabled` + CacheType Key = `cache.type` + CacheMaxElementSize Key = `cache.maxelementsize` - MailerEnabled Key = `` - MailerHost Key = `` - MailerPort Key = `` - MailerUser Key = `` - MailerPassword Key = `` - MailerSkipTLSVerify Key = `` - MailerFromEmail Key = `` - MailerQueuelength Key = `` - MailerQueueTimeout Key = `` + MailerEnabled Key = `mailer.enabled` + MailerHost Key = `mailer.host` + MailerPort Key = `mailer.port` + MailerUser Key = `mailer.user` + MailerPassword Key = `mailer.password` + MailerSkipTLSVerify Key = `mailer.skiptlsverify` + MailerFromEmail Key = `mailer.fromemail` + MailerQueuelength Key = `mailer.queuelength` + MailerQueueTimeout Key = `mailer.queuetimeout` - RedisEnabled Key = `` - RedisHost Key = `` - RedisPassword Key = `` - RedisDB Key = `` + RedisEnabled Key = `redis.enabled` + RedisHost Key = `redis.host` + RedisPassword Key = `redis.password` + RedisDB Key = `redis.db` - LogEnabled Key = `` - LogErrors Key = `` - LogStandard Key = `` - LogHttp Key = `` - LogEcho Key = `` - LogPath Key = `` + 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` ) // GetString returns a string config value @@ -117,47 +118,47 @@ func InitConfig() { panic(err) } exPath := filepath.Dir(ex) - viper.SetDefault("service.rootpath", exPath) - viper.SetDefault("service.pagecount", 50) - viper.SetDefault("service.enablemetrics", false) + ServiceRootpath.setDefault(exPath) + ServicePageCount.setDefault(50) + ServiceEnableMetrics.setDefault(false) // 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") - viper.SetDefault("database.maxopenconnections", 100) - viper.SetDefault("database.maxidleconnections", 50) - viper.SetDefault("database.maxconnectionlifetime", 10000) + 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) // Cacher - viper.SetDefault("cache.enabled", false) - viper.SetDefault("cache.type", "memory") - viper.SetDefault("cache.maxelementsize", 1000) + CacheEnabled.setDefault(false) + CacheType.setDefault("memory") + CacheMaxElementSize.setDefault(1000) // Mailer - viper.SetDefault("mailer.enabled", false) - 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) + MailerEnabled.setDefault(false) + MailerHost.setDefault("") + MailerPort.setDefault("587") + MailerUser.setDefault("user") + MailerPassword.setDefault("") + MailerSkipTLSVerify.setDefault(false) + MailerFromEmail.setDefault("mail@vikunja") + MailerQueuelength.setDefault(100) + MailerQueueTimeout.setDefault(30) // Redis - viper.SetDefault("redis.enabled", false) - viper.SetDefault("redis.host", "localhost:6379") - viper.SetDefault("redis.password", "") - viper.SetDefault("redis.db", 0) + RedisEnabled.setDefault(false) + RedisHost.setDefault("localhost:6379") + RedisPassword.setDefault("") + RedisDB.setDefault(0) // 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") + LogEnabled.setDefault(true) + LogErrors.setDefault("stdout") + LogStandard.setDefault("stdout") + LogDatabase.setDefault(false) + LogHttp.setDefault("stdout") + LogEcho.setDefault("off") + LogPath.setDefault(ServiceRootpath.GetString() + "/logs") // Init checking for environment variables viper.SetEnvPrefix("vikunja") @@ -165,7 +166,7 @@ func InitConfig() { viper.AutomaticEnv() // Load the config file - viper.AddConfigPath(viper.GetString("service.rootpath")) + viper.AddConfigPath(ServiceRootpath.GetString()) viper.AddConfigPath("/etc/vikunja/") viper.AddConfigPath("~/.config/vikunja") viper.AddConfigPath(".") -- 2.45.1 From aa714254e0a31ab2165f8f88df1bfc6f432bc018 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 6 Jul 2019 20:35:29 +0200 Subject: [PATCH 5/8] Get setting values everywhere via the package --- pkg/cmd/web.go | 4 ++-- pkg/config/config.go | 21 ++++++++++++++++----- pkg/db/db.go | 23 +++++++++++------------ pkg/integrations/integrations.go | 4 ++-- pkg/log/logging.go | 23 ++++++++++++----------- pkg/mail/mail.go | 14 +++++++------- pkg/mail/send_mail.go | 8 ++++---- pkg/metrics/metrics.go | 3 ++- pkg/migration/migration.go | 4 ++-- pkg/models/main_test.go | 2 +- pkg/models/models.go | 12 ++++++------ pkg/models/unit_tests.go | 2 +- pkg/models/user_add_update.go | 6 +++--- pkg/models/user_password_reset.go | 6 +++--- pkg/red/redis.go | 12 ++++++------ pkg/routes/api/v1/login.go | 4 ++-- pkg/routes/routes.go | 16 ++++++++-------- 17 files changed, 88 insertions(+), 76 deletions(-) diff --git a/pkg/cmd/web.go b/pkg/cmd/web.go index 067f83204..6cd4cd73c 100644 --- a/pkg/cmd/web.go +++ b/pkg/cmd/web.go @@ -17,13 +17,13 @@ package cmd import ( + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/routes" "code.vikunja.io/api/pkg/swagger" "context" "fmt" "github.com/spf13/cobra" - "github.com/spf13/viper" "os" "os/signal" "time" @@ -49,7 +49,7 @@ var webCmd = &cobra.Command{ routes.RegisterRoutes(e) // Start server go func() { - if err := e.Start(viper.GetString("service.interface")); err != nil { + if err := e.Start(config.ServiceInterface.GetString()); err != nil { e.Logger.Info("shutting down...") } }() diff --git a/pkg/config/config.go b/pkg/config/config.go index 182b7b5ba..7c868182a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -24,6 +24,7 @@ import ( "os" "path/filepath" "strings" + "time" ) // Key is used as a config key @@ -56,7 +57,7 @@ const ( MailerEnabled Key = `mailer.enabled` MailerHost Key = `mailer.host` MailerPort Key = `mailer.port` - MailerUser Key = `mailer.user` + MailerUsername Key = `mailer.username` MailerPassword Key = `mailer.password` MailerSkipTLSVerify Key = `mailer.skiptlsverify` MailerFromEmail Key = `mailer.fromemail` @@ -87,9 +88,19 @@ func (k Key) GetBool() bool { return viper.GetBool(string(k)) } -// GetInt returns an int64 config value -func (k Key) GetInt() int64 { - return viper.GetInt64(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 @@ -140,7 +151,7 @@ func InitConfig() { MailerEnabled.setDefault(false) MailerHost.setDefault("") MailerPort.setDefault("587") - MailerUser.setDefault("user") + MailerUsername.setDefault("user") MailerPassword.setDefault("") MailerSkipTLSVerify.setDefault(false) MailerFromEmail.setDefault("mail@vikunja") diff --git a/pkg/db/db.go b/pkg/db/db.go index fd8d124f3..bd2b570cc 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -22,7 +22,6 @@ import ( "fmt" "github.com/go-xorm/core" "github.com/go-xorm/xorm" - "github.com/spf13/viper" "strconv" "time" @@ -33,12 +32,12 @@ import ( // CreateDBEngine initializes a db engine from the config func CreateDBEngine() (engine *xorm.Engine, err error) { // If the database type is not set, this likely means we need to initialize the config first - if viper.GetString("database.type") == "" { + if config.DatabaseType.GetString() == "" { config.InitConfig() } // Use Mysql if set - if viper.GetString("database.type") == "mysql" { + if config.DatabaseType.GetString() == "mysql" { engine, err = initMysqlEngine() if err != nil { return @@ -52,7 +51,7 @@ func CreateDBEngine() (engine *xorm.Engine, err error) { } engine.SetMapper(core.GonicMapper{}) - engine.ShowSQL(viper.GetString("log.database") != "off") + engine.ShowSQL(config.LogDatabase.GetString() != "off") engine.SetLogger(xorm.NewSimpleLogger(log.GetLogWriter("database"))) return @@ -61,17 +60,17 @@ func CreateDBEngine() (engine *xorm.Engine, err error) { func initMysqlEngine() (engine *xorm.Engine, err error) { connStr := fmt.Sprintf( "%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true", - viper.GetString("database.user"), - viper.GetString("database.password"), - viper.GetString("database.host"), - viper.GetString("database.database")) + config.DatabaseUser.GetString(), + config.DatabasePassword.GetString(), + config.DatabaseHost.GetString(), + config.DatabaseDatabase.GetString()) engine, err = xorm.NewEngine("mysql", connStr) if err != nil { return } - engine.SetMaxOpenConns(viper.GetInt("database.maxopenconnections")) - engine.SetMaxIdleConns(viper.GetInt("database.maxidleconnections")) - max, err := time.ParseDuration(strconv.Itoa(viper.GetInt("database.maxconnectionlifetime")) + `ms`) + engine.SetMaxOpenConns(config.DatabaseMaxOpenConnections.GetInt()) + engine.SetMaxIdleConns(config.DatabaseMaxIdleConnections.GetInt()) + max, err := time.ParseDuration(strconv.Itoa(config.DatabaseMaxConnectionLifetime.GetInt()) + `ms`) if err != nil { return } @@ -80,7 +79,7 @@ func initMysqlEngine() (engine *xorm.Engine, err error) { } func initSqliteEngine() (engine *xorm.Engine, err error) { - path := viper.GetString("database.path") + path := config.DatabasePath.GetString() if path == "" { path = "./db.db" } diff --git a/pkg/integrations/integrations.go b/pkg/integrations/integrations.go index b022e2d6b..6048b84ea 100644 --- a/pkg/integrations/integrations.go +++ b/pkg/integrations/integrations.go @@ -75,7 +75,7 @@ var ( func setupTestEnv() (e *echo.Echo, err error) { config.InitConfig() - models.SetupTests(viper.GetString("service.rootpath")) + models.SetupTests(config.ServiceRootpath.GetString()) err = models.LoadFixtures() if err != nil { @@ -114,7 +114,7 @@ func addTokenToContext(t *testing.T, user *models.User, c echo.Context) { assert.NoError(t, err) // We send the string token through the parsing function to get a valid jwt.Token tken, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) { - return []byte(viper.GetString("service.JWTSecret")), nil + return []byte(config.ServiceJWTSecret.GetString()), nil }) assert.NoError(t, err) c.Set("user", tken) diff --git a/pkg/log/logging.go b/pkg/log/logging.go index 4b3b37e0c..fd75d1f8c 100644 --- a/pkg/log/logging.go +++ b/pkg/log/logging.go @@ -17,6 +17,7 @@ package log import ( + "code.vikunja.io/api/pkg/config" "github.com/op/go-logging" "github.com/spf13/viper" "io" @@ -39,18 +40,18 @@ var Log = logging.MustGetLogger("vikunja") // InitLogger initializes the global log handler func InitLogger() { - if !viper.GetBool("log.enabled") { + if !config.LogEnabled.GetBool() { // Disable all logging when loggin in general is disabled, overwriting everything a user might have set. - viper.Set("log.errors", "off") - viper.Set("log.standard", "off") - viper.Set("log.database", "off") - viper.Set("log.http", "off") - viper.Set("log.echo", "off") + config.LogErrors.Set("off") + config.LogStandard.Set("off") + config.LogDatabase.Set("off") + config.LogHttp.Set("off") + config.LogEcho.Set("off") return } - if viper.GetString("log.errors") == "file" || viper.GetString("log.standard") == "file" { - err := os.Mkdir(viper.GetString("log.path"), 0744) + if config.LogErrors.GetString() == "file" || config.LogStandard.GetString() == "file" { + err := os.Mkdir(config.LogPath.GetString(), 0744) if err != nil && !os.IsExist(err) { log.Fatal("Could not create log folder: ", err.Error()) } @@ -59,7 +60,7 @@ func InitLogger() { var logBackends []logging.Backend // We define our two backends - if viper.GetString("log.standard") != "off" { + if config.LogStandard.GetString() != "off" { stdWriter := GetLogWriter("standard") stdBackend := logging.NewLogBackend(stdWriter, "", 0) @@ -67,7 +68,7 @@ func InitLogger() { logBackends = append(logBackends, logging.NewBackendFormatter(stdBackend, logging.MustStringFormatter(Fmt+"\n"))) } - if viper.GetString("log.error") != "off" { + if config.LogErrors.GetString() != "off" { errWriter := GetLogWriter("error") errBackend := logging.NewLogBackend(errWriter, "", 0) @@ -86,7 +87,7 @@ func GetLogWriter(logfile string) (writer io.Writer) { writer = os.Stderr // Set the default case to prevent nil pointer panics switch viper.GetString("log." + logfile) { case "file": - f, err := os.OpenFile(viper.GetString("log.path")+"/"+logfile+".log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + f, err := os.OpenFile(config.LogPath.GetString()+"/"+logfile+".log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Fatal(err) } diff --git a/pkg/mail/mail.go b/pkg/mail/mail.go index 6401957ce..8cfff9d5e 100644 --- a/pkg/mail/mail.go +++ b/pkg/mail/mail.go @@ -17,9 +17,9 @@ package mail import ( + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" "crypto/tls" - "github.com/spf13/viper" "gopkg.in/gomail.v2" "time" ) @@ -29,20 +29,20 @@ var Queue chan *gomail.Message // StartMailDaemon starts the mail daemon func StartMailDaemon() { - Queue = make(chan *gomail.Message, viper.GetInt("mailer.queuelength")) + Queue = make(chan *gomail.Message, config.MailerQueuelength.GetInt()) - if !viper.GetBool("mailer.enabled") { + if !config.MailerEnabled.GetBool() { return } - if viper.GetString("mailer.host") == "" { + if config.MailerHost.GetString() == "" { log.Log.Warning("Mailer seems to be not configured! Please see the config docs for more details.") return } go func() { - d := gomail.NewDialer(viper.GetString("mailer.host"), viper.GetInt("mailer.port"), viper.GetString("mailer.username"), viper.GetString("mailer.password")) - d.TLSConfig = &tls.Config{InsecureSkipVerify: viper.GetBool("mailer.skiptlsverify")} + d := gomail.NewDialer(config.MailerHost.GetString(), config.MailerPort.GetInt(), config.MailerUsername.GetString(), config.MailerPassword.GetString()) + d.TLSConfig = &tls.Config{InsecureSkipVerify: config.MailerSkipTLSVerify.GetBool()} var s gomail.SendCloser var err error @@ -64,7 +64,7 @@ func StartMailDaemon() { } // Close the connection to the SMTP server if no email was sent in // the last 30 seconds. - case <-time.After(viper.GetDuration("mailer.queuetimeout") * time.Second): + case <-time.After(config.MailerQueueTimeout.GetDuration() * time.Second): if open { if err := s.Close(); err != nil { log.Log.Error("Error closing the mail server connection: %s\n", err) diff --git a/pkg/mail/send_mail.go b/pkg/mail/send_mail.go index 532ab140b..b64140aec 100644 --- a/pkg/mail/send_mail.go +++ b/pkg/mail/send_mail.go @@ -18,9 +18,9 @@ package mail import ( "bytes" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/utils" "github.com/labstack/gommon/log" - "github.com/spf13/viper" "gopkg.in/gomail.v2" "text/template" ) @@ -54,7 +54,7 @@ type header struct { // SendMail puts a mail in the queue func SendMail(opts *Opts) { m := gomail.NewMessage() - m.SetHeader("From", viper.GetString("mailer.fromemail")) + m.SetHeader("From", config.MailerFromEmail.GetString()) m.SetHeader("To", opts.To) m.SetHeader("Subject", opts.Subject) for _, h := range opts.Headers { @@ -85,13 +85,13 @@ func SendMailWithTemplate(to, subject, tpl string, data map[string]interface{}) var plainContent bytes.Buffer t := &Template{ - Templates: template.Must(template.ParseGlob(viper.GetString("service.rootpath") + "/templates/mail/*.tmpl")), + Templates: template.Must(template.ParseGlob(config.ServiceRootpath.GetString() + "/templates/mail/*.tmpl")), } boundary := "np" + utils.MakeRandomString(13) data["Boundary"] = boundary - data["FrontendURL"] = viper.GetString("service.frontendurl") + data["FrontendURL"] = config.ServiceFrontendurl.GetString() if err := t.Templates.ExecuteTemplate(&htmlContent, tpl+".html.tmpl", data); err != nil { log.Error(3, "Template: %v", err) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index f304c2428..e8d629be0 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -17,6 +17,7 @@ package metrics import ( + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/red" "github.com/go-redis/redis" @@ -112,7 +113,7 @@ func SetCount(count int64, key string) error { // UpdateCount updates a count with a given amount func UpdateCount(update int64, key string) { - if !viper.GetBool("service.enablemetrics") { + if !config.ServiceEnableMetrics.GetBool() { return } oldtotal, err := GetCount(key) diff --git a/pkg/migration/migration.go b/pkg/migration/migration.go index fb81c997f..48fe4047e 100644 --- a/pkg/migration/migration.go +++ b/pkg/migration/migration.go @@ -17,12 +17,12 @@ package migration import ( + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/models" "github.com/go-xorm/xorm" "github.com/olekukonko/tablewriter" - "github.com/spf13/viper" "os" "sort" "src.techknowlogick.com/xormigrate" @@ -103,7 +103,7 @@ func Rollback(migrationID string) { // Deletes a column from a table. All arguments are strings, to let them be standalone and not depending on any struct. func dropTableColum(x *xorm.Engine, tableName, col string) error { - switch viper.GetString("database.type") { + switch config.DatabaseType.GetString() { case "sqlite": log.Log.Warning("Unable to drop columns in SQLite") case "mysql": diff --git a/pkg/models/main_test.go b/pkg/models/main_test.go index c28eb5a6a..59f8fd447 100644 --- a/pkg/models/main_test.go +++ b/pkg/models/main_test.go @@ -24,5 +24,5 @@ import ( func TestMain(m *testing.M) { config.InitConfig() - MainTest(m, viper.GetString("service.rootpath")) + MainTest(m, config.ServiceRootpath.GetString()) } diff --git a/pkg/models/models.go b/pkg/models/models.go index 6bb9267d4..d781997dc 100644 --- a/pkg/models/models.go +++ b/pkg/models/models.go @@ -17,6 +17,7 @@ package models import ( + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/log" "encoding/gob" @@ -24,7 +25,6 @@ import ( "github.com/go-xorm/xorm" xrc "github.com/go-xorm/xorm-redis-cache" _ "github.com/mattn/go-sqlite3" // Because. - "github.com/spf13/viper" ) var ( @@ -61,13 +61,13 @@ func SetEngine() (err error) { // Cache // We have to initialize the cache here to avoid import cycles - if viper.GetBool("cache.enabled") { - switch viper.GetString("cache.type") { + if config.CacheEnabled.GetBool() { + switch config.CacheType.GetString() { case "memory": - cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), viper.GetInt("cache.maxelementsize")) + cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), config.CacheMaxElementSize.GetInt()) x.SetDefaultCacher(cacher) case "redis": - cacher := xrc.NewRedisCacher(viper.GetString("redis.host"), viper.GetString("redis.password"), xrc.DEFAULT_EXPIRATION, x.Logger()) + cacher := xrc.NewRedisCacher(config.RedisEnabled.GetString(), config.RedisPassword.GetString(), xrc.DEFAULT_EXPIRATION, x.Logger()) x.SetDefaultCacher(cacher) gob.Register(GetTables()) default: @@ -85,7 +85,7 @@ func getLimitFromPageIndex(page int) (limit, start int) { return 0, 0 } - limit = viper.GetInt("service.pagecount") + limit = config.ServicePageCount.GetInt() start = limit * (page - 1) return } diff --git a/pkg/models/unit_tests.go b/pkg/models/unit_tests.go index 01621457d..038e9a1db 100644 --- a/pkg/models/unit_tests.go +++ b/pkg/models/unit_tests.go @@ -71,7 +71,7 @@ func createTestEngine(fixturesDir string) error { return err } - if viper.GetString("database.type") == "mysql" { + if config.DatabaseType.GetString() == "mysql" { fixturesHelper = &testfixtures.MySQL{} } } else { diff --git a/pkg/models/user_add_update.go b/pkg/models/user_add_update.go index 03d61072e..9598d6ed7 100644 --- a/pkg/models/user_add_update.go +++ b/pkg/models/user_add_update.go @@ -17,10 +17,10 @@ package models import ( + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/mail" "code.vikunja.io/api/pkg/metrics" "code.vikunja.io/api/pkg/utils" - "github.com/spf13/viper" "golang.org/x/crypto/bcrypt" ) @@ -69,7 +69,7 @@ func CreateUser(user User) (newUser User, err error) { } newUser.IsActive = true - if viper.GetBool("mailer.enabled") { + if config.MailerEnabled.GetBool() { // The new user should not be activated until it confirms his mail address newUser.IsActive = false // Generate a confirm token @@ -99,7 +99,7 @@ func CreateUser(user User) (newUser User, err error) { } // Dont send a mail if we're testing - if !viper.GetBool("mailer.enabled") { + if !config.MailerEnabled.GetBool() { return newUserOut, err } diff --git a/pkg/models/user_password_reset.go b/pkg/models/user_password_reset.go index 3f351d4a2..900472538 100644 --- a/pkg/models/user_password_reset.go +++ b/pkg/models/user_password_reset.go @@ -17,9 +17,9 @@ package models import ( + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/mail" "code.vikunja.io/api/pkg/utils" - "github.com/spf13/viper" ) // PasswordReset holds the data to reset a password @@ -62,7 +62,7 @@ func UserPasswordReset(reset *PasswordReset) (err error) { } // Dont send a mail if we're testing - if !viper.GetBool("mailer.enabled") { + if !config.MailerEnabled.GetBool() { return } @@ -103,7 +103,7 @@ func RequestUserPasswordResetToken(tr *PasswordTokenRequest) (err error) { } // Dont send a mail if we're testing - if !viper.GetBool("mailer.enabled") { + if !config.MailerEnabled.GetBool() { return } diff --git a/pkg/red/redis.go b/pkg/red/redis.go index 041138f35..c13371d16 100644 --- a/pkg/red/redis.go +++ b/pkg/red/redis.go @@ -17,27 +17,27 @@ package red import ( + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" "github.com/go-redis/redis" - "github.com/spf13/viper" ) var r *redis.Client // InitRedis initializes a redis connection func InitRedis() { - if !viper.GetBool("redis.enabled") { + if !config.RedisEnabled.GetBool() { return } - if viper.GetString("redis.host") == "" { + if config.RedisHost.GetString() == "" { log.Log.Fatal("No redis host provided.") } r = redis.NewClient(&redis.Options{ - Addr: viper.GetString("redis.host"), - Password: viper.GetString("redis.password"), - DB: viper.GetInt("redis.db"), + Addr: config.RedisHost.GetString(), + Password: config.RedisPassword.GetString(), + DB: config.RedisDB.GetInt(), }) err := r.Ping().Err() diff --git a/pkg/routes/api/v1/login.go b/pkg/routes/api/v1/login.go index 78d063f3f..4753d6498 100644 --- a/pkg/routes/api/v1/login.go +++ b/pkg/routes/api/v1/login.go @@ -17,11 +17,11 @@ package v1 import ( + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/models" "code.vikunja.io/web/handler" "github.com/dgrijalva/jwt-go" "github.com/labstack/echo/v4" - "github.com/spf13/viper" "net/http" "time" ) @@ -77,5 +77,5 @@ func CreateNewJWTTokenForUser(user *models.User) (token string, err error) { claims["avatar"] = user.AvatarURL // Generate encoded token and send it as response. - return t.SignedString([]byte(viper.GetString("service.JWTSecret"))) + return t.SignedString([]byte(config.ServiceJWTSecret.GetString())) } diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index fa2aa0c19..d49eb3d25 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -39,6 +39,7 @@ package routes import ( + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/metrics" "code.vikunja.io/api/pkg/models" @@ -52,7 +53,6 @@ import ( "github.com/labstack/echo/v4/middleware" elog "github.com/labstack/gommon/log" "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/spf13/viper" "strings" ) @@ -88,7 +88,7 @@ func NewEcho() *echo.Echo { e.HideBanner = true if l, ok := e.Logger.(*elog.Logger); ok { - if viper.GetString("log.echo") == "off" { + if config.LogEcho.GetString() == "off" { l.SetLevel(elog.OFF) } l.EnableColor() @@ -97,7 +97,7 @@ func NewEcho() *echo.Echo { } // Logger - if viper.GetString("log.http") != "off" { + if config.LogHttp.GetString() != "off" { e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ Format: log.WebFmt + "\n", Output: log.GetLogWriter("http"), @@ -121,7 +121,7 @@ func NewEcho() *echo.Echo { // RegisterRoutes registers all routes for the application func RegisterRoutes(e *echo.Echo) { - if viper.GetBool("service.enablecaldav") { + if config.ServiceEnableCaldav.GetBool() { // Caldav routes wkg := e.Group("/.well-known") wkg.Use(middleware.BasicAuth(caldavBasicAuth)) @@ -155,9 +155,9 @@ func registerAPIRoutes(a *echo.Group) { a.GET("/docs", apiv1.RedocUI) // Prometheus endpoint - if viper.GetBool("service.enablemetrics") { + if config.ServiceEnableMetrics.GetBool() { - if !viper.GetBool("redis.enabled") { + if !config.RedisEnabled.GetBool() { log.Log.Fatal("You have to enable redis in order to use metrics") } @@ -217,10 +217,10 @@ func registerAPIRoutes(a *echo.Group) { // ===== Routes with Authetification ===== // Authetification - a.Use(middleware.JWT([]byte(viper.GetString("service.JWTSecret")))) + a.Use(middleware.JWT([]byte(config.ServiceJWTSecret.GetString()))) // Middleware to collect metrics - if viper.GetBool("service.enablemetrics") { + if config.ServiceJWTSecret.GetBool() { a.Use(func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { -- 2.45.1 From b2da5771b1f0c204a71bed6182bd2a97a9710889 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 6 Jul 2019 20:38:52 +0200 Subject: [PATCH 6/8] Fixed lint issues --- pkg/config/config.go | 15 ++++++++------- pkg/log/logging.go | 2 +- pkg/routes/routes.go | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 7c868182a..9dcd594f0 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -17,14 +17,15 @@ package config import ( - "code.vikunja.io/api/pkg/log" "crypto/rand" "fmt" - "github.com/spf13/viper" + "log" "os" "path/filepath" "strings" "time" + + "github.com/spf13/viper" ) // Key is used as a config key @@ -73,7 +74,7 @@ const ( LogErrors Key = `log.errors` LogStandard Key = `log.standard` LogDatabase Key = `log.database` - LogHttp Key = `log.echo` + LogHTTP Key = `log.echo` LogEcho Key = `log.echo` LogPath Key = `log.path` ) @@ -115,7 +116,7 @@ func InitConfig() { // Service config random, err := random(32) if err != nil { - log.Log.Fatal(err.Error()) + log.Fatal(err.Error()) } // Service @@ -167,7 +168,7 @@ func InitConfig() { LogErrors.setDefault("stdout") LogStandard.setDefault("stdout") LogDatabase.setDefault(false) - LogHttp.setDefault("stdout") + LogHTTP.setDefault("stdout") LogEcho.setDefault("off") LogPath.setDefault(ServiceRootpath.GetString() + "/logs") @@ -184,8 +185,8 @@ func InitConfig() { viper.SetConfigName("config") err = viper.ReadInConfig() if err != nil { - log.Log.Info(err) - log.Log.Info("Using defaults.") + log.Println(err.Error()) + log.Println("Using defaults.") } } diff --git a/pkg/log/logging.go b/pkg/log/logging.go index fd75d1f8c..ca28800c1 100644 --- a/pkg/log/logging.go +++ b/pkg/log/logging.go @@ -45,7 +45,7 @@ func InitLogger() { config.LogErrors.Set("off") config.LogStandard.Set("off") config.LogDatabase.Set("off") - config.LogHttp.Set("off") + config.LogHTTP.Set("off") config.LogEcho.Set("off") return } diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index d49eb3d25..aecf909ce 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -97,7 +97,7 @@ func NewEcho() *echo.Echo { } // Logger - if config.LogHttp.GetString() != "off" { + if config.LogHTTP.GetString() != "off" { e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ Format: log.WebFmt + "\n", Output: log.GetLogWriter("http"), -- 2.45.1 From 09f7541ed63751070a04fa6dee0754787fb7b8c7 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 6 Jul 2019 21:55:39 +0200 Subject: [PATCH 7/8] Fixed unused packages --- pkg/metrics/metrics.go | 1 - pkg/models/unit_tests.go | 1 - 2 files changed, 2 deletions(-) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index e8d629be0..eafdd4d0a 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -23,7 +23,6 @@ import ( "github.com/go-redis/redis" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" - "github.com/spf13/viper" ) var r *redis.Client diff --git a/pkg/models/unit_tests.go b/pkg/models/unit_tests.go index 038e9a1db..e869a71c8 100644 --- a/pkg/models/unit_tests.go +++ b/pkg/models/unit_tests.go @@ -24,7 +24,6 @@ import ( "fmt" "github.com/go-xorm/core" "github.com/go-xorm/xorm" - "github.com/spf13/viper" "gopkg.in/testfixtures.v2" "os" "path/filepath" -- 2.45.1 From e2a7bc7a1d9bfa28d8b7177c47acd68ebe7a68b5 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 6 Jul 2019 22:00:32 +0200 Subject: [PATCH 8/8] Fixed unused packages --- pkg/integrations/integrations.go | 1 - pkg/models/main_test.go | 1 - 2 files changed, 2 deletions(-) diff --git a/pkg/integrations/integrations.go b/pkg/integrations/integrations.go index 6048b84ea..fee47e44b 100644 --- a/pkg/integrations/integrations.go +++ b/pkg/integrations/integrations.go @@ -25,7 +25,6 @@ import ( "code.vikunja.io/web/handler" "github.com/dgrijalva/jwt-go" "github.com/labstack/echo/v4" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" diff --git a/pkg/models/main_test.go b/pkg/models/main_test.go index 59f8fd447..bd8ebd649 100644 --- a/pkg/models/main_test.go +++ b/pkg/models/main_test.go @@ -18,7 +18,6 @@ package models import ( "code.vikunja.io/api/pkg/config" - "github.com/spf13/viper" "testing" ) -- 2.45.1