From 80d565e978b50b6ff0ca6917650d6f0f15ca1e78 Mon Sep 17 00:00:00 2001 From: Florian Beisel Date: Wed, 17 Jan 2024 22:24:18 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20prepare=20to=20make=20the=20data?= =?UTF-8?q?base=20name=20configurable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We should not hardcode the database name at different places in db.Init(). This will allow to make the database path configurable in a later change, and importantly prevent discrepancies --- db/db.go | 7 ++++--- main.go | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/db/db.go b/db/db.go index fea61bd..ec47bc9 100644 --- a/db/db.go +++ b/db/db.go @@ -4,6 +4,7 @@ import ( "database/sql" "encoding/json" "errors" + "fmt" "log" "git.beisel.it/florian/hostname-service/models" @@ -17,16 +18,16 @@ import ( var DB *sql.DB // Initialize the database and create tables if they don't exist -func Init() { +func Init(db string) { var err error - DB, err = sql.Open("sqlite3", "hostname-service.db") + DB, err = sql.Open("sqlite3", db) if err != nil { log.Fatalf("Error opening database: %v", err) } m, err := migrate.New( "file://db/migrations/", - "sqlite3://hostname-service.db", + fmt.Sprintf("sqlite3://%s", db), ) if err != nil { diff --git a/main.go b/main.go index 9437d52..8663228 100644 --- a/main.go +++ b/main.go @@ -29,9 +29,12 @@ import ( // @description Type "Bearer" followed by a space and JWT token. func main() { - db.Init() + gin.SetMode(gin.DebugMode) + + db.Init("hostname-service.db") router := gin.Default() + docs.SwaggerInfo.Host = "localhost:8080" docs.SwaggerInfo.BasePath = "/api/v1"