Skip to content

Commit

Permalink
♻️ refactor: updated codebase #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Nov 17, 2023
1 parent a66122c commit 7582b39
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 22 deletions.
22 changes: 11 additions & 11 deletions internal/core/handlers.go
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 20 additions & 0 deletions internal/core/init.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 7 additions & 1 deletion internal/core/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
26 changes: 26 additions & 0 deletions internal/handlers/common_handler.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 1 addition & 9 deletions internal/middlewares/interceptor_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
1 change: 0 additions & 1 deletion internal/models/.gitkeep

This file was deleted.

10 changes: 10 additions & 0 deletions internal/models/common_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type Common struct {
}

type CommonRequest struct {
}

type CommonResponse struct {
}
26 changes: 26 additions & 0 deletions internal/repository/common_repository.go
Original file line number Diff line number Diff line change
@@ -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
}
24 changes: 24 additions & 0 deletions internal/service/common_service.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 7582b39

Please sign in to comment.