From 4de0576c267c65ad53509765c124c443bcb49331 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 17 May 2023 13:35:13 +0200 Subject: [PATCH] go: add some more startup checks for DB --- error.go | 8 ++++++++ run.go | 13 ++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 error.go diff --git a/error.go b/error.go new file mode 100644 index 0000000..ea895bd --- /dev/null +++ b/error.go @@ -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") +) diff --git a/run.go b/run.go index da13f0b..0853c28 100644 --- a/run.go +++ b/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()