use log.Fatal instead panic on config errors
This commit is contained in:
parent
2dc0f02d1c
commit
fce1b99683
7 changed files with 22 additions and 28 deletions
|
|
@ -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_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_EXPIRY | time.Duration | 15m | jwt access token expiry
|
||||||
AUTH_JWT_REFRESH_EXPIRY | time.Duration | 1h | jwt refresh 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_PORT | int || email smtp port
|
||||||
EMAIL_SMTP_USER | string || email smtp username
|
EMAIL_SMTP_USER | string || email smtp username
|
||||||
EMAIL_SMTP_PASSWORD | string || email smtp password
|
EMAIL_SMTP_PASSWORD | string || email smtp password
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/dhax/go-base/api"
|
"github.com/dhax/go-base/api"
|
||||||
"github.com/go-chi/docgen"
|
"github.com/go-chi/docgen"
|
||||||
|
|
@ -67,8 +68,8 @@ func genRoutesDoc() {
|
||||||
Intro: "GoBase REST API.",
|
Intro: "GoBase REST API.",
|
||||||
})
|
})
|
||||||
if err := ioutil.WriteFile("routes.md", []byte(md), 0644); err != nil {
|
if err := ioutil.WriteFile("routes.md", []byte(md), 0644); err != nil {
|
||||||
fmt.Print(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Print("OK\n")
|
fmt.Println("OK")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
homedir "github.com/mitchellh/go-homedir"
|
homedir "github.com/mitchellh/go-homedir"
|
||||||
|
|
@ -76,8 +77,7 @@ func initConfig() {
|
||||||
// Find home directory.
|
// Find home directory.
|
||||||
home, err := homedir.Dir()
|
home, err := homedir.Dir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search config in home directory with name ".go-base" (without extension).
|
// Search config in home directory with name ".go-base" (without extension).
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,8 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/dhax/go-base/api"
|
"github.com/dhax/go-base/api"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
@ -28,7 +30,7 @@ var serveCmd = &cobra.Command{
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
server, err := api.NewServer()
|
server, err := api.NewServer()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
server.Start()
|
server.Start()
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
package migrate
|
package migrate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/dhax/go-base/database"
|
"github.com/dhax/go-base/database"
|
||||||
"github.com/go-pg/migrations"
|
"github.com/go-pg/migrations"
|
||||||
|
|
@ -13,7 +12,7 @@ import (
|
||||||
func Migrate(args []string) {
|
func Migrate(args []string) {
|
||||||
db, err := database.DBConn()
|
db, err := database.DBConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = db.RunInTransaction(func(tx *pg.Tx) error {
|
err = db.RunInTransaction(func(tx *pg.Tx) error {
|
||||||
|
|
@ -22,14 +21,14 @@ func Migrate(args []string) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if newVersion != oldVersion {
|
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 {
|
} else {
|
||||||
fmt.Printf("version is %d\n", oldVersion)
|
log.Printf("version is %d\n", oldVersion)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exitf(err.Error())
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -38,12 +37,12 @@ func Migrate(args []string) {
|
||||||
func Reset() {
|
func Reset() {
|
||||||
db, err := database.DBConn()
|
db, err := database.DBConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exitf(err.Error())
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
version, err := migrations.Version(db)
|
version, err := migrations.Version(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exitf(err.Error())
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = db.RunInTransaction(func(tx *pg.Tx) error {
|
err = db.RunInTransaction(func(tx *pg.Tx) error {
|
||||||
|
|
@ -52,21 +51,12 @@ func Reset() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
version = newVersion
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != 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)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
@ -28,7 +28,7 @@ func DBConn() (*pg.DB, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Printf("%s %s\n", time.Since(event.StartTime), query)
|
log.Printf("%s %s\n", time.Since(event.StartTime), query)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -39,7 +40,7 @@ func NewLogger() *logrus.Logger {
|
||||||
}
|
}
|
||||||
l, err := logrus.ParseLevel(level)
|
l, err := logrus.ParseLevel(level)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
Logger.Level = l
|
Logger.Level = l
|
||||||
return Logger
|
return Logger
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue