2023-12-16 07:20:01 +00:00
|
|
|
package httpserver
|
|
|
|
|
2024-01-02 14:04:16 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2024-09-15 07:36:09 +00:00
|
|
|
agentkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/agent/kind_box_req"
|
2024-06-14 08:41:36 +00:00
|
|
|
|
2024-07-24 23:45:04 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/config"
|
2024-01-22 08:14:57 +00:00
|
|
|
adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin"
|
2024-08-01 10:20:18 +00:00
|
|
|
adminagenthandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/agent"
|
2024-07-01 17:13:28 +00:00
|
|
|
adminKindBoxHandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box"
|
2024-01-22 08:14:57 +00:00
|
|
|
adminkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box_req"
|
2024-07-13 12:33:07 +00:00
|
|
|
agentkindboxhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/agent/kind_box"
|
2024-01-22 14:41:55 +00:00
|
|
|
benefactoraddresshandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/address"
|
2024-01-14 15:53:37 +00:00
|
|
|
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
|
2024-06-25 21:58:11 +00:00
|
|
|
benefactorkindboxhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box"
|
2024-01-16 16:13:06 +00:00
|
|
|
benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req"
|
2024-05-14 13:07:09 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/docs"
|
2024-08-01 10:20:18 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/service"
|
2024-01-02 15:35:26 +00:00
|
|
|
echo "github.com/labstack/echo/v4"
|
2024-08-06 11:49:46 +00:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2024-05-14 13:07:09 +00:00
|
|
|
echoSwagger "github.com/swaggo/echo-swagger"
|
2024-01-02 14:04:16 +00:00
|
|
|
)
|
2023-12-16 07:20:01 +00:00
|
|
|
|
|
|
|
type Server struct {
|
2024-01-16 16:13:06 +00:00
|
|
|
config config.Config
|
|
|
|
Router *echo.Echo
|
2024-01-19 16:56:11 +00:00
|
|
|
adminHandler adminhandler.Handler
|
2024-01-22 08:14:57 +00:00
|
|
|
adminKindBoxReqHandler adminkindboxreqhandler.Handler
|
2024-07-01 17:13:28 +00:00
|
|
|
adminKindBoxHandler adminKindBoxHandler.Handler
|
2024-08-01 10:20:18 +00:00
|
|
|
adminAgentHandler adminagenthandler.Handler
|
2024-07-13 12:33:07 +00:00
|
|
|
agentKindBoxHandler agentkindboxhandler.Handler
|
2024-09-15 07:36:09 +00:00
|
|
|
agentKindBoxReqHandler agentkindboxreqhandler.Handler
|
2024-08-01 10:20:18 +00:00
|
|
|
benefactorHandler benefactorhandler.Handler
|
|
|
|
benefactorKindBoxReqHandler benefactorkindboxreqhandler.Handler
|
|
|
|
benefactorAddressHandler benefactoraddresshandler.Handler
|
|
|
|
benefactorKindBoxHandler benefactorkindboxhandler.Handler
|
2023-12-16 07:20:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 16:13:06 +00:00
|
|
|
func New(
|
|
|
|
cfg config.Config,
|
2024-08-01 10:20:18 +00:00
|
|
|
svc *service.Service,
|
|
|
|
) *Server {
|
|
|
|
return &Server{
|
2024-01-16 16:13:06 +00:00
|
|
|
Router: echo.New(),
|
|
|
|
config: cfg,
|
2024-08-01 10:20:18 +00:00
|
|
|
adminHandler: adminhandler.New(svc.AdminAuthSvc, svc.AdminSvc, svc.AdminAuthorizeSvc),
|
|
|
|
adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
|
2024-08-06 21:41:09 +00:00
|
|
|
adminKindBoxHandler: adminKindBoxHandler.New(svc.AdminAuthSvc, svc.AdminKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
|
2024-08-01 10:20:18 +00:00
|
|
|
adminAgentHandler: adminagenthandler.New(svc.AdminAuthSvc, svc.AdminAgentSvc, svc.AdminAuthorizeSvc),
|
2024-09-16 14:56:05 +00:00
|
|
|
agentKindBoxHandler: agentkindboxhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
|
|
|
|
agentKindBoxReqHandler: agentkindboxreqhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
|
2024-08-01 10:20:18 +00:00
|
|
|
benefactorHandler: benefactorhandler.New(svc.BenefactorAuthSvc, svc.BenefactorSvc),
|
2024-08-16 17:37:37 +00:00
|
|
|
benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(svc.BenefactorAuthSvc, svc.BenefactorKindBoxReqSvc, svc.NotificationSvc),
|
2024-08-01 10:20:18 +00:00
|
|
|
benefactorAddressHandler: benefactoraddresshandler.New(svc.BenefactorAuthSvc, svc.BenefactorAddressSvc),
|
2024-08-16 17:37:37 +00:00
|
|
|
benefactorKindBoxHandler: benefactorkindboxhandler.New(svc.BenefactorAuthSvc, svc.BenefactorKindBoxSvc, svc.NotificationSvc),
|
2023-12-16 07:20:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) Serve() {
|
2024-08-06 11:49:46 +00:00
|
|
|
s.RegisterRoutes()
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) RegisterRoutes() {
|
2024-01-02 14:04:16 +00:00
|
|
|
s.Router.Use(middleware.RequestID())
|
|
|
|
s.Router.Use(middleware.Recover())
|
2024-09-08 10:04:06 +00:00
|
|
|
s.Router.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
|
|
|
AllowOrigins: s.config.HTTPServer.Cors.AllowOrigins,
|
|
|
|
}))
|
2024-09-04 14:02:52 +00:00
|
|
|
registerSwagger(s.Router)
|
2024-01-02 14:04:16 +00:00
|
|
|
|
|
|
|
// Routes
|
|
|
|
s.Router.GET("/health-check", s.healthCheck)
|
2024-01-14 15:53:37 +00:00
|
|
|
s.benefactorHandler.SetRoutes(s.Router)
|
2024-01-17 20:17:06 +00:00
|
|
|
s.benefactorKindBoxReqHandler.SetRoutes(s.Router)
|
2024-01-22 14:41:55 +00:00
|
|
|
s.benefactorAddressHandler.SetRoutes(s.Router)
|
2024-06-25 21:58:11 +00:00
|
|
|
s.benefactorKindBoxHandler.SetRoutes(s.Router)
|
2024-01-19 16:56:11 +00:00
|
|
|
s.adminHandler.SetRoutes(s.Router)
|
2024-09-06 11:30:29 +00:00
|
|
|
s.adminAgentHandler.SetRoutes(s.Router)
|
2024-01-22 08:14:57 +00:00
|
|
|
s.adminKindBoxReqHandler.SetRoutes(s.Router)
|
2024-07-01 17:13:28 +00:00
|
|
|
s.adminKindBoxHandler.SetRoutes(s.Router)
|
2024-07-13 12:33:07 +00:00
|
|
|
s.agentKindBoxHandler.SetRoutes(s.Router)
|
2024-09-15 07:36:09 +00:00
|
|
|
s.agentKindBoxReqHandler.SetRoutes(s.Router)
|
2023-12-20 15:39:25 +00:00
|
|
|
}
|
2024-05-14 13:07:09 +00:00
|
|
|
|
2024-09-04 14:02:52 +00:00
|
|
|
func registerSwagger(s *echo.Echo) {
|
2024-06-14 08:41:36 +00:00
|
|
|
// TODO: move this to a better place and make it more dynamic and configurable
|
2024-05-17 20:16:28 +00:00
|
|
|
docs.SwaggerInfo.Title = "NIKI API"
|
|
|
|
docs.SwaggerInfo.Description = "This is the API documentation for the NIKI project"
|
2024-05-14 13:07:09 +00:00
|
|
|
docs.SwaggerInfo.Version = "1.0.0"
|
2024-05-17 20:16:28 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
s.GET("/swagger/*any", echoSwagger.WrapHandler)
|
|
|
|
}
|