Skip to content

Commit aba1834

Browse files
committed
feat: Add fx-di to test base
1 parent 220b56d commit aba1834

File tree

12 files changed

+60
-72
lines changed

12 files changed

+60
-72
lines changed

internal/pkg/http/echo/server/echo-server.go

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,58 +15,45 @@ const (
1515
WriteTimeout = 15 * time.Second
1616
)
1717

18-
type EchoServer struct {
19-
Log logger.ILogger
20-
Cfg *config.EchoConfig
21-
Echo *echo.Echo
22-
}
23-
24-
func NewEchoServer(log logger.ILogger, cfg *config.EchoConfig) *EchoServer {
18+
func NewEchoServer() *echo.Echo {
2519
e := echo.New()
26-
return &EchoServer{Log: log, Cfg: cfg, Echo: e}
20+
return e
2721
}
2822

29-
func (s *EchoServer) RunHttpServer(ctx context.Context, configEcho ...func(echoServer *echo.Echo)) error {
30-
s.Echo.Server.ReadTimeout = ReadTimeout
31-
s.Echo.Server.WriteTimeout = WriteTimeout
32-
s.Echo.Server.MaxHeaderBytes = MaxHeaderBytes
33-
34-
if len(configEcho) > 0 {
35-
httpFunc := configEcho[0]
36-
if httpFunc != nil {
37-
httpFunc(s.Echo)
38-
}
39-
}
23+
func RunHttpServer(ctx context.Context, echo *echo.Echo, log logger.ILogger, cfg *config.EchoConfig) error {
24+
echo.Server.ReadTimeout = ReadTimeout
25+
echo.Server.WriteTimeout = WriteTimeout
26+
echo.Server.MaxHeaderBytes = MaxHeaderBytes
4027

4128
go func() {
4229
for {
4330
select {
4431
case <-ctx.Done():
45-
s.Log.Infof("shutting down Http PORT: {%s}", s.Cfg.Port)
46-
err := s.Echo.Shutdown(ctx)
32+
log.Infof("shutting down Http PORT: {%s}", cfg.Port)
33+
err := echo.Shutdown(ctx)
4734
if err != nil {
48-
s.Log.Errorf("(Shutdown) err: {%v}", err)
35+
log.Errorf("(Shutdown) err: {%v}", err)
4936
return
5037
}
51-
s.Log.Info("server exited properly")
38+
log.Info("server exited properly")
5239
return
5340
}
5441
}
5542
}()
5643

57-
err := s.Echo.Start(s.Cfg.Port)
44+
err := echo.Start(cfg.Port)
5845

5946
return err
6047
}
6148

62-
func (s *EchoServer) ApplyVersioningFromHeader() {
63-
s.Echo.Pre(apiVersion)
49+
func ApplyVersioningFromHeader(echo *echo.Echo) {
50+
echo.Pre(apiVersion)
6451
}
6552

66-
func (s *EchoServer) RegisterGroupFunc(groupName string, builder func(g *echo.Group)) *EchoServer {
67-
builder(s.Echo.Group(groupName))
53+
func RegisterGroupFunc(groupName string, echo *echo.Echo, builder func(g *echo.Group)) *echo.Echo {
54+
builder(echo.Group(groupName))
6855

69-
return s
56+
return echo
7057
}
7158

7259
// APIVersion Header Based Versioning

internal/pkg/oauth2/password_credentials.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/go-oauth2/oauth2/v4/store"
1212
"github.com/golang-jwt/jwt"
1313
"github.com/labstack/echo/v4"
14-
echo_server "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/server"
1514
"log"
1615
"net/http"
1716
"sync"
@@ -53,7 +52,7 @@ func clientStore(clients ...*models.Client) oauth2.ClientStore {
5352
}
5453

5554
// ref: https://github.com/go-oauth2/oauth2
56-
func RunOauthServer(e *echo_server.EchoServer) {
55+
func RunOauthServer(e *echo.Echo) {
5756

5857
manager.MapClientStorage(clientStore(clients...))
5958

@@ -83,8 +82,8 @@ func RunOauthServer(e *echo_server.EchoServer) {
8382
log.Println("Response Error:", re.Error.Error())
8483
})
8584

86-
e.Echo.GET("connect/token", token)
87-
e.Echo.GET("validate-token", validateBearerToken)
85+
e.GET("connect/token", token)
86+
e.GET("validate-token", validateBearerToken)
8887
}
8988

9089
func validateBearerToken(c echo.Context) error {

internal/services/identity-service/identity/configurations/infrastructures_configurator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"github.com/go-playground/validator"
66
"github.com/go-resty/resty/v2"
7+
"github.com/labstack/echo/v4"
78
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/grpc"
8-
echo_server "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/server"
99
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/logger"
1010
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/rabbitmq"
1111
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/config"
@@ -16,14 +16,14 @@ import (
1616
"gorm.io/gorm"
1717
)
1818

19-
func InitialInfrastructures(echoServer *echo_server.EchoServer, log logger.ILogger, ctx context.Context, grpcServer *grpc.GrpcServer,
19+
func InitialInfrastructures(echo *echo.Echo, log logger.ILogger, ctx context.Context, grpcServer *grpc.GrpcServer,
2020
userRepository contracts2.UserRepository, config *config.Config, rabbitmqPublisher *rabbitmq.Publisher, conn *amqp.Connection,
2121
gorm *gorm.DB, tracer trace.Tracer, httpClient *resty.Client) *contracts.InfrastructureConfiguration {
2222

2323
infar := &contracts.InfrastructureConfiguration{
2424
Log: log,
2525
Context: ctx,
26-
Echo: echoServer.Echo,
26+
Echo: echo,
2727
GrpcServer: grpcServer,
2828
UserRepository: userRepository,
2929
Cfg: config,

internal/services/identity-service/identity/configurations/middleware_configurations.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,29 @@ import (
44
"github.com/labstack/echo/v4"
55
"github.com/labstack/echo/v4/middleware"
66
echo_middleware "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/middleware"
7-
echo_server "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/server"
87
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/open-telemetry"
98
otel_middleware "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/open-telemetry/middleware"
109
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/identity/constants"
1110
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/identity/middlewares"
1211
"strings"
1312
)
1413

15-
func ConfigMiddlewares(e *echo_server.EchoServer, jaegerCfg *open_telemetry.JaegerConfig) {
14+
func ConfigMiddlewares(e *echo.Echo, jaegerCfg *open_telemetry.JaegerConfig) {
1615

17-
e.Echo.HideBanner = false
16+
e.HideBanner = false
1817

19-
e.Echo.Use(middleware.Logger())
20-
e.Echo.HTTPErrorHandler = middlewares.ProblemDetailsHandler
21-
e.Echo.Use(otel_middleware.EchoTracerMiddleware(jaegerCfg.ServiceName))
18+
e.Use(middleware.Logger())
19+
e.HTTPErrorHandler = middlewares.ProblemDetailsHandler
20+
e.Use(otel_middleware.EchoTracerMiddleware(jaegerCfg.ServiceName))
2221

23-
e.Echo.Use(echo_middleware.CorrelationIdMiddleware)
24-
e.Echo.Use(middleware.RequestID())
25-
e.Echo.Use(middleware.GzipWithConfig(middleware.GzipConfig{
22+
e.Use(echo_middleware.CorrelationIdMiddleware)
23+
e.Use(middleware.RequestID())
24+
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
2625
Level: constants.GzipLevel,
2726
Skipper: func(c echo.Context) bool {
2827
return strings.Contains(c.Request().URL.Path, "swagger")
2928
},
3029
}))
3130

32-
e.Echo.Use(middleware.BodyLimit(constants.BodyLimit))
31+
e.Use(middleware.BodyLimit(constants.BodyLimit))
3332
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package configurations
22

33
import (
4-
echo_server "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/server"
4+
"github.com/labstack/echo/v4"
55
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/docs"
66
echoSwagger "github.com/swaggo/echo-swagger"
77
)
88

9-
func ConfigSwagger(e *echo_server.EchoServer) {
9+
func ConfigSwagger(e *echo.Echo) {
1010

1111
docs.SwaggerInfo.Version = "1.0"
1212
docs.SwaggerInfo.Title = "Identities Service Api"
1313
docs.SwaggerInfo.Description = "Identities Service Api"
14-
e.Echo.GET("/swagger/*", echoSwagger.WrapHandler)
14+
e.GET("/swagger/*", echoSwagger.WrapHandler)
1515
}

internal/services/identity-service/identity/server/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ package server
22

33
import (
44
"context"
5+
"github.com/labstack/echo/v4"
56
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/grpc"
7+
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/config"
68
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/server"
79
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/logger"
810
"github.com/pkg/errors"
911
"go.uber.org/fx"
1012
"net/http"
1113
)
1214

13-
func RunServers(lc fx.Lifecycle, log logger.ILogger, echoServer *server.EchoServer, grpcServer *grpc.GrpcServer, ctx context.Context) error {
15+
func RunServers(lc fx.Lifecycle, log logger.ILogger, echo *echo.Echo, grpcServer *grpc.GrpcServer, ctx context.Context, echoConfig *config.EchoConfig) error {
1416

1517
lc.Append(fx.Hook{
1618
OnStart: func(_ context.Context) error {
1719
go func() {
18-
if err := echoServer.RunHttpServer(ctx); !errors.Is(err, http.ErrServerClosed) {
20+
if err := server.RunHttpServer(ctx, echo, log, echoConfig); !errors.Is(err, http.ErrServerClosed) {
1921
log.Fatalf("error running http server: %v", err)
2022
}
2123
}()

internal/services/product-service/product/configurations/infrastructures_configurator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"github.com/go-playground/validator"
66
"github.com/go-resty/resty/v2"
7+
"github.com/labstack/echo/v4"
78
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/grpc"
8-
echo_server "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/server"
99
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/logger"
1010
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/rabbitmq"
1111
"github.com/meysamhadeli/shop-golang-microservices/internal/services/product-service/config"
@@ -16,14 +16,14 @@ import (
1616
"gorm.io/gorm"
1717
)
1818

19-
func InitialInfrastructures(echoServer *echo_server.EchoServer, log logger.ILogger, ctx context.Context, grpcClient grpc.GrpcClient,
19+
func InitialInfrastructures(echo *echo.Echo, log logger.ILogger, ctx context.Context, grpcClient grpc.GrpcClient,
2020
productRepository data.ProductRepository, config *config.Config, rabbitmqPublisher *rabbitmq.Publisher, conn *amqp.Connection,
2121
gorm *gorm.DB, tracer trace.Tracer, httpClient *resty.Client) *contracts.InfrastructureConfiguration {
2222

2323
infar := &contracts.InfrastructureConfiguration{
2424
Log: log,
2525
Context: ctx,
26-
Echo: echoServer.Echo,
26+
Echo: echo,
2727
GrpcClient: grpcClient,
2828
ProductRepository: productRepository,
2929
Cfg: config,

internal/services/product-service/product/configurations/middleware_configurations.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,30 @@ import (
44
"github.com/labstack/echo/v4"
55
"github.com/labstack/echo/v4/middleware"
66
echo_middleware "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/middleware"
7-
echo_server "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/server"
87
open_telemetry "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/open-telemetry"
98
otel_middleware "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/open-telemetry/middleware"
109
"github.com/meysamhadeli/shop-golang-microservices/internal/services/product-service/product/constants"
1110
"github.com/meysamhadeli/shop-golang-microservices/internal/services/product-service/product/middlewares"
1211
"strings"
1312
)
1413

15-
func ConfigMiddlewares(e *echo_server.EchoServer, jaegerCfg *open_telemetry.JaegerConfig) {
14+
func ConfigMiddlewares(e *echo.Echo, jaegerCfg *open_telemetry.JaegerConfig) {
1615

17-
e.Echo.HideBanner = false
16+
e.HideBanner = false
1817

19-
e.Echo.Use(middleware.Logger())
18+
e.Use(middleware.Logger())
2019

21-
e.Echo.HTTPErrorHandler = middlewares.ProblemDetailsHandler
22-
e.Echo.Use(otel_middleware.EchoTracerMiddleware(jaegerCfg.ServiceName))
20+
e.HTTPErrorHandler = middlewares.ProblemDetailsHandler
21+
e.Use(otel_middleware.EchoTracerMiddleware(jaegerCfg.ServiceName))
2322

24-
e.Echo.Use(echo_middleware.CorrelationIdMiddleware)
25-
e.Echo.Use(middleware.RequestID())
26-
e.Echo.Use(middleware.GzipWithConfig(middleware.GzipConfig{
23+
e.Use(echo_middleware.CorrelationIdMiddleware)
24+
e.Use(middleware.RequestID())
25+
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
2726
Level: constants.GzipLevel,
2827
Skipper: func(c echo.Context) bool {
2928
return strings.Contains(c.Request().URL.Path, "swagger")
3029
},
3130
}))
3231

33-
e.Echo.Use(middleware.BodyLimit(constants.BodyLimit))
32+
e.Use(middleware.BodyLimit(constants.BodyLimit))
3433
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package configurations
22

33
import (
4-
echo_server "github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/server"
4+
"github.com/labstack/echo/v4"
55
"github.com/meysamhadeli/shop-golang-microservices/internal/services/product-service/docs"
66
echoSwagger "github.com/swaggo/echo-swagger"
77
)
88

9-
func ConfigSwagger(e *echo_server.EchoServer) {
9+
func ConfigSwagger(e *echo.Echo) {
1010

1111
docs.SwaggerInfo.Version = "1.0"
1212
docs.SwaggerInfo.Title = "Products Service Api"
1313
docs.SwaggerInfo.Description = "Products Service Api"
14-
e.Echo.GET("/swagger/*", echoSwagger.WrapHandler)
14+
e.GET("/swagger/*", echoSwagger.WrapHandler)
1515
}

internal/services/product-service/server/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ package server
22

33
import (
44
"context"
5+
"github.com/labstack/echo/v4"
6+
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/config"
57
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/http/echo/server"
68
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/logger"
79
"github.com/pkg/errors"
810
"go.uber.org/fx"
911
"net/http"
1012
)
1113

12-
func RunServers(lc fx.Lifecycle, log logger.ILogger, echoServer *server.EchoServer, ctx context.Context) error {
14+
func RunServers(lc fx.Lifecycle, log logger.ILogger, echo *echo.Echo, ctx context.Context, echoConfig *config.EchoConfig) error {
1315

1416
lc.Append(fx.Hook{
1517
OnStart: func(_ context.Context) error {
1618
go func() {
17-
if err := echoServer.RunHttpServer(ctx); !errors.Is(err, http.ErrServerClosed) {
19+
if err := server.RunHttpServer(ctx, echo, log, echoConfig); !errors.Is(err, http.ErrServerClosed) {
1820
log.Fatalf("error running http server: %v", err)
1921
}
2022
}()

0 commit comments

Comments
 (0)