package main import ( "context" "log" "os" "os/signal" "syscall" "gitea.urkob.com/urko/ess-etl-go/config" "gitea.urkob.com/urko/ess-etl-go/internal/api/http" "gitea.urkob.com/urko/ess-etl-go/internal/services" "gitea.urkob.com/urko/ess-etl-go/pkg/adapter/repository/mongodb/employee_wi" "gitea.urkob.com/urko/ess-etl-go/pkg/crono" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { cr := crono.New() defer cr.Table() cfg := config.NewConfig(".env") ctx, cancel := context.WithCancel(signalContext(context.Background())) defer cancel() dbOpts := options.Client() dbOpts.ApplyURI(cfg.DbAddress) client, err := mongo.NewClient(dbOpts) if err != nil { log.Fatalln("mongo.NewClient", err) } log.Println("mongodb client is connected") if err = client.Connect(ctx); err != nil { log.Fatalln("client.Connect", err) } employeeWICollection := client.Database(cfg.DbName).Collection(cfg.EmployeeWorkInformationCollection) professionalRepo := employee_wi.NewRepo(employeeWICollection) restServer := http. NewRestServer(cfg, cr). WithEmployeeWIHandler(services.NewEmployeeWIService(ctx, professionalRepo)). WithAMSHander() cr.MarkAndRestart("dependencies loaded") log.Println(cfg) go func() { if err = restServer.Start(cfg.ApiPort, ""); err != nil { log.Fatalln("restServer.Start", err) cancel() } }() <-ctx.Done() restServer.Shutdown() log.Println("gracefully shutdown") } func signalContext(ctx context.Context) context.Context { ctx, cancel := context.WithCancel(ctx) sigs := make(chan os.Signal, 1) signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) go func() { log.Println("listening for shutdown signal") <-sigs log.Println("shutdown signal received") signal.Stop(sigs) close(sigs) cancel() }() return ctx }