From 473052ed8f2c83052ed5b47a7f4cec68ac2621a6 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 13 Oct 2024 19:52:28 +0200 Subject: model: Replace global db variable by custom type Reducing global variables makes it easier to understand functions independently of the rest of the code. Adding the new model.DB type as a custom variant of the sql.DB type makes it possible to write methods for the database which makes the code way more readable. --- main.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index f744206..f648a2d 100644 --- a/main.go +++ b/main.go @@ -34,16 +34,16 @@ func main() { } log.Printf("Storage directory: %s\n", storage.Path) - model.ConnectDatabase(filepath.Join(storage.Path, "ceres.sqlite3")) - defer model.DisconnectDatabase() - model.MigrateDatabase(version) + db := model.OpenDB(filepath.Join(storage.Path, "ceres.sqlite3")) + defer db.Close() + db.Migrate(version) if flags.examples { - model.InjectExampleRecipes() - log.Println("Added example recipes") + db.CreateExamples() + log.Println("Created example recipes") } - server := NewServer(config.HttpAddress) + server := NewServer(config.HttpAddress, db) go server.Start() defer server.Stop() -- cgit v1.2.3-70-g09d2