Skip to content

Commit

Permalink
support init connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-goebel committed Feb 21, 2024
1 parent b933a80 commit c24812b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
23 changes: 11 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}

0 comments on commit c24812b

Please sign in to comment.