Skip to content

Commit 9c548d1

Browse files
authored
Refactor/layering (#4)
* Upgrade to go 1.22 * Remove services * Update dockerfile and docker compose file * Remove data layer Create domain layer Move data model to domain layer and create repository contracts in domain layer * Move redis and repository implementation to infra * Change constant directory location * Change validation directory location * Change middleware directory location * Add new items to log category and error constants * Add some mapper functions to data transfer objects * Update validation reference in base response * Change mapper input from * Create dependency manager to create repositories based its contracts * Write usecases * Change handlers directory location * Change routers directory location * Update main file references * Update docs
1 parent 347a0d5 commit 9c548d1

File tree

123 files changed

+5301
-3832
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+5301
-3832
lines changed

docker/docker-compose.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ services:
2121
- webapi_network
2222
depends_on:
2323
- elasticsearch
24-
24+
2525
####################### ELK #######################
2626
elasticsearch:
2727
build:
@@ -77,7 +77,7 @@ services:
7777
#- ../prod/logs:/var/log/filebeat/service:ro
7878
- logs:/var/log/filebeat
7979
- logs:/app:ro
80-
80+
8181
environment:
8282
FILEBEAT_INTERNAL_PASSWORD: ${FILEBEAT_INTERNAL_PASSWORD:-}
8383
BEATS_SYSTEM_PASSWORD: ${FILEBEAT_INTERNAL_PASSWORD:-}
@@ -118,15 +118,15 @@ services:
118118
restart: unless-stopped
119119
depends_on:
120120
- postgres
121-
121+
122122
####################### REDIS #######################
123123
redis:
124124
image: redis:latest
125125
container_name: redis_container
126-
command: ["redis-server", "/etc/redis/redis.conf"]
126+
command: [ "redis-server", "/etc/redis/redis.conf" ]
127127
volumes:
128128
- ./redis/redis.conf:/etc/redis/redis.conf
129-
- redis:/etc/redis
129+
- redis:/etc/redis
130130
ports:
131131
- "6379:6379"
132132
networks:
@@ -182,7 +182,7 @@ services:
182182
command:
183183
- "--config.file=/etc/alertmanager/config.yml"
184184
- "--storage.path=/alertmanager"
185-
185+
186186
grafana:
187187
image: grafana/grafana
188188
user: "472"
@@ -196,7 +196,7 @@ services:
196196
env_file:
197197
- ./grafana/config.monitoring
198198
networks:
199-
- webapi_network
199+
- webapi_network
200200
restart: always
201201
car-api1:
202202
build: ../src/
@@ -241,6 +241,7 @@ volumes:
241241
prometheus_data:
242242
grafana_data:
243243

244+
244245
networks:
245246
webapi_network:
246-
driver: bridge
247+
driver: bridge

src/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.19-buster as builder
1+
FROM golang:1.19-buster AS builder
22

33
WORKDIR /app
44

src/api/api.go

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"github.com/gin-gonic/gin"
77
"github.com/gin-gonic/gin/binding"
88
"github.com/go-playground/validator/v10"
9-
"github.com/naeemaei/golang-clean-web-api/api/middlewares"
10-
"github.com/naeemaei/golang-clean-web-api/api/routers"
11-
validation "github.com/naeemaei/golang-clean-web-api/api/validations"
9+
"github.com/naeemaei/golang-clean-web-api/api/middleware"
10+
"github.com/naeemaei/golang-clean-web-api/api/router"
11+
validation "github.com/naeemaei/golang-clean-web-api/api/validation"
1212
"github.com/naeemaei/golang-clean-web-api/config"
1313
"github.com/naeemaei/golang-clean-web-api/docs"
1414
"github.com/naeemaei/golang-clean-web-api/pkg/logging"
@@ -27,10 +27,10 @@ func InitServer(cfg *config.Config) {
2727
RegisterValidators()
2828
RegisterPrometheus()
2929

30-
r.Use(middlewares.DefaultStructuredLogger(cfg))
31-
r.Use(middlewares.Cors(cfg))
32-
r.Use(middlewares.Prometheus())
33-
r.Use(gin.Logger(), gin.CustomRecovery(middlewares.ErrorHandler) /*middlewares.TestMiddleware()*/, middlewares.LimitByRequest())
30+
r.Use(middleware.DefaultStructuredLogger(cfg))
31+
r.Use(middleware.Cors(cfg))
32+
r.Use(middleware.Prometheus())
33+
r.Use(gin.Logger(), gin.CustomRecovery(middleware.ErrorHandler) /*middleware.TestMiddleware()*/, middleware.LimitByRequest())
3434

3535
RegisterRoutes(r, cfg)
3636
RegisterSwagger(r, cfg)
@@ -49,63 +49,63 @@ func RegisterRoutes(r *gin.Engine, cfg *config.Config) {
4949
{
5050
// Test
5151
health := v1.Group("/health")
52-
testRouter := v1.Group("/test" /*middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"})*/)
52+
testRouter := v1.Group("/test" /*middleware.Authentication(cfg), middleware.Authorization([]string{"admin"})*/)
5353

5454
// User
5555
users := v1.Group("/users")
5656

5757
// Base
58-
countries := v1.Group("/countries", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
59-
cities := v1.Group("/cities", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
60-
files := v1.Group("/files", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
61-
companies := v1.Group("/companies", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
62-
colors := v1.Group("/colors", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
63-
years := v1.Group("/years", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
58+
countries := v1.Group("/countries", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
59+
cities := v1.Group("/cities", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
60+
files := v1.Group("/files", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
61+
companies := v1.Group("/companies", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
62+
colors := v1.Group("/colors", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
63+
years := v1.Group("/years", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
6464

6565
// Property
66-
properties := v1.Group("/properties", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
67-
propertyCategories := v1.Group("/property-categories", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
66+
properties := v1.Group("/properties", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
67+
propertyCategories := v1.Group("/property-categories", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
6868

6969
// Car
70-
carTypes := v1.Group("/car-types", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
71-
gearboxes := v1.Group("/gearboxes", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
72-
carModels := v1.Group("/car-models", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
73-
carModelColors := v1.Group("/car-model-colors", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
74-
carModelYears := v1.Group("/car-model-years", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
75-
carModelPriceHistories := v1.Group("/car-model-price-histories", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
76-
carModelImages := v1.Group("/car-model-images", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
77-
carModelProperties := v1.Group("/car-model-properties", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
78-
carModelComments := v1.Group("/car-model-comments", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin", "default"}))
70+
carTypes := v1.Group("/car-types", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
71+
gearboxes := v1.Group("/gearboxes", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
72+
carModels := v1.Group("/car-models", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
73+
carModelColors := v1.Group("/car-model-colors", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
74+
carModelYears := v1.Group("/car-model-years", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
75+
carModelPriceHistories := v1.Group("/car-model-price-histories", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
76+
carModelImages := v1.Group("/car-model-images", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
77+
carModelProperties := v1.Group("/car-model-properties", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
78+
carModelComments := v1.Group("/car-model-comments", middleware.Authentication(cfg), middleware.Authorization([]string{"admin", "default"}))
7979

8080
// Test
81-
routers.Health(health)
82-
routers.TestRouter(testRouter)
81+
router.Health(health)
82+
router.TestRouter(testRouter)
8383

8484
// User
85-
routers.User(users, cfg)
85+
router.User(users, cfg)
8686

8787
// Base
88-
routers.Country(countries, cfg)
89-
routers.City(cities, cfg)
90-
routers.File(files, cfg)
91-
routers.Company(companies, cfg)
92-
routers.Color(colors, cfg)
93-
routers.Year(years, cfg)
88+
router.Country(countries, cfg)
89+
router.City(cities, cfg)
90+
router.File(files, cfg)
91+
router.Company(companies, cfg)
92+
router.Color(colors, cfg)
93+
router.Year(years, cfg)
9494

9595
// Property
96-
routers.Property(properties, cfg)
97-
routers.PropertyCategory(propertyCategories, cfg)
96+
router.Property(properties, cfg)
97+
router.PropertyCategory(propertyCategories, cfg)
9898

9999
// Car
100-
routers.CarType(carTypes, cfg)
101-
routers.Gearbox(gearboxes, cfg)
102-
routers.CarModel(carModels, cfg)
103-
routers.CarModelColor(carModelColors, cfg)
104-
routers.CarModelYear(carModelYears, cfg)
105-
routers.CarModelPriceHistory(carModelPriceHistories, cfg)
106-
routers.CarModelImage(carModelImages, cfg)
107-
routers.CarModelProperty(carModelProperties, cfg)
108-
routers.CarModelComment(carModelComments, cfg)
100+
router.CarType(carTypes, cfg)
101+
router.Gearbox(gearboxes, cfg)
102+
router.CarModel(carModels, cfg)
103+
router.CarModelColor(carModelColors, cfg)
104+
router.CarModelYear(carModelYears, cfg)
105+
router.CarModelPriceHistory(carModelPriceHistories, cfg)
106+
router.CarModelImage(carModelImages, cfg)
107+
router.CarModelProperty(carModelProperties, cfg)
108+
router.CarModelComment(carModelComments, cfg)
109109

110110
r.Static("/static", "./uploads")
111111

@@ -115,7 +115,7 @@ func RegisterRoutes(r *gin.Engine, cfg *config.Config) {
115115
v2 := api.Group("/v2")
116116
{
117117
health := v2.Group("/health")
118-
routers.Health(health)
118+
router.Health(health)
119119
}
120120
}
121121

@@ -144,14 +144,14 @@ func RegisterSwagger(r *gin.Engine, cfg *config.Config) {
144144
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
145145
}
146146

147-
func RegisterPrometheus(){
147+
func RegisterPrometheus() {
148148
err := prometheus.Register(metrics.DbCall)
149149
if err != nil {
150150
logger.Error(logging.Prometheus, logging.Startup, err.Error(), nil)
151151
}
152-
152+
153153
err = prometheus.Register(metrics.HttpDuration)
154154
if err != nil {
155155
logger.Error(logging.Prometheus, logging.Startup, err.Error(), nil)
156156
}
157-
}
157+
}

src/api/dto/base.go

Lines changed: 0 additions & 120 deletions
This file was deleted.

0 commit comments

Comments
 (0)