niki/delivery/http_server/server.go

58 lines
2.1 KiB
Go
Raw Normal View History

2023-12-16 07:20:01 +00:00
package httpserver
import (
"fmt"
config "git.gocasts.ir/ebhomengo/niki/config"
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor"
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req"
2024-01-02 15:35:26 +00:00
echo "github.com/labstack/echo/v4"
middleware "github.com/labstack/echo/v4/middleware"
)
2023-12-16 07:20:01 +00:00
type Server struct {
config config.Config
Router *echo.Echo
benefactorHandler benefactorhandler.Handler
benefactorkindboxreqhandler benefactorkindboxreqhandler.Handler
2023-12-16 07:20:01 +00:00
}
func New(
cfg config.Config,
benefactorSvc benefactorservice.Service,
benefactorVld benefactorvalidator.Validator,
authSvc authservice.Service,
benefactorKindBoxReqSvc benefactorkindboxreqservice.Service,
benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator,
) Server {
2023-12-16 07:20:01 +00:00
return Server{
Router: echo.New(),
config: cfg,
benefactorHandler: benefactorhandler.New(cfg.Auth, benefactorSvc, benefactorVld),
benefactorkindboxreqhandler: benefactorkindboxreqhandler.New(cfg.Auth, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld),
2023-12-16 07:20:01 +00:00
}
}
func (s Server) Serve() {
s.Router.Use(middleware.RequestID())
s.Router.Use(middleware.Recover())
// Routes
s.Router.GET("/health-check", s.healthCheck)
s.benefactorHandler.SetRoutes(s.Router)
s.benefactorkindboxreqhandler.SetRoutes(s.Router)
// Start server
address := fmt.Sprintf(":%d", s.config.HTTPServer.Port)
fmt.Printf("start echo server on %s\n", address)
if err := s.Router.Start(address); err != nil {
fmt.Println("router start error", err)
}
2023-12-20 15:39:25 +00:00
}