diff options
-rw-r--r-- | checkjobs.go | 6 | ||||
-rw-r--r-- | confhelper/confhelper.go | 22 | ||||
-rw-r--r-- | db.go | 15 | ||||
-rw-r--r-- | handler.go | 3 | ||||
-rw-r--r-- | jobedit.go | 15 | ||||
-rw-r--r-- | mailing.go | 18 | ||||
-rw-r--r-- | mails.go | 6 | ||||
-rw-r--r-- | main.go | 26 | ||||
-rw-r--r-- | tpls.go | 6 |
9 files changed, 48 insertions, 69 deletions
diff --git a/checkjobs.go b/checkjobs.go index 2b4af74..c053dd5 100644 --- a/checkjobs.go +++ b/checkjobs.go @@ -3,6 +3,7 @@ package main import ( "bytes" "fmt" + "kch42.de/gostuff/mailremind/confhelper" "kch42.de/gostuff/mailremind/model" "log" "time" @@ -11,10 +12,7 @@ import ( var checkInterval int64 func initCheckjobs() { - var err error - if checkInterval, err = conf.GetInt("schedules", "checkInterval"); err != nil { - log.Fatalf("Could not read config schedules.checkInterval: %s", err) - } + checkInterval = confhelper.ConfIntOrFatal(conf, "schedules", "checkInterval") } func checkjobs() { diff --git a/confhelper/confhelper.go b/confhelper/confhelper.go new file mode 100644 index 0000000..607f50f --- /dev/null +++ b/confhelper/confhelper.go @@ -0,0 +1,22 @@ +package confhelper + +import ( + "github.com/kch42/simpleconf" + "log" +) + +func ConfStringOrFatal(conf simpleconf.Config, section, key string) string { + s, err := conf.GetString(section, key) + if err != nil { + log.Fatalf("Could not read config value %s.%s: %s", section, key, err) + } + return s +} + +func ConfIntOrFatal(conf simpleconf.Config, section, key string) int64 { + i, err := conf.GetInt(section, key) + if err != nil { + log.Fatalf("Could not read config value %s.%s: %s", section, key, err) + } + return i +} @@ -1,6 +1,7 @@ package main import ( + "kch42.de/gostuff/mailremind/confhelper" "kch42.de/gostuff/mailremind/model" "log" ) @@ -9,21 +10,15 @@ var db model.DBInfo var dbcon model.DBCon func initDB() { - dbdrv, err := conf.GetString("db", "driver") - if err != nil { - log.Fatalf("Could not get db.driver from config: %s", err) - } - - dbconf, err := conf.GetString("db", "conf") - if err != nil { - log.Fatalf("Could not get db.conf from config: %s", err) - } + dbdrv := confhelper.ConfStringOrFatal(conf, "db", "driver") + dbconf := confhelper.ConfStringOrFatal(conf, "db", "conf") var ok bool if db, ok = model.GetDBInfo(dbdrv); !ok { - log.Fatalf("Could not get info for dbdrv %s: %s", dbdrv, err) + log.Fatalf("Could not get info for dbdrv %s", dbdrv) } + var err error if dbcon, err = db.Connect(dbconf); err != nil { log.Fatalf("Unable to connect to %s database: %s", dbdrv, err) } @@ -52,8 +52,9 @@ func mkHttpHandler(h Handler, tpl *template.Template) http.HandlerFunc { return func(rw http.ResponseWriter, req *http.Request) { sess, err := getSess(req) if err != nil { + req.Cookies() log.Printf("Error while getting session: %s", err) - rw.Write([]byte("Unable to create session")) // TODO: Better error message... + rw.Write([]byte("Unable to create a session. Try reloading the page or delete the cookies for this site.")) } user := userFromSess(sess) @@ -4,6 +4,7 @@ import ( "github.com/gorilla/mux" "github.com/gorilla/sessions" "kch42.de/gostuff/mailremind/chronos" + "kch42.de/gostuff/mailremind/confhelper" "kch42.de/gostuff/mailremind/model" "log" "net/http" @@ -56,18 +57,8 @@ func chronToSchedTL(chron chronos.Chronos, u model.User) scheduleTpldata { var maxSchedules, jobsLimit int func initLimits() { - schedules, err := conf.GetInt("limits", "schedules") - if err != nil { - log.Fatalf("Could not read config limits.schedules: %s", err) - } - - jobs, err := conf.GetInt("limits", "jobs") - if err != nil { - log.Fatalf("Could not read config limits.jobs: %s", err) - } - - maxSchedules = int(schedules) - jobsLimit = int(jobs) + maxSchedules = int(confhelper.ConfIntOrFatal(conf, "limits", "schedules")) + jobsLimit = int(confhelper.ConfIntOrFatal(conf, "limits", "jobs")) } const bestTimeFmtEver = "2006-01-02 15:04:05" @@ -1,6 +1,7 @@ package main import ( + "kch42.de/gostuff/mailremind/confhelper" "kch42.de/gostuff/mailremind/mailing" "log" ) @@ -22,21 +23,10 @@ func Mail(to, from string, msg []byte) bool { } func initMailing() { - meth, err := conf.GetString("mail", "method") - if err != nil { - log.Fatalf("Could not get mail.method from config: %s", err) - } - - MailFrom, err = conf.GetString("mail", "addr") - if err != nil { - log.Fatalf("Could not get mail.addr from config: %s", err) - } - - parallel, err := conf.GetInt("mail", "parallel") - if err != nil { - log.Fatalf("Could not get mail.parallel from config: %s", err) - } + meth := confhelper.ConfStringOrFatal(conf, "mail", "method") + MailFrom = confhelper.ConfStringOrFatal(conf, "mail", "addr") + parallel := confhelper.ConfIntOrFatal(conf, "mail", "parallel") if parallel <= 0 { log.Fatalln("mail.parallel must be > 0") } @@ -3,6 +3,7 @@ package main import ( "bytes" "fmt" + "kch42.de/gostuff/mailremind/confhelper" "kch42.de/gostuff/mailremind/model" "log" "path" @@ -24,10 +25,7 @@ var ( ) func initMails() { - tplroot, err := conf.GetString("paths", "mailtpls") - if err != nil { - log.Fatalf("Could not get paths.mailtpls from config: %s", err) - } + tplroot := confhelper.ConfStringOrFatal(conf, "paths", "mailtpls") mailActivationcode = loadMailTpl(tplroot, "activationcode") mailPwreset = loadMailTpl(tplroot, "pwreset") @@ -7,6 +7,7 @@ import ( "github.com/gorilla/mux" "github.com/gorilla/sessions" "github.com/kch42/simpleconf" + "kch42.de/gostuff/mailremind/confhelper" _ "kch42.de/gostuff/mailremind/model/mysql" "log" "math/rand" @@ -24,19 +25,13 @@ var baseurl string var SessionStorage sessions.Store func initSessions() { - _auth, err := conf.GetString("securecookies", "auth") - if err != nil { - log.Fatalf("Could not get securecookies.auth from config: %s", err) - } + _auth := confhelper.ConfStringOrFatal(conf, "securecookies", "auth") auth, err := hex.DecodeString(_auth) if err != nil { log.Fatalf("Could not decode securecookies.auth as hex: %s", err) } - _crypt, err := conf.GetString("securecookies", "crypt") - if err != nil { - log.Fatalf("Could not get securecookies.crypt from config: %s", err) - } + _crypt := confhelper.ConfStringOrFatal(conf, "securecookies", "crypt") crypt, err := hex.DecodeString(_crypt) if err != nil { log.Fatalf("Could not decode securecookies.auth as hex: %s", err) @@ -54,9 +49,7 @@ func main() { log.Fatalf("Could not read config: %s", err) } - if baseurl, err = conf.GetString("web", "baseurl"); err != nil { - log.Fatalf("Could not get web.baseurl from config: %s", err) - } + baseurl = confhelper.ConfStringOrFatal(conf, "web", "baseurl") rand.Seed(time.Now().UnixNano()) @@ -69,15 +62,8 @@ func main() { initLimits() defer dbcon.Close() - staticpath, err := conf.GetString("paths", "static") - if err != nil { - log.Fatalf("Could not get paths.static config: %s", err) - } - - laddr, err := conf.GetString("net", "laddr") - if err != nil { - log.Fatalf("Could not get net.laddr config: %s", err) - } + staticpath := confhelper.ConfStringOrFatal(conf, "paths", "static") + laddr := confhelper.ConfStringOrFatal(conf, "net", "laddr") initCheckjobs() go checkjobs() @@ -2,6 +2,7 @@ package main import ( "html/template" + "kch42.de/gostuff/mailremind/confhelper" "log" "path" ) @@ -29,10 +30,7 @@ var ( ) func initTpls() { - tplpath, err := conf.GetString("paths", "tpls") - if err != nil { - log.Fatalf("Could not get paths.tpls config: %s", err) - } + tplpath := confhelper.ConfStringOrFatal(conf, "paths", "tpls") tplRegister = loadTpl(tplpath, "register") tplMsg = loadTpl(tplpath, "msg") |