Refactor Dockerfile and CI Configuration
- Updated the CI configuration in .drone.yml to use the new Dockerfile located at ./cmd/web/Dockerfile. - Removed the old Dockerfile and Dockerfile-drone as they are no longer needed. - Refactored the cmd/web/Dockerfile to streamline the build process and improve organization. - Introduced a new bootstrap package to handle application initialization, including configuration, logging, MongoDB, Redis, and authorization service setup. - Added a health check endpoint for monitoring the server status, enhancing the API's operational capabilities.
This commit is contained in:
+12
-12
@@ -27,21 +27,21 @@ steps:
|
|||||||
username: cicd
|
username: cicd
|
||||||
password:
|
password:
|
||||||
from_secret: REGISTRY_PASSWORD
|
from_secret: REGISTRY_PASSWORD
|
||||||
dockerfile: Dockerfile-drone
|
dockerfile: ./cmd/web/Dockerfile
|
||||||
repo: artifactory.ravanertebat.ir/tm/web
|
repo: artifactory.ravanertebat.ir/tm/web
|
||||||
registry: artifactory.ravanertebat.ir
|
registry: artifactory.ravanertebat.ir
|
||||||
|
|
||||||
- name: buid scraper
|
# - name: buid scraper
|
||||||
image: plugins/docker
|
# image: plugins/docker
|
||||||
settings:
|
# settings:
|
||||||
build_args:
|
# build_args:
|
||||||
- BUILD_APP=scraper
|
# - BUILD_APP=scraper
|
||||||
username: cicd
|
# username: cicd
|
||||||
password:
|
# password:
|
||||||
from_secret: REGISTRY_PASSWORD
|
# from_secret: REGISTRY_PASSWORD
|
||||||
dockerfile: Dockerfile-drone
|
# dockerfile: Dockerfile-drone
|
||||||
repo: artifactory.ravanertebat.ir/tm/scraper
|
# repo: artifactory.ravanertebat.ir/tm/scraper
|
||||||
registry: artifactory.ravanertebat.ir
|
# registry: artifactory.ravanertebat.ir
|
||||||
|
|
||||||
node:
|
node:
|
||||||
tag: aecde-docker-runner
|
tag: aecde-docker-runner
|
||||||
|
|||||||
-60
@@ -1,60 +0,0 @@
|
|||||||
# Build stage
|
|
||||||
FROM golang:1.21-alpine AS builder
|
|
||||||
|
|
||||||
# Install git and ca-certificates (needed for go mod download)
|
|
||||||
RUN apk add --no-cache git ca-certificates tzdata
|
|
||||||
|
|
||||||
# Create non-root user for security
|
|
||||||
RUN addgroup -g 1001 -S appgroup && \
|
|
||||||
adduser -u 1001 -S appuser -G appgroup
|
|
||||||
|
|
||||||
# Set working directory
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
# Copy go mod files
|
|
||||||
COPY go.mod go.sum ./
|
|
||||||
|
|
||||||
# Download dependencies
|
|
||||||
RUN go mod download
|
|
||||||
|
|
||||||
# Copy source code
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
# Build the application
|
|
||||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
|
|
||||||
-ldflags='-w -s -extldflags "-static"' \
|
|
||||||
-a -installsuffix cgo \
|
|
||||||
-o bin/api \
|
|
||||||
cmd/api/main.go
|
|
||||||
|
|
||||||
# Final stage
|
|
||||||
FROM scratch
|
|
||||||
|
|
||||||
# Copy ca-certificates from builder
|
|
||||||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
|
||||||
|
|
||||||
# Copy timezone data
|
|
||||||
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
|
|
||||||
|
|
||||||
# Copy user information from builder
|
|
||||||
COPY --from=builder /etc/passwd /etc/passwd
|
|
||||||
COPY --from=builder /etc/group /etc/group
|
|
||||||
|
|
||||||
# Copy the binary
|
|
||||||
COPY --from=builder /app/bin/api /api
|
|
||||||
|
|
||||||
# Copy configuration
|
|
||||||
COPY --from=builder /app/configs /configs
|
|
||||||
|
|
||||||
# Use non-root user
|
|
||||||
USER appuser:appgroup
|
|
||||||
|
|
||||||
# Expose port
|
|
||||||
EXPOSE 8081
|
|
||||||
|
|
||||||
# Health check
|
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
|
||||||
CMD ["/api", "-health-check"] || exit 1
|
|
||||||
|
|
||||||
# Set entrypoint
|
|
||||||
ENTRYPOINT ["/api"]
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
ARG GO_VERSION=1.23
|
|
||||||
ARG BASH_VERSION=5.0
|
|
||||||
ARG BUILD_APP=web
|
|
||||||
|
|
||||||
FROM golang:${GO_VERSION} AS goBuilder
|
|
||||||
WORKDIR /go/src/company/tm_back
|
|
||||||
COPY . .
|
|
||||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o ${BUILD_APP} ./cmd/${BUILD_APP}/main.go
|
|
||||||
|
|
||||||
FROM bash:${BASH_VERSION}
|
|
||||||
RUN apk --no-cache add busybox-suid curl rsync tzdata tcpdump tree ca-certificates s-nail \
|
|
||||||
&& ln -s /usr/bin/mail /usr/bin/s-nail \
|
|
||||||
&& mkdir -p /go/bin/media /go/bin/log /go/bin/config
|
|
||||||
WORKDIR /go/bin
|
|
||||||
COPY --from=goBuilder /go/src/company/tm_back/${BUILD_APP} .
|
|
||||||
EXPOSE 8090
|
|
||||||
ENTRYPOINT /go/bin/${BUILD_APP}
|
|
||||||
+13
-16
@@ -1,25 +1,22 @@
|
|||||||
ARG GO_VERSION=1.23
|
ARG GO_VERSION=1.23
|
||||||
ARG BASH_VERSION=5.0
|
ARG BASH_VERSION=5.0
|
||||||
|
|
||||||
FROM golang:$GO_VERSION AS goBuilder
|
FROM golang:${GO_VERSION} AS go-builder
|
||||||
RUN mkdir -p /usr/local/go/src/company/tm_back
|
|
||||||
COPY . /go/src/company/tm_back/
|
|
||||||
RUN echo $GOROOT
|
|
||||||
|
|
||||||
RUN cd /go/src/company/tm_back \
|
WORKDIR /go/src/company/tm_back
|
||||||
&& CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o web ./cmd/web/main.go
|
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
FROM amd64/bash:$BASH_VERSION
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o web-build ./cmd/web/main.go
|
||||||
|
|
||||||
RUN apk --no-cache add --update busybox-suid \
|
FROM bash:${BASH_VERSION}
|
||||||
&& apk --no-cache add curl rsync tzdata tcpdump tree ca-certificates s-nail \
|
RUN apk --no-cache add busybox-suid curl rsync tzdata tcpdump tree ca-certificates s-nail \
|
||||||
&& ln -s /usr/bin/mail /usr/bin/s-nail \
|
&& ln -s /usr/bin/mail /usr/bin/s-nail \
|
||||||
&& mkdir -p /go/bin/media \
|
&& mkdir -p /go/bin/media /go/bin/log /go/bin/config
|
||||||
&& mkdir -p /go/bin/log \
|
WORKDIR /go/bin
|
||||||
&& mkdir -p /go/bin/config
|
|
||||||
WORKDIR /go/bin/
|
COPY --from=go-builder /go/src/company/tm_back/web-build .
|
||||||
COPY --from=goBuilder /go/src/company/tm_back/web .
|
|
||||||
EXPOSE 8090
|
EXPOSE 8090
|
||||||
# Set the binary.
|
|
||||||
ENTRYPOINT ["/go/bin/web"]
|
ENTRYPOINT /go/bin/web-build
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package bootstrap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Init Application Configuration
|
// Init Application Configuration
|
||||||
func initConfig() Config {
|
func InitConfig() Config {
|
||||||
conf, err := config.LoadConfig(".", &Config{})
|
conf, err := config.LoadConfig(".", &Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("Failed to load config: %v", err))
|
panic(fmt.Sprintf("Failed to load config: %v", err))
|
||||||
@@ -26,7 +26,7 @@ func initConfig() Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Init Logger Service
|
// Init Logger Service
|
||||||
func initLogger(conf config.LoggingConfig) logger.Logger {
|
func InitLogger(conf config.LoggingConfig) logger.Logger {
|
||||||
return logger.NewLogger(&logger.Config{
|
return logger.NewLogger(&logger.Config{
|
||||||
Level: conf.Level,
|
Level: conf.Level,
|
||||||
Format: conf.Format,
|
Format: conf.Format,
|
||||||
@@ -42,7 +42,7 @@ func initLogger(conf config.LoggingConfig) logger.Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Init MongoDB Connection Manager
|
// Init MongoDB Connection Manager
|
||||||
func initMongoDB(conf config.MongoConfig, log logger.Logger) *mongo.ConnectionManager {
|
func InitMongoDB(conf config.MongoConfig, log logger.Logger) *mongo.ConnectionManager {
|
||||||
// Convert infra.MongoConfig to mongo.ConnectionConfig
|
// Convert infra.MongoConfig to mongo.ConnectionConfig
|
||||||
connectionConfig := mongo.ConnectionConfig{
|
connectionConfig := mongo.ConnectionConfig{
|
||||||
URI: conf.URI,
|
URI: conf.URI,
|
||||||
@@ -75,7 +75,7 @@ func initMongoDB(conf config.MongoConfig, log logger.Logger) *mongo.ConnectionMa
|
|||||||
}
|
}
|
||||||
|
|
||||||
// initHTTPServer initializes the Echo HTTP server with middleware
|
// initHTTPServer initializes the Echo HTTP server with middleware
|
||||||
func initHTTPServer(conf Config, log logger.Logger) *echo.Echo {
|
func InitHTTPServer(conf Config, log logger.Logger) *echo.Echo {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
|
|
||||||
// Configure Echo
|
// Configure Echo
|
||||||
@@ -129,7 +129,7 @@ func initHTTPServer(conf Config, log logger.Logger) *echo.Echo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Init Redis Connection Manager
|
// Init Redis Connection Manager
|
||||||
func initRedis(conf config.RedisConfig, log logger.Logger) redis.Client {
|
func InitRedis(conf config.RedisConfig, log logger.Logger) redis.Client {
|
||||||
connectionConfig := &redis.Config{
|
connectionConfig := &redis.Config{
|
||||||
Host: conf.Host,
|
Host: conf.Host,
|
||||||
Port: conf.Port,
|
Port: conf.Port,
|
||||||
@@ -158,7 +158,7 @@ func initRedis(conf config.RedisConfig, log logger.Logger) redis.Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Init Authorization Service
|
// Init Authorization Service
|
||||||
func initAuthorizationService(conf JWTConfig, redisClient redis.Client, log logger.Logger) authorization.AuthorizationService {
|
func InitAuthorizationService(conf JWTConfig, redisClient redis.Client, log logger.Logger) authorization.AuthorizationService {
|
||||||
authConfig := &authorization.AuthorizationConfig{
|
authConfig := &authorization.AuthorizationConfig{
|
||||||
AccessTokenSecret: conf.AccessSecret,
|
AccessTokenSecret: conf.AccessSecret,
|
||||||
RefreshTokenSecret: conf.RefreshSecret,
|
RefreshTokenSecret: conf.RefreshSecret,
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package bootstrap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"tm/pkg/config"
|
"tm/pkg/config"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package bootstrap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
+8
-7
@@ -71,18 +71,19 @@ import (
|
|||||||
"tm/internal/tender_approval"
|
"tm/internal/tender_approval"
|
||||||
"tm/internal/user"
|
"tm/internal/user"
|
||||||
|
|
||||||
|
"tm/cmd/web/bootstrap"
|
||||||
_ "tm/cmd/web/docs" // This is generated by swag
|
_ "tm/cmd/web/docs" // This is generated by swag
|
||||||
"tm/cmd/web/router"
|
"tm/cmd/web/router"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
conf := initConfig()
|
conf := bootstrap.InitConfig()
|
||||||
|
|
||||||
logger := initLogger(conf.Logging)
|
logger := bootstrap.InitLogger(conf.Logging)
|
||||||
defer logger.Sync()
|
defer logger.Sync()
|
||||||
|
|
||||||
// Initialize MongoDB connection manager
|
// Initialize MongoDB connection manager
|
||||||
mongoManager := initMongoDB(conf.Database.MongoDB, logger)
|
mongoManager := bootstrap.InitMongoDB(conf.Database.MongoDB, logger)
|
||||||
defer func() {
|
defer func() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@@ -94,7 +95,7 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Initialize Redis connection manager
|
// Initialize Redis connection manager
|
||||||
redisClient := initRedis(conf.Cache.Redis, logger)
|
redisClient := bootstrap.InitRedis(conf.Cache.Redis, logger)
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := redisClient.Close(); err != nil {
|
if err := redisClient.Close(); err != nil {
|
||||||
logger.Error("Failed to close Redis connection", map[string]interface{}{
|
logger.Error("Failed to close Redis connection", map[string]interface{}{
|
||||||
@@ -104,8 +105,8 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Initialize authorization service
|
// Initialize authorization service
|
||||||
userAuthService := initAuthorizationService(conf.UserAuth.JWT, redisClient, logger)
|
userAuthService := bootstrap.InitAuthorizationService(conf.UserAuth.JWT, redisClient, logger)
|
||||||
customerAuthService := initAuthorizationService(conf.CustomerAuth.JWT, redisClient, logger)
|
customerAuthService := bootstrap.InitAuthorizationService(conf.CustomerAuth.JWT, redisClient, logger)
|
||||||
|
|
||||||
// Initialize repositories with MongoDB connection manager
|
// Initialize repositories with MongoDB connection manager
|
||||||
userRepository := user.NewUserRepository(mongoManager, logger)
|
userRepository := user.NewUserRepository(mongoManager, logger)
|
||||||
@@ -145,7 +146,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Initialize HTTP server
|
// Initialize HTTP server
|
||||||
e := initHTTPServer(conf, logger)
|
e := bootstrap.InitHTTPServer(conf, logger)
|
||||||
|
|
||||||
// Register routes
|
// Register routes
|
||||||
router.RegisterAdminRoutes(e, userHandler, companyHandler, customerHandler, tenderHandler, feedbackHandler, tenderApprovalHandler)
|
router.RegisterAdminRoutes(e, userHandler, companyHandler, customerHandler, tenderHandler, feedbackHandler, tenderApprovalHandler)
|
||||||
|
|||||||
Reference in New Issue
Block a user