go: add some more startup checks for DB
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
leo 2023-05-17 13:35:13 +02:00
parent e5980b15e5
commit 4de0576c26
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
2 changed files with 20 additions and 1 deletions

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

@ -111,6 +111,17 @@ func run() error {
connstr := os.Getenv("PCMT_CONNSTRING") connstr := os.Getenv("PCMT_CONNSTRING")
dbtype := os.Getenv("PCMT_DBTYPE") 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) setting.SetDbConnstring(connstr)
// type can be one of "postgres" or "sqlite3". // type can be one of "postgres" or "sqlite3".
setting.SetDbType(dbtype) setting.SetDbType(dbtype)
@ -119,7 +130,7 @@ func run() error {
db, err := ent.Open(setting.DbType(), setting.DbConnstring()) db, err := ent.Open(setting.DbType(), setting.DbConnstring())
if err != nil { 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() defer db.Close()