diff --git a/backend/main.go b/backend/main.go index e9ec6f1..3b30693 100644 --- a/backend/main.go +++ b/backend/main.go @@ -15,10 +15,18 @@ import ( var migrations embed.FS var db *Db +// ServerConfig holds configuration for the server. type ServerConfig struct { + // The datasource to the SQLite database. + // Use ":memory:" for an in-memory database. Datasource string - Port string - Started chan bool + + // The port to listen on. + // Use an empty string to listen on a random port. + Port string + + // The channel that gets signaled when the server has started. + Started chan bool } func getUsers(c *gin.Context) { @@ -84,6 +92,11 @@ func getUserGoals(c *gin.Context) { c.IndentedJSON(http.StatusOK, goals) } +/* +* +Initialises the database, and then starts the server. +If the context gets cancelled, the server is shutdown and the database is closed. +*/ func start(ctx context.Context, config *ServerConfig) { db = NewDb(config.Datasource) defer db.db.MustClose() @@ -104,7 +117,9 @@ func start(ctx context.Context, config *ServerConfig) { }() log.Printf("Running server on port %s\n", config.Port) - config.Started <- true + if config.Started != nil { + config.Started <- true + } <-ctx.Done() log.Println("Shutting down") if err := srv.Shutdown(context.Background()); err != nil {