ess-etl-go/internal/api/http/server.go

70 lines
1.7 KiB
Go

package http
import (
"fmt"
"log"
"gitea.urkob.com/urko/crono"
"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 {
app *fiber.App
cfg *config.Config
cr *crono.Crono
employeeWIHdl *handler.EmployeeWIHandler
amsEmployeeWIHdl *handler.AMSEmployeeWIHandler
}
func NewRestServer(cfg *config.Config, cr *crono.Crono) *restServer {
return &restServer{
cfg: cfg,
cr: cr,
}
}
func (s *restServer) WithEmployeeWIHandler(employeeWISrv services.EmployeeWIService) *restServer {
s.employeeWIHdl = handler.NewEmployeeWIHandler(employeeWISrv)
return s
}
func (s *restServer) WithAMSHander() *restServer {
s.amsEmployeeWIHdl = handler.NewAMSEmployeeWIHandler(s.cfg.AmsApi, s.cfg.AmsApiKey)
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)
})
s.app.Get("/ams/employee_wi/:id", func(c *fiber.Ctx) error {
return s.amsEmployeeWIHdl.Get(c)
})
s.cr.MarkAndRestart("server up")
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)
}
}