Add documentation

This commit is contained in:
Sebastiaan de Schaetzen 2025-05-08 11:50:25 +02:00
parent 7638d9d0af
commit da8d099397

View File

@ -15,9 +15,17 @@ import (
var migrations embed.FS var migrations embed.FS
var db *Db var db *Db
// ServerConfig holds configuration for the server.
type ServerConfig struct { type ServerConfig struct {
// The datasource to the SQLite database.
// Use ":memory:" for an in-memory database.
Datasource string Datasource string
// The port to listen on.
// Use an empty string to listen on a random port.
Port string Port string
// The channel that gets signaled when the server has started.
Started chan bool Started chan bool
} }
@ -84,6 +92,11 @@ func getUserGoals(c *gin.Context) {
c.IndentedJSON(http.StatusOK, goals) 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) { func start(ctx context.Context, config *ServerConfig) {
db = NewDb(config.Datasource) db = NewDb(config.Datasource)
defer db.db.MustClose() 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) log.Printf("Running server on port %s\n", config.Port)
if config.Started != nil {
config.Started <- true config.Started <- true
}
<-ctx.Done() <-ctx.Done()
log.Println("Shutting down") log.Println("Shutting down")
if err := srv.Shutdown(context.Background()); err != nil { if err := srv.Shutdown(context.Background()); err != nil {