diff --git a/internal/core/handlers.go b/internal/core/handlers.go index 6c63b40..203bc09 100644 --- a/internal/core/handlers.go +++ b/internal/core/handlers.go @@ -1,24 +1,24 @@ package core import ( + "github.com/sivaosorg/gocell/internal/handlers" "github.com/sivaosorg/gocell/internal/middlewares" + "github.com/sivaosorg/gocell/internal/repository" + "github.com/sivaosorg/gocell/internal/service" syncconf "github.com/sivaosorg/gocell/internal/syncConf" ) type coreHandler struct { - middlewares *middlewares.MiddlewareManager -} - -func NewCoreHandler() *coreHandler { - return &coreHandler{} -} - -func (c *coreHandler) setMiddlewares(value *middlewares.MiddlewareManager) *coreHandler { - c.middlewares = value - return c + middlewares *middlewares.MiddlewareManager + commonHandler *handlers.CommonHandler } func (c *CoreCommand) handler() { + commonRepository := repository.NewCommonRepository(c.psql, c.psqlStatus) + commonSvc := service.NewCommonService(commonRepository) + commonHandler := handlers.NewCommonHandler(commonSvc) + c.handlers = NewCoreHandler(). - setMiddlewares(middlewares.NewMiddlewareManager(syncconf.Conf)) + setMiddlewares(middlewares.NewMiddlewareManager(syncconf.Conf)). + setCommonHandler(commonHandler) } diff --git a/internal/core/init.go b/internal/core/init.go new file mode 100644 index 0000000..54d194e --- /dev/null +++ b/internal/core/init.go @@ -0,0 +1,20 @@ +package core + +import ( + "github.com/sivaosorg/gocell/internal/handlers" + "github.com/sivaosorg/gocell/internal/middlewares" +) + +func NewCoreHandler() *coreHandler { + return &coreHandler{} +} + +func (c *coreHandler) setMiddlewares(value *middlewares.MiddlewareManager) *coreHandler { + c.middlewares = value + return c +} + +func (c *coreHandler) setCommonHandler(value *handlers.CommonHandler) *coreHandler { + c.commonHandler = value + return c +} diff --git a/internal/core/routes.go b/internal/core/routes.go index 1b3321b..def5a04 100644 --- a/internal/core/routes.go +++ b/internal/core/routes.go @@ -7,9 +7,15 @@ import ( ) func (c *CoreCommand) routes(core *gin.Engine) { - core.GET("/api/v1/swagger/index.html", ginSwagger.WrapHandler( swaggerFiles.Handler, ginSwagger.DefaultModelsExpandDepth(-1), )) + v1 := core.Group("/api/v1") + { + v1.GET("/common/psql-status", + c.handlers.middlewares.RequestMiddleWare(), + c.handlers.middlewares.NoopMiddleWare(), + c.handlers.commonHandler.OnPsqlStatus) + } } diff --git a/internal/handlers/common_handler.go b/internal/handlers/common_handler.go new file mode 100644 index 0000000..5d5be17 --- /dev/null +++ b/internal/handlers/common_handler.go @@ -0,0 +1,26 @@ +package handlers + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/sivaosorg/gocell/internal/service" + "github.com/sivaosorg/govm/entity" +) + +type CommonHandler struct { + commonSvc service.CommonService +} + +func NewCommonHandler(commonSvc service.CommonService) *CommonHandler { + h := &CommonHandler{ + commonSvc: commonSvc, + } + return h +} + +func (h *CommonHandler) OnPsqlStatus(ctx *gin.Context) { + response := entity.NewResponseEntity().SetStatusCode(http.StatusOK).SetData(h.commonSvc.GetPsqlStatus()) + ctx.JSON(response.StatusCode, response) + return +} diff --git a/internal/middlewares/interceptor_middleware.go b/internal/middlewares/interceptor_middleware.go index fe53779..7eb64b7 100644 --- a/internal/middlewares/interceptor_middleware.go +++ b/internal/middlewares/interceptor_middleware.go @@ -40,15 +40,7 @@ func (m *MiddlewareManager) RequestMiddleWare() gin.HandlerFunc { bodyBuffer: bytes.NewBufferString(""), } c.Writer = wrappedWriter - // Make the request handling asynchronous - go func() { - defer func() { - if r := recover(); r != nil { - logger.Debugf("Asynchronous request recover handling: %v", r) - } - }() - c.Next() - }() + c.Next() m.async(c, wrappedWriter) } } diff --git a/internal/models/.gitkeep b/internal/models/.gitkeep deleted file mode 100644 index 98140d5..0000000 --- a/internal/models/.gitkeep +++ /dev/null @@ -1 +0,0 @@ -# define models here \ No newline at end of file diff --git a/internal/models/common_model.go b/internal/models/common_model.go new file mode 100644 index 0000000..1c55e0d --- /dev/null +++ b/internal/models/common_model.go @@ -0,0 +1,10 @@ +package models + +type Common struct { +} + +type CommonRequest struct { +} + +type CommonResponse struct { +} diff --git a/internal/repository/common_repository.go b/internal/repository/common_repository.go new file mode 100644 index 0000000..2794ac5 --- /dev/null +++ b/internal/repository/common_repository.go @@ -0,0 +1,26 @@ +package repository + +import ( + "github.com/sivaosorg/govm/dbx" + "github.com/sivaosorg/postgresconn/postgresconn" +) + +type CommonRepository interface { + GetPsqlStatus() dbx.Dbx +} + +type commonRepositoryImpl struct { + psql *postgresconn.Postgres + psqlStatus dbx.Dbx +} + +func NewCommonRepository(psql *postgresconn.Postgres, psqlStatus dbx.Dbx) CommonRepository { + return &commonRepositoryImpl{ + psql: psql, + psqlStatus: psqlStatus, + } +} + +func (repo *commonRepositoryImpl) GetPsqlStatus() dbx.Dbx { + return repo.psqlStatus +} diff --git a/internal/service/common_service.go b/internal/service/common_service.go new file mode 100644 index 0000000..52e653b --- /dev/null +++ b/internal/service/common_service.go @@ -0,0 +1,24 @@ +package service + +import ( + "github.com/sivaosorg/gocell/internal/repository" + "github.com/sivaosorg/govm/dbx" +) + +type CommonService interface { + GetPsqlStatus() dbx.Dbx +} + +type commonServiceImpl struct { + commonRepository repository.CommonRepository +} + +func NewCommonService(commonRepository repository.CommonRepository) CommonService { + return &commonServiceImpl{ + commonRepository: commonRepository, + } +} + +func (s *commonServiceImpl) GetPsqlStatus() dbx.Dbx { + return s.commonRepository.GetPsqlStatus() +}