Fix error printing

This commit is contained in:
r 2020-01-28 20:50:14 +00:00
parent 4613e9cc7b
commit a877c58ac3

25
main.go
View file

@ -1,6 +1,8 @@
package main package main
import ( import (
"errors"
"fmt"
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
@ -25,10 +27,15 @@ func init() {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
} }
func errExit(err error) {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
func main() { func main() {
opts, _, err := util.Getopts(os.Args, "f:") opts, _, err := util.Getopts(os.Args, "f:")
if err != nil { if err != nil {
log.Fatal(err) errExit(err)
} }
for _, opt := range opts { for _, opt := range opts {
@ -40,34 +47,34 @@ func main() {
config, err := config.ParseFile(configFile) config, err := config.ParseFile(configFile)
if err != nil { if err != nil {
log.Fatal(err) errExit(err)
} }
if !config.IsValid() { if !config.IsValid() {
log.Fatal("invalid config") errExit(errors.New("invalid config"))
} }
templatesGlobPattern := filepath.Join(config.TemplatesPath, "*") templatesGlobPattern := filepath.Join(config.TemplatesPath, "*")
renderer, err := renderer.NewRenderer(templatesGlobPattern) renderer, err := renderer.NewRenderer(templatesGlobPattern)
if err != nil { if err != nil {
log.Fatal(err) errExit(err)
} }
err = os.Mkdir(config.DatabasePath, 0755) err = os.Mkdir(config.DatabasePath, 0755)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
log.Fatal(err) errExit(err)
} }
sessionDBPath := filepath.Join(config.DatabasePath, "session") sessionDBPath := filepath.Join(config.DatabasePath, "session")
sessionDB, err := kv.NewDatabse(sessionDBPath) sessionDB, err := kv.NewDatabse(sessionDBPath)
if err != nil { if err != nil {
log.Fatal(err) errExit(err)
} }
appDBPath := filepath.Join(config.DatabasePath, "app") appDBPath := filepath.Join(config.DatabasePath, "app")
appDB, err := kv.NewDatabse(appDBPath) appDB, err := kv.NewDatabse(appDBPath)
if err != nil { if err != nil {
log.Fatal(err) errExit(err)
} }
sessionRepo := repo.NewSessionRepo(sessionDB) sessionRepo := repo.NewSessionRepo(sessionDB)
@ -85,7 +92,7 @@ func main() {
} else { } else {
lf, err := os.Open(config.LogFile) lf, err := os.Open(config.LogFile)
if err != nil { if err != nil {
log.Fatal(err) errExit(err)
} }
defer lf.Close() defer lf.Close()
logger = log.New(lf, "", log.LstdFlags) logger = log.New(lf, "", log.LstdFlags)
@ -101,6 +108,6 @@ func main() {
logger.Println("listening on", config.ListenAddress) logger.Println("listening on", config.ListenAddress)
err = http.ListenAndServe(config.ListenAddress, handler) err = http.ListenAndServe(config.ListenAddress, handler)
if err != nil { if err != nil {
log.Fatal(err) errExit(err)
} }
} }