2023-12-16 07:20:01 +00:00
|
|
|
package httpserver
|
|
|
|
|
2024-01-02 14:04:16 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2024-01-15 10:33:24 +00:00
|
|
|
|
|
|
|
config "git.gocasts.ir/ebhomengo/niki/config"
|
2024-01-14 15:53:37 +00:00
|
|
|
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
|
|
|
|
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
|
|
|
|
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
|
2024-01-02 15:35:26 +00:00
|
|
|
echo "github.com/labstack/echo/v4"
|
|
|
|
middleware "github.com/labstack/echo/v4/middleware"
|
2024-01-02 14:04:16 +00:00
|
|
|
)
|
2023-12-16 07:20:01 +00:00
|
|
|
|
|
|
|
type Server struct {
|
2024-01-14 15:53:37 +00:00
|
|
|
config config.Config
|
|
|
|
Router *echo.Echo
|
|
|
|
benefactorHandler benefactorhandler.Handler
|
2023-12-16 07:20:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 15:53:37 +00:00
|
|
|
func New(cfg config.Config, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator) Server {
|
2023-12-16 07:20:01 +00:00
|
|
|
return Server{
|
2024-01-14 15:53:37 +00:00
|
|
|
Router: echo.New(),
|
|
|
|
config: cfg,
|
|
|
|
benefactorHandler: benefactorhandler.New(cfg.Auth, benefactorSvc, benefactorVld),
|
2023-12-16 07:20:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) Serve() {
|
2024-01-02 14:04:16 +00:00
|
|
|
s.Router.Use(middleware.RequestID())
|
|
|
|
|
|
|
|
s.Router.Use(middleware.Recover())
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
s.Router.GET("/health-check", s.healthCheck)
|
2024-01-14 15:53:37 +00:00
|
|
|
s.benefactorHandler.SetRoutes(s.Router)
|
2024-01-02 14:04:16 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|