From c24812bf269ccacc175a052e485ba4de3e6238f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20G=C3=B6bel?= Date: Wed, 21 Feb 2024 15:15:23 +0100 Subject: [PATCH] support init connection string --- README.md | 6 +++--- app.go | 18 ++++++++++++++++++ main.go | 23 +++++++++++------------ metadata.json | 7 ++++++- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 9d53bb4..4507f39 100644 --- a/README.md +++ b/README.md @@ -16,10 +16,10 @@ To start and initialize an app in an Eliona environment, the app has to be regis ### Environment variables -- `APPNAME`: must be set to `thingdust`. Some resources use this name to identify the app inside an Eliona environment. - -- `CONNECTION_STRING`: configures the [Eliona database](https://github.com/eliona-smart-building-assistant/go-eliona/tree/main/db). Otherwise, the app can't be initialized and started. (e.g. `postgres://user:pass@localhost:5432/iot`) +- `CONNECTION_STRING`: configures the [Eliona database](https://github.com/eliona-smart-building-assistant/go-eliona/tree/main/db). Otherwise, the app can't be initialized and started (e.g. `postgres://user:pass@localhost:5432/iot`). +- `INIT_CONNECTION_STRING`: configures the [Eliona database](https://github.com/eliona-smart-building-assistant/go-eliona/tree/main/db) for app initialization like creating schema and tables (e.g. `postgres://user:pass@localhost:5432/iot`). Default is content of `CONNECTION_STRING`. + - `API_ENDPOINT`: configures the endpoint to access the [Eliona API v2](https://github.com/eliona-smart-building-assistant/eliona-api). Otherwise, the app can't be initialized and started. (e.g. `http://api-v2:3000/v2`) - `API_TOKEN`: defines the secret to authenticate the app and access the API. diff --git a/app.go b/app.go index 783f6fb..5710b1c 100644 --- a/app.go +++ b/app.go @@ -17,6 +17,8 @@ package main import ( "context" + "github.com/eliona-smart-building-assistant/go-eliona/app" + "github.com/eliona-smart-building-assistant/go-utils/db" utilshttp "github.com/eliona-smart-building-assistant/go-utils/http" nethttp "net/http" "thingdust/apiserver" @@ -33,6 +35,22 @@ import ( "github.com/eliona-smart-building-assistant/go-utils/log" ) +func initialization() { + ctx := context.Background() + + // Necessary to close used init resources + conn := db.NewInitConnectionWithContextAndApplicationName(ctx, app.AppName()) + defer conn.Close(ctx) + + // Init the app before the first run. + app.Init(db.Pool(), app.AppName(), + asset.InitAssetTypeFile("eliona/asset-type-thingdust_space.json"), + app.ExecSqlFile("conf/init.sql"), + conf.InitConfiguration, + eliona.InitEliona, + ) +} + func CheckConfigsandSetActiveState() { configs, err := conf.GetConfigs(context.Background()) if err != nil { diff --git a/main.go b/main.go index 115d82e..fe46ab6 100644 --- a/main.go +++ b/main.go @@ -18,11 +18,9 @@ package main import ( "context" "thingdust/conf" - "thingdust/eliona" "time" "github.com/eliona-smart-building-assistant/go-eliona/app" - "github.com/eliona-smart-building-assistant/go-eliona/asset" "github.com/eliona-smart-building-assistant/go-utils/common" "github.com/eliona-smart-building-assistant/go-utils/db" "github.com/eliona-smart-building-assistant/go-utils/log" @@ -37,16 +35,17 @@ func main() { database := db.Database(app.AppName()) defer database.Close() boil.SetDB(database) - // Necessary to close used init resources, because db.Pool() is used in this app. - defer db.ClosePool() - - // Init the app before the first run. - app.Init(db.Pool(), app.AppName(), - asset.InitAssetTypeFile("eliona/asset-type-thingdust_space.json"), - app.ExecSqlFile("conf/init.sql"), - conf.InitConfiguration, - eliona.InitEliona, - ) + + // Set the database logging level. + if log.Lev() >= log.TraceLevel { + boil.DebugMode = true + boil.DebugWriter = log.GetWriter(log.TraceLevel, "database") + } + + // Initialize the app + initialization() + + // Starting the service to collect the data for each configured Hailo Smart Hub. common.WaitForWithOs( common.Loop(CheckConfigsandSetActiveState, time.Second), listenApiRequests, diff --git a/metadata.json b/metadata.json index f088f34..f4440d5 100644 --- a/metadata.json +++ b/metadata.json @@ -15,5 +15,10 @@ "apiUrl": "v1", "apiSpecificationPath": "/version/openapi.json", "documentationUrl": "https://doc.eliona.io/collection/eliona-apps/externe-apps/thingdust", - "useEnvironment": [ "CONNECTION_STRING", "API_ENDPOINT", "API_TOKEN" ] + "useEnvironment": [ + "CONNECTION_STRING", + "INIT_CONNECTION_STRING", + "API_ENDPOINT", + "API_TOKEN" + ] }