brunel/main.go

48 lines
739 B
Go
Raw Normal View History

2024-07-28 20:59:50 +02:00
package main
import (
"brunel/db"
"brunel/helpers"
"context"
"log/slog"
"os"
"os/signal"
)
func main() {
datab, err := db.New()
if err != nil {
panic("failed to connect database")
}
repo := db.NewRepository(datab)
helpers.DBInst = repo
// Run your server.
ctx := context.Background()
// trap Ctrl+C and call cancel on the context
ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer func() {
signal.Stop(c)
cancel()
}()
go func() {
select {
case <-c:
cancel()
case <-ctx.Done():
}
}()
go func() {
if err := runServer(ctx); err != nil {
slog.Error("Failed to start server!", "details", err.Error())
os.Exit(1)
}
}()
<-ctx.Done()
}