Skip to content

Commit

Permalink
App exits nicely on ctrl C
Browse files Browse the repository at this point in the history
  • Loading branch information
airvine-leicom committed Mar 30, 2023
1 parent db55085 commit aae3aaf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,11 @@ func listenApi() {
apiserver.NewSpacesApiController(apiservices.NewSpacesApiService()),
)})
}
func listenApiRequests() {
err := nethttp.ListenAndServe(":"+common.Getenv("API_SERVER_PORT", "3000"), apiserver.NewRouter(
apiserver.NewConfigurationApiController(apiservices.NewConfigurationApiService()),
apiserver.NewVersionApiController(apiservices.NewVersionApiService()),
apiserver.NewCustomizationApiController(apiservices.NewCustomizationApiService()),
))
log.Fatal("main", "Error in API Server: %v", err)
}
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ import (
"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"
"github.com/volatiletech/sqlboiler/v4/boil"
)

// The main function starts the app by starting all services necessary for this app and waits
// until all services are finished.
func main() {
log.Info("main", "Starting the app.")

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"),
Expand All @@ -43,14 +49,17 @@ func main() {
)
common.WaitForWithOs(
common.Loop(CheckConfigsandSetActiveState, time.Second),
listenApi,
listenApiRequests,
)

// At the end set all configuration inactive
_, err := conf.SetAllConfigsInactive(context.Background())
if err != nil {
log.Error("conf", "setting all configs inactive: %v", err)
}

log.Info("main", "Terminate the app.")

}


0 comments on commit aae3aaf

Please sign in to comment.