2023-03-31 11:49:03 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"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"
|
2023-04-03 19:01:32 +02:00
|
|
|
"gitea.urkob.com/urko/ess-etl-go/pkg/crono"
|
2023-03-31 11:49:03 +02:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type restServer struct {
|
|
|
|
app *fiber.App
|
|
|
|
cfg *config.Config
|
2023-04-03 19:01:32 +02:00
|
|
|
cr *crono.Crono
|
2023-03-31 11:49:03 +02:00
|
|
|
employeeWIHdl *handler.EmployeeWorkInformationHandler
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *restServer) WithProfessionalHandler(employeeWISrv services.EmployeeWIService) *restServer {
|
|
|
|
s.employeeWIHdl = handler.NewEmployeeWorkInformation(employeeWISrv)
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
}
|