48 lines
739 B
Go
48 lines
739 B
Go
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()
|
|
}
|