diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..193dab1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM golang:1.25.5-alpine3.23 as build + +RUN apk add --no-cache git ca-certificates tzdata + +WORKDIR /58team_blog +COPY go.mod go.sum ./ + +RUN go mod download + +COPY . . +RUN GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o /blog ./cmd/main.go + +FROM alpine:3.23 AS deploy + +WORKDIR /58team_blog + +RUN addgroup -g 1001 -S appuser && \ + adduser -u 1001 -S appuser -G appuser + +COPY --from=build /blog /blog + +RUN chown -R appuser:appuser /blog + +USER appuser + +EXPOSE 8080 +EXPOSE 5432 + +CMD ["/blog"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7cd7e0f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,76 @@ +version: '3.8' + +services: + app: + build: . + container_name: blog_backend + ports: + - "8080:8080" + environment: + - GIN_MODE=release + - PORT=8080 + - DATABASE_URL=postgres://userpg:1205@localhost:5432/58blog + depends_on: + - db + - redis + networks: + - app-network + volumes: + - ./logs:/app/logs + - ./uploads:/app/uploads + restart: unless-stopped + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8080/health"] + interval: 30s + timeout: 10s + retries: 3 + + db: + image: postgres:15-alpine + container_name: gin-db + environment: + - POSTGRES_USER=appuser + - POSTGRES_PASSWORD=secretpassword + - POSTGRES_DB=mydb + ports: + - "5432:5432" + volumes: + - postgres_data:/var/lib/postgresql/data + - ./init.sql:/docker-entrypoint-initdb.d/init.sql + networks: + - app-network + restart: unless-stopped + + redis: + image: redis:7-alpine + container_name: gin-redis + ports: + - "6379:6379" + volumes: + - redis_data:/data + networks: + - app-network + restart: unless-stopped + + nginx: + image: nginx:alpine + container_name: gin-nginx + ports: + - "80:80" + - "443:443" + volumes: + - ./nginx.conf:/etc/nginx/nginx.conf:ro + - ./ssl:/etc/nginx/ssl:ro + depends_on: + - app + networks: + - app-network + restart: unless-stopped + +networks: + app-network: + driver: bridge + +volumes: + postgres_data: + redis_data: diff --git a/internal/infrastructure/config.go b/internal/infrastructure/config.go index e0540c5..dc7f834 100644 --- a/internal/infrastructure/config.go +++ b/internal/infrastructure/config.go @@ -6,15 +6,14 @@ import ( ) type Config struct { - DBUser string `mapstructure:"db-user" default:"userpg"` - DBName string `mapstructure:"db-name" default:"58blog"` - DBPass string `mapstructure:"db-password" default:"1205"` - DBHost string `mapstructure:"db-host" default:"localhost"` - DBPort string `mapstructure:"db-port" default:"5432"` - AdminName string `mapstructure:"admin_name" default:"muts"` - AdminPassword string `mapstructure:"admin_pass" default:"1205"` - ImagesPath string `mapstructure:"images_path" default:"./images/"` - PostsPath string `mapstructure:"posts_path" default:"./posts/"` + DBUser string `mapstructure:"DBUser" default:"userpg"` + DBName string `mapstructure:"DBName" default:"58blog"` + DBPass string `mapstructure:"DBPassword" default:"1205"` + DBHost string `mapstructure:"DBHost" default:"localhost"` + DBPort string `mapstructure:"DBPort" default:"5432"` + AdminName string `mapstructure:"ADMIN_NAME" default:"muts"` + AdminPassword string `mapstructure:"ADMIN_PASS" default:"1205"` + ImagesPath string `mapstructure:"IMAGES_PATH" default:"./images/"` } func LoadConfig() (config *Config, err error) { @@ -28,9 +27,12 @@ func LoadConfig() (config *Config, err error) { viper.AddConfigPath(".") viper.AddConfigPath("/etc/58team_blog/") viper.AddConfigPath("/58team_blog/cfgs/") + viper.AutomaticEnv() if err = viper.ReadInConfig(); err != nil { - return nil, err + if _, ok := err.(viper.ConfigFileNotFoundError); !ok { + return nil, err + } } if err = viper.Unmarshal(&config); err != nil {