go: add some more startup checks for DB
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
e5980b15e5
commit
4de0576c26
8
error.go
Normal file
8
error.go
Normal file
@ -0,0 +1,8 @@
|
||||
package main
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
errDBNotConfigured = errors.New("Database connection string or database type were not set")
|
||||
errUnsupportedDBType = errors.New("DBTYPE can only be one of postgres or sqlite3")
|
||||
)
|
13
run.go
13
run.go
@ -111,6 +111,17 @@ func run() error {
|
||||
connstr := os.Getenv("PCMT_CONNSTRING")
|
||||
dbtype := os.Getenv("PCMT_DBTYPE")
|
||||
|
||||
// check and bail early.
|
||||
switch {
|
||||
case connstr == "" || dbtype == "":
|
||||
log.Errorf("PCMT_CONNSTRING or PCMT_DBTYPE or *both* were UNSET, bailing...")
|
||||
return errDBNotConfigured
|
||||
|
||||
case dbtype != "postgres" && dbtype != "sqlite3":
|
||||
log.Errorf("unsupported DB type specified, bailing...")
|
||||
return errUnsupportedDBType
|
||||
}
|
||||
|
||||
setting.SetDbConnstring(connstr)
|
||||
// type can be one of "postgres" or "sqlite3".
|
||||
setting.SetDbType(dbtype)
|
||||
@ -119,7 +130,7 @@ func run() error {
|
||||
|
||||
db, err := ent.Open(setting.DbType(), setting.DbConnstring())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open a connection to sqlite: %v", err)
|
||||
return fmt.Errorf("failed to open a connection to database: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user