Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/layering #4

Merged
merged 18 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.22

- name: Build
run: cd src && go mod download && go build -v cmd/main.go
17 changes: 9 additions & 8 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
- webapi_network
depends_on:
- elasticsearch

####################### ELK #######################
elasticsearch:
build:
Expand Down Expand Up @@ -77,7 +77,7 @@ services:
#- ../prod/logs:/var/log/filebeat/service:ro
- logs:/var/log/filebeat
- logs:/app:ro

environment:
FILEBEAT_INTERNAL_PASSWORD: ${FILEBEAT_INTERNAL_PASSWORD:-}
BEATS_SYSTEM_PASSWORD: ${FILEBEAT_INTERNAL_PASSWORD:-}
Expand Down Expand Up @@ -118,15 +118,15 @@ services:
restart: unless-stopped
depends_on:
- postgres

####################### REDIS #######################
redis:
image: redis:latest
container_name: redis_container
command: ["redis-server", "/etc/redis/redis.conf"]
command: [ "redis-server", "/etc/redis/redis.conf" ]
volumes:
- ./redis/redis.conf:/etc/redis/redis.conf
- redis:/etc/redis
- redis:/etc/redis
ports:
- "6379:6379"
networks:
Expand Down Expand Up @@ -182,7 +182,7 @@ services:
command:
- "--config.file=/etc/alertmanager/config.yml"
- "--storage.path=/alertmanager"

grafana:
image: grafana/grafana
user: "472"
Expand All @@ -196,7 +196,7 @@ services:
env_file:
- ./grafana/config.monitoring
networks:
- webapi_network
- webapi_network
restart: always
car-api1:
build: ../src/
Expand Down Expand Up @@ -241,6 +241,7 @@ volumes:
prometheus_data:
grafana_data:


networks:
webapi_network:
driver: bridge
driver: bridge
2 changes: 1 addition & 1 deletion src/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19-buster as builder
FROM golang:1.19-buster AS builder

WORKDIR /app

Expand Down
98 changes: 49 additions & 49 deletions src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"github.com/naeemaei/golang-clean-web-api/api/middlewares"
"github.com/naeemaei/golang-clean-web-api/api/routers"
validation "github.com/naeemaei/golang-clean-web-api/api/validations"
"github.com/naeemaei/golang-clean-web-api/api/middleware"
"github.com/naeemaei/golang-clean-web-api/api/router"
validation "github.com/naeemaei/golang-clean-web-api/api/validation"
"github.com/naeemaei/golang-clean-web-api/config"
"github.com/naeemaei/golang-clean-web-api/docs"
"github.com/naeemaei/golang-clean-web-api/pkg/logging"
Expand All @@ -27,10 +27,10 @@ func InitServer(cfg *config.Config) {
RegisterValidators()
RegisterPrometheus()

r.Use(middlewares.DefaultStructuredLogger(cfg))
r.Use(middlewares.Cors(cfg))
r.Use(middlewares.Prometheus())
r.Use(gin.Logger(), gin.CustomRecovery(middlewares.ErrorHandler) /*middlewares.TestMiddleware()*/, middlewares.LimitByRequest())
r.Use(middleware.DefaultStructuredLogger(cfg))
r.Use(middleware.Cors(cfg))
r.Use(middleware.Prometheus())
r.Use(gin.Logger(), gin.CustomRecovery(middleware.ErrorHandler) /*middleware.TestMiddleware()*/, middleware.LimitByRequest())

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

// User
users := v1.Group("/users")

// Base
countries := v1.Group("/countries", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
cities := v1.Group("/cities", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
files := v1.Group("/files", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
companies := v1.Group("/companies", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
colors := v1.Group("/colors", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
years := v1.Group("/years", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
countries := v1.Group("/countries", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
cities := v1.Group("/cities", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
files := v1.Group("/files", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
companies := v1.Group("/companies", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
colors := v1.Group("/colors", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
years := v1.Group("/years", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))

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

// Car
carTypes := v1.Group("/car-types", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
gearboxes := v1.Group("/gearboxes", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
carModels := v1.Group("/car-models", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
carModelColors := v1.Group("/car-model-colors", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
carModelYears := v1.Group("/car-model-years", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
carModelPriceHistories := v1.Group("/car-model-price-histories", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
carModelImages := v1.Group("/car-model-images", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
carModelProperties := v1.Group("/car-model-properties", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin"}))
carModelComments := v1.Group("/car-model-comments", middlewares.Authentication(cfg), middlewares.Authorization([]string{"admin", "default"}))
carTypes := v1.Group("/car-types", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
gearboxes := v1.Group("/gearboxes", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
carModels := v1.Group("/car-models", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
carModelColors := v1.Group("/car-model-colors", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
carModelYears := v1.Group("/car-model-years", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
carModelPriceHistories := v1.Group("/car-model-price-histories", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
carModelImages := v1.Group("/car-model-images", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
carModelProperties := v1.Group("/car-model-properties", middleware.Authentication(cfg), middleware.Authorization([]string{"admin"}))
carModelComments := v1.Group("/car-model-comments", middleware.Authentication(cfg), middleware.Authorization([]string{"admin", "default"}))

// Test
routers.Health(health)
routers.TestRouter(testRouter)
router.Health(health)
router.TestRouter(testRouter)

// User
routers.User(users, cfg)
router.User(users, cfg)

// Base
routers.Country(countries, cfg)
routers.City(cities, cfg)
routers.File(files, cfg)
routers.Company(companies, cfg)
routers.Color(colors, cfg)
routers.Year(years, cfg)
router.Country(countries, cfg)
router.City(cities, cfg)
router.File(files, cfg)
router.Company(companies, cfg)
router.Color(colors, cfg)
router.Year(years, cfg)

// Property
routers.Property(properties, cfg)
routers.PropertyCategory(propertyCategories, cfg)
router.Property(properties, cfg)
router.PropertyCategory(propertyCategories, cfg)

// Car
routers.CarType(carTypes, cfg)
routers.Gearbox(gearboxes, cfg)
routers.CarModel(carModels, cfg)
routers.CarModelColor(carModelColors, cfg)
routers.CarModelYear(carModelYears, cfg)
routers.CarModelPriceHistory(carModelPriceHistories, cfg)
routers.CarModelImage(carModelImages, cfg)
routers.CarModelProperty(carModelProperties, cfg)
routers.CarModelComment(carModelComments, cfg)
router.CarType(carTypes, cfg)
router.Gearbox(gearboxes, cfg)
router.CarModel(carModels, cfg)
router.CarModelColor(carModelColors, cfg)
router.CarModelYear(carModelYears, cfg)
router.CarModelPriceHistory(carModelPriceHistories, cfg)
router.CarModelImage(carModelImages, cfg)
router.CarModelProperty(carModelProperties, cfg)
router.CarModelComment(carModelComments, cfg)

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

Expand All @@ -115,7 +115,7 @@ func RegisterRoutes(r *gin.Engine, cfg *config.Config) {
v2 := api.Group("/v2")
{
health := v2.Group("/health")
routers.Health(health)
router.Health(health)
}
}

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

func RegisterPrometheus(){
func RegisterPrometheus() {
err := prometheus.Register(metrics.DbCall)
if err != nil {
logger.Error(logging.Prometheus, logging.Startup, err.Error(), nil)
}

err = prometheus.Register(metrics.HttpDuration)
if err != nil {
logger.Error(logging.Prometheus, logging.Startup, err.Error(), nil)
}
}
}
120 changes: 0 additions & 120 deletions src/api/dto/base.go

This file was deleted.

Loading
Loading