From 6355c9e8e17db8b6390930315051b994872b4f8e Mon Sep 17 00:00:00 2001 From: Hector Date: Sun, 7 Feb 2021 10:36:08 +0000 Subject: [PATCH] feat: fail on startup if database file does not exist (#8) Add a check when connecting to the sqlite3 database to ensure that the file exists before connecting. If the file does not exist, the connection fails. --- db/db.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/db/db.go b/db/db.go index 0d92edc..90e1788 100644 --- a/db/db.go +++ b/db/db.go @@ -3,6 +3,7 @@ package db import ( "database/sql" "log" + "os" ) const queryBadIpsPerJail = "SELECT j.name, (SELECT COUNT(1) FROM bips b WHERE j.name = b.jail) FROM jails j" @@ -14,6 +15,9 @@ type Fail2BanDB struct { } func MustConnectToDb(databasePath string) *Fail2BanDB { + if _, err := os.Stat(databasePath); os.IsNotExist(err) { + log.Fatalf("database path does not exist: %v", err) + } db, err := sql.Open("sqlite3", databasePath) if err != nil { log.Fatal(err)