2023-03-31 11:49:03 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
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"
|
|
|
|
"gitea.urkob.com/urko/ess-etl-go/internal/api/http/handler"
|
|
|
|
"gitea.urkob.com/urko/ess-etl-go/internal/services"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type restServer struct {
|
2023-04-05 12:18:07 +02:00
|
|
|
app *fiber.App
|
|
|
|
cfg *config.Config
|
|
|
|
cr *crono.Crono
|
|
|
|
employeeWIHdl *handler.EmployeeWIHandler
|
|
|
|
amsEmployeeWIHdl *handler.AMSEmployeeWIHandler
|
2023-03-31 11:49:03 +02:00
|
|
|
}
|
|
|
|
|
2023-04-03 19:01:32 +02:00
|
|
|
func NewRestServer(cfg *config.Config, cr *crono.Crono) *restServer {
|
2023-03-31 11:49:03 +02:00
|
|
|
return &restServer{
|
|
|
|
cfg: cfg,
|
2023-04-03 19:01:32 +02:00
|
|
|
cr: cr,
|
2023-03-31 11:49:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 12:18:07 +02:00
|
|
|
func (s *restServer) WithEmployeeWIHandler(employeeWISrv services.EmployeeWIService) *restServer {
|
|
|
|
s.employeeWIHdl = handler.NewEmployeeWIHandler(employeeWISrv)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-04-24 11:55:33 +02:00
|
|
|
func (s *restServer) WithAMSHandler() *restServer {
|
2023-04-05 12:18:07 +02:00
|
|
|
s.amsEmployeeWIHdl = handler.NewAMSEmployeeWIHandler(s.cfg.AmsApi, s.cfg.AmsApiKey)
|
2023-03-31 11:49:03 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
func (s *restServer) Start(apiPort, bearerToken string) error {
|
|
|
|
s.app = fiber.New()
|
|
|
|
|
|
|
|
s.app.Use(cors.New(cors.Config{
|
|
|
|
AllowMethods: "GET,POST,OPTIONS",
|
|
|
|
AllowOrigins: "*",
|
|
|
|
AllowHeaders: "Origin, Accept, Content-Type, X-CSRF-Token, Authorization",
|
|
|
|
ExposeHeaders: "Origin",
|
|
|
|
}))
|
|
|
|
|
|
|
|
s.app.Get("/employee_wi/:id", func(c *fiber.Ctx) error {
|
|
|
|
return s.employeeWIHdl.Get(c)
|
|
|
|
})
|
|
|
|
|
2023-04-05 12:18:07 +02:00
|
|
|
s.app.Get("/ams/employee_wi/:id", func(c *fiber.Ctx) error {
|
|
|
|
return s.amsEmployeeWIHdl.Get(c)
|
|
|
|
})
|
|
|
|
|
2023-04-03 19:01:32 +02:00
|
|
|
s.cr.MarkAndRestart("server up")
|
2023-03-31 11:49:03 +02:00
|
|
|
if err := s.app.Listen(":" + apiPort); err != nil {
|
|
|
|
return fmt.Errorf("app.Listen: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *restServer) Shutdown() {
|
|
|
|
if err := s.app.Server().Shutdown(); err != nil {
|
|
|
|
log.Printf("app.Server().Shutdown(): %s\n", err)
|
|
|
|
}
|
|
|
|
}
|