ess-etl-go/cmd/server/main.go

79 lines
1.8 KiB
Go
Raw Normal View History

package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
2023-04-05 19:04:52 +02:00
"gitea.urkob.com/urko/crono"
"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"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
2023-03-31 13:05:36 +02:00
cr := crono.New()
defer cr.Table()
cfg := config.NewConfig(".env")
2023-03-31 13:05:36 +02:00
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.
2023-04-03 19:01:32 +02:00
NewRestServer(cfg, cr).
WithEmployeeWIHandler(services.NewEmployeeWIService(ctx, professionalRepo)).
WithAMSHander()
2023-03-31 13:05:36 +02:00
cr.MarkAndRestart("dependencies loaded")
2023-04-03 19:01:32 +02:00
log.Println(cfg)
2023-03-31 13:05:36 +02:00
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
}