2023-03-31 11:49:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-10 11:43:13 +02:00
|
|
|
"flag"
|
2023-03-31 11:49:03 +02:00
|
|
|
"log"
|
2023-04-10 11:43:13 +02:00
|
|
|
"net/http"
|
2023-03-31 11:49:03 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2023-04-10 11:43:13 +02:00
|
|
|
"runtime/pprof"
|
|
|
|
"runtime/trace"
|
2023-03-31 11:49:03 +02:00
|
|
|
"syscall"
|
|
|
|
|
2023-04-05 19:04:52 +02:00
|
|
|
"gitea.urkob.com/urko/crono"
|
2023-03-31 11:49:03 +02:00
|
|
|
"gitea.urkob.com/urko/ess-etl-go/config"
|
2023-04-10 11:43:13 +02:00
|
|
|
apihttp "gitea.urkob.com/urko/ess-etl-go/internal/api/http"
|
2023-03-31 11:49:03 +02:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2023-04-10 11:43:13 +02:00
|
|
|
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
|
|
|
var traceflag = flag.String("trace", "", "write trace to file")
|
|
|
|
|
2023-03-31 11:49:03 +02:00
|
|
|
func main() {
|
2023-04-10 11:43:13 +02:00
|
|
|
flag.Parse()
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add pprof endpoints
|
|
|
|
go func() {
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
log.Println(http.ListenAndServe("localhost:6060", nil))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if *traceflag != "" {
|
|
|
|
log.Println("trace on")
|
|
|
|
trace.Start(os.Stdout)
|
|
|
|
defer func() {
|
|
|
|
log.Println("on stop")
|
|
|
|
trace.Stop()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-03-31 13:05:36 +02:00
|
|
|
cr := crono.New()
|
|
|
|
defer cr.Table()
|
2023-03-31 11:49:03 +02:00
|
|
|
cfg := config.NewConfig(".env")
|
2023-03-31 13:05:36 +02:00
|
|
|
ctx, cancel := context.WithCancel(signalContext(context.Background()))
|
|
|
|
defer cancel()
|
2023-03-31 11:49:03 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-04-10 11:43:13 +02:00
|
|
|
restServer := apihttp.
|
2023-04-03 19:01:32 +02:00
|
|
|
NewRestServer(cfg, cr).
|
2023-04-05 12:18:07 +02:00
|
|
|
WithEmployeeWIHandler(services.NewEmployeeWIService(ctx, professionalRepo)).
|
|
|
|
WithAMSHander()
|
2023-03-31 11:49:03 +02:00
|
|
|
|
2023-03-31 13:05:36 +02:00
|
|
|
cr.MarkAndRestart("dependencies loaded")
|
|
|
|
go func() {
|
|
|
|
if err = restServer.Start(cfg.ApiPort, ""); err != nil {
|
|
|
|
log.Fatalln("restServer.Start", err)
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}()
|
2023-03-31 11:49:03 +02:00
|
|
|
<-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
|
|
|
|
}
|