🎨 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:
		
							
								
								
									
										7
									
								
								db/db.go
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								db/db.go
									
									
									
									
									
								
							@@ -4,6 +4,7 @@ import (
 | 
				
			|||||||
	"database/sql"
 | 
						"database/sql"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.beisel.it/florian/hostname-service/models"
 | 
						"git.beisel.it/florian/hostname-service/models"
 | 
				
			||||||
@@ -17,16 +18,16 @@ import (
 | 
				
			|||||||
var DB *sql.DB
 | 
					var DB *sql.DB
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Initialize the database and create tables if they don't exist
 | 
					// Initialize the database and create tables if they don't exist
 | 
				
			||||||
func Init() {
 | 
					func Init(db string) {
 | 
				
			||||||
	var err error
 | 
						var err error
 | 
				
			||||||
	DB, err = sql.Open("sqlite3", "hostname-service.db")
 | 
						DB, err = sql.Open("sqlite3", db)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatalf("Error opening database: %v", err)
 | 
							log.Fatalf("Error opening database: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	m, err := migrate.New(
 | 
						m, err := migrate.New(
 | 
				
			||||||
		"file://db/migrations/",
 | 
							"file://db/migrations/",
 | 
				
			||||||
		"sqlite3://hostname-service.db",
 | 
							fmt.Sprintf("sqlite3://%s", db),
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										5
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								main.go
									
									
									
									
									
								
							@@ -29,9 +29,12 @@ import (
 | 
				
			|||||||
//	@description				Type "Bearer" followed by a space and JWT token.
 | 
					//	@description				Type "Bearer" followed by a space and JWT token.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	db.Init()
 | 
						gin.SetMode(gin.DebugMode)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						db.Init("hostname-service.db")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	router := gin.Default()
 | 
						router := gin.Default()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	docs.SwaggerInfo.Host = "localhost:8080"
 | 
						docs.SwaggerInfo.Host = "localhost:8080"
 | 
				
			||||||
	docs.SwaggerInfo.BasePath = "/api/v1"
 | 
						docs.SwaggerInfo.BasePath = "/api/v1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user