🎨 prepare to make the database name configurable

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
This commit is contained in:
Florian Beisel 2024-01-17 22:24:18 +01:00
parent 49281f2ca8
commit 80d565e978
Signed by: florian
GPG Key ID: 79ECA2E54996FF4D
2 changed files with 8 additions and 4 deletions

View File

@ -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 {

View File

@ -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"