72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
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"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func main() {
|
|
cfg := config.NewConfig(".env")
|
|
ctx := context.Background()
|
|
|
|
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).
|
|
WithProfessionalHandler(services.NewEmployeeWIService(ctx, professionalRepo))
|
|
|
|
if err = restServer.Start(cfg.ApiPort, ""); err != nil {
|
|
log.Fatalln("restServer.Start", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(signalContext(context.Background()))
|
|
defer 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
|
|
}
|