Added: Dockerfile, support for configurations from env
This commit is contained in:
parent
ea8ab7c0ed
commit
c1ed822293
3 changed files with 117 additions and 10 deletions
29
Dockerfile
Normal file
29
Dockerfile
Normal file
|
|
@ -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"]
|
||||||
76
docker-compose.yml
Normal file
76
docker-compose.yml
Normal file
|
|
@ -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:
|
||||||
|
|
@ -6,15 +6,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DBUser string `mapstructure:"db-user" default:"userpg"`
|
DBUser string `mapstructure:"DBUser" default:"userpg"`
|
||||||
DBName string `mapstructure:"db-name" default:"58blog"`
|
DBName string `mapstructure:"DBName" default:"58blog"`
|
||||||
DBPass string `mapstructure:"db-password" default:"1205"`
|
DBPass string `mapstructure:"DBPassword" default:"1205"`
|
||||||
DBHost string `mapstructure:"db-host" default:"localhost"`
|
DBHost string `mapstructure:"DBHost" default:"localhost"`
|
||||||
DBPort string `mapstructure:"db-port" default:"5432"`
|
DBPort string `mapstructure:"DBPort" default:"5432"`
|
||||||
AdminName string `mapstructure:"admin_name" default:"muts"`
|
AdminName string `mapstructure:"ADMIN_NAME" default:"muts"`
|
||||||
AdminPassword string `mapstructure:"admin_pass" default:"1205"`
|
AdminPassword string `mapstructure:"ADMIN_PASS" default:"1205"`
|
||||||
ImagesPath string `mapstructure:"images_path" default:"./images/"`
|
ImagesPath string `mapstructure:"IMAGES_PATH" default:"./images/"`
|
||||||
PostsPath string `mapstructure:"posts_path" default:"./posts/"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig() (config *Config, err error) {
|
func LoadConfig() (config *Config, err error) {
|
||||||
|
|
@ -28,10 +27,13 @@ func LoadConfig() (config *Config, err error) {
|
||||||
viper.AddConfigPath(".")
|
viper.AddConfigPath(".")
|
||||||
viper.AddConfigPath("/etc/58team_blog/")
|
viper.AddConfigPath("/etc/58team_blog/")
|
||||||
viper.AddConfigPath("/58team_blog/cfgs/")
|
viper.AddConfigPath("/58team_blog/cfgs/")
|
||||||
|
viper.AutomaticEnv()
|
||||||
|
|
||||||
if err = viper.ReadInConfig(); err != nil {
|
if err = viper.ReadInConfig(); err != nil {
|
||||||
|
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err = viper.Unmarshal(&config); err != nil {
|
if err = viper.Unmarshal(&config); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue