use log.Fatal instead panic on config errors

This commit is contained in:
dhax 2017-10-19 00:40:25 +02:00
parent 2dc0f02d1c
commit fce1b99683
7 changed files with 22 additions and 28 deletions

View file

@ -65,7 +65,7 @@ AUTH_LOGIN_TOKEN_EXPIRY | time.Duration | 11m | login token expiry
AUTH_JWT_SECRET | string | random | jwt sign and verify key - value "random" creates random 32 char secret at startup (and automatically invalidates existing tokens on app restarts, so during dev you might want to set a fixed value here)
AUTH_JWT_EXPIRY | time.Duration | 15m | jwt access token expiry
AUTH_JWT_REFRESH_EXPIRY | time.Duration | 1h | jwt refresh token expiry
EMAIL_SMTP_HOST | string || email smtp host (if set and connection can't be established then app panics)
EMAIL_SMTP_HOST | string || email smtp host (if set and connection can't be established then app exits)
EMAIL_SMTP_PORT | int || email smtp port
EMAIL_SMTP_USER | string || email smtp username
EMAIL_SMTP_PASSWORD | string || email smtp password

View file

@ -17,6 +17,7 @@ package cmd
import (
"fmt"
"io/ioutil"
"log"
"github.com/dhax/go-base/api"
"github.com/go-chi/docgen"
@ -67,8 +68,8 @@ func genRoutesDoc() {
Intro: "GoBase REST API.",
})
if err := ioutil.WriteFile("routes.md", []byte(md), 0644); err != nil {
fmt.Print(err)
log.Println(err)
return
}
fmt.Print("OK\n")
fmt.Println("OK")
}

View file

@ -16,6 +16,7 @@ package cmd
import (
"fmt"
"log"
"os"
homedir "github.com/mitchellh/go-homedir"
@ -76,8 +77,7 @@ func initConfig() {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
log.Fatal(err)
}
// Search config in home directory with name ".go-base" (without extension).

View file

@ -15,6 +15,8 @@
package cmd
import (
"log"
"github.com/dhax/go-base/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -28,7 +30,7 @@ var serveCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
server, err := api.NewServer()
if err != nil {
panic(err)
log.Fatal(err)
}
server.Start()
},

View file

@ -1,8 +1,7 @@
package migrate
import (
"fmt"
"os"
"log"
"github.com/dhax/go-base/database"
"github.com/go-pg/migrations"
@ -13,7 +12,7 @@ import (
func Migrate(args []string) {
db, err := database.DBConn()
if err != nil {
panic(err)
log.Fatal(err)
}
err = db.RunInTransaction(func(tx *pg.Tx) error {
@ -22,14 +21,14 @@ func Migrate(args []string) {
return err
}
if newVersion != oldVersion {
fmt.Printf("migrated from version %d to %d\n", oldVersion, newVersion)
log.Printf("migrated from version %d to %d\n", oldVersion, newVersion)
} else {
fmt.Printf("version is %d\n", oldVersion)
log.Printf("version is %d\n", oldVersion)
}
return nil
})
if err != nil {
exitf(err.Error())
log.Fatal(err)
}
}
@ -38,12 +37,12 @@ func Migrate(args []string) {
func Reset() {
db, err := database.DBConn()
if err != nil {
exitf(err.Error())
log.Fatal(err)
}
version, err := migrations.Version(db)
if err != nil {
exitf(err.Error())
log.Fatal(err)
}
err = db.RunInTransaction(func(tx *pg.Tx) error {
@ -52,21 +51,12 @@ func Reset() {
if err != nil {
return err
}
fmt.Printf("migrated from version %d to %d\n", oldVersion, newVersion)
log.Printf("migrated from version %d to %d\n", oldVersion, newVersion)
version = newVersion
}
return nil
})
if err != nil {
exitf(err.Error())
log.Fatal(err)
}
}
func errorf(s string, args ...interface{}) {
fmt.Fprintf(os.Stderr, s+"\n", args...)
}
func exitf(s string, args ...interface{}) {
errorf(s, args...)
os.Exit(1)
}

View file

@ -1,7 +1,7 @@
package database
import (
"fmt"
"log"
"time"
"github.com/spf13/viper"
@ -28,7 +28,7 @@ func DBConn() (*pg.DB, error) {
if err != nil {
panic(err)
}
fmt.Printf("%s %s\n", time.Since(event.StartTime), query)
log.Printf("%s %s\n", time.Since(event.StartTime), query)
})
}

View file

@ -2,6 +2,7 @@ package logging
import (
"fmt"
"log"
"net/http"
"time"
@ -39,7 +40,7 @@ func NewLogger() *logrus.Logger {
}
l, err := logrus.ParseLevel(level)
if err != nil {
panic(err)
log.Fatal(err)
}
Logger.Level = l
return Logger