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

60 lines
1.4 KiB
Go

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"
"gitea.urkob.com/urko/ess-etl-go/pkg/crono"
"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.EmployeeWorkInformationHandler
}
func NewRestServer(cfg *config.Config, cr *crono.Crono) *restServer {
return &restServer{
cfg: cfg,
cr: cr,
}
}
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)
})
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)
}
}