Skip to content

Commit

Permalink
update docker config and update redis opts for local
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchitrk committed Dec 11, 2024
1 parent ddda977 commit 4b3dd58
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 32 deletions.
30 changes: 15 additions & 15 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
POSTGRES_USER=zygdev
POSTGRES_PASSWORD=secure
POSTGRES_PASSWORD=VeryS3cure
POSTGRES_DB=zygdb
DATABASE_HOST=database:5432
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE_HOST}/${POSTGRES_DB}

REDIS_ADDR=
REDIS_PASS=
REDIS_ADDR=redis:6379
REDIS_USERNAME=zygdev
REDIS_PASSWORD=redispass
REDIS_TLS_ENABLED=0

# Get these from Supabase console.
SUPABASE_JWT_SECRET=

# Resend for Email APIs.
RESEND_API_KEY=

# Set this to 1 if you want db queries in logs.
ZYG_DB_QUERY_DEBUG=0

# Main API server port
ZYG_SRV_PORT=8000

# External API server port
ZYG_XSRV_PORT=8080

# Set this to '0' if you don't want db queries in logs.
ZYG_DB_QUERY_DEBUG=0

# Cloudflare AccountID
CF_ACCOUNT_ID=

Expand All @@ -33,3 +23,13 @@ R2_ACCESS_KEY_ID=

# Access secret for S3 compat R2 storage
R2_ACCESS_SECRET_KEY=

# Set this to 1 if you want db queries in logs.
ZYG_DB_QUERY_DEBUG=0

# Main API server port
ZYG_SRV_PORT=8080

# External API server port
ZYG_XSRV_PORT=8000

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ go.work
# Secrets or Envs
*.env
*.env.local
.env.yaml
env.sh
env.local.sh


# Python files and directories
Expand Down
16 changes: 11 additions & 5 deletions backend/cmd/srv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,21 @@ func run(ctx context.Context) error {

slog.Info("database", slog.Any("db time", tm.Format(time.RFC1123)))

rdb := redis.NewClient(&redis.Options{
// Redis options
opts := &redis.Options{
Addr: zyg.RedisAddr(),
Username: zyg.RedisUsername(),
Password: zyg.RedisPassword(),
TLSConfig: &tls.Config{
DB: 0,
}

if zyg.RedisTLSEnabled() {
opts.TLSConfig = &tls.Config{
InsecureSkipVerify: true,
},
DB: 0,
})
}
}

rdb := redis.NewClient(opts)

defer func(rdb *redis.Client) {
err := rdb.Close()
Expand Down
16 changes: 11 additions & 5 deletions backend/cmd/xsrv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,21 @@ func run(ctx context.Context) error {

slog.Info("database", slog.Any("db time", tm.Format(time.RFC1123)))

rdb := redis.NewClient(&redis.Options{
// Redis options
opts := &redis.Options{
Addr: zyg.RedisAddr(),
Username: zyg.RedisUsername(),
Password: zyg.RedisPassword(),
TLSConfig: &tls.Config{
DB: 0,
}

if zyg.RedisTLSEnabled() {
opts.TLSConfig = &tls.Config{
InsecureSkipVerify: true,
},
DB: 0,
})
}
}

rdb := redis.NewClient(opts)

defer func(rdb *redis.Client) {
err := rdb.Close()
Expand Down
12 changes: 10 additions & 2 deletions backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,25 @@ func RedisAddr() string {
}

func RedisUsername() string {
value, ok := os.LookupEnv("REDIS_USER")
value, ok := os.LookupEnv("REDIS_USERNAME")
if !ok {
return "zygdev"
}
return value
}

func RedisPassword() string {
value, ok := os.LookupEnv("REDIS_PASS")
value, ok := os.LookupEnv("REDIS_PASSWORD")
if !ok {
return ""
}
return value
}

func RedisTLSEnabled() bool {
enabled, err := strconv.ParseBool(os.Getenv("REDIS_TLS_ENABLED"))
if err != nil {
return false
}
return enabled
}
16 changes: 14 additions & 2 deletions backend/srv.DockerFile
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
FROM golang:1.23 AS builder

ARG ZYG_SRV_PORT=5000
ARG ZYG_SRV_PORT=8080
ARG DATABASE_URL
ARG REDIS_ADDR
ARG REDIS_PASSWORD
ARG REDIS_TLS_ENABLED=0
ARG SUPABASE_JWT_SECRET
ARG RESEND_API_KEY
ARG CF_ACCOUNT_ID
ARG R2_ACCESS_KEY_ID
ARG R2_ACCESS_SECRET_KEY
ARG ZYG_DB_QUERY_DEBUG=0

WORKDIR /usr/src/app
Expand All @@ -19,15 +25,21 @@ COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server ./cmd/srv/main.go

# Build the runtime container image from scratch, copying what is needed from the previous stage.
FROM alpine
FROM alpine:3.21

# Copy the binary to the production image from the builder stage.
COPY --from=builder /usr/src/app/server /usr/local/bin/server

ENV ZYG_SRV_PORT=${ZYG_SRV_PORT}
ENV DATABASE_URL=${DATABASE_URL}
ENV REDIS_ADDR=${REDIS_ADDR}
ENV REDIS_USERNAME=${REDIS_USERNAME}
ENV REDIS_TLS_ENABLED=${REDIS_TLS_ENABLED}
ENV SUPABASE_JWT_SECRET=${SUPABASE_JWT_SECRET}
ENV RESEND_API_KEY=${RESEND_API_KEY}
ENV CF_ACCOUNT_ID=${CF_ACCOUNT_ID}
ENV R2_ACCESS_KEY_ID=${R2_ACCESS_KEY_ID}
ENV R2_ACCESS_SECRET_KEY=${R2_ACCESS_SECRET_KEY}
ENV ZYG_DB_QUERY_DEBUG=${ZYG_DB_QUERY_DEBUG}

EXPOSE ${ZYG_SRV_PORT}
Expand Down
16 changes: 14 additions & 2 deletions backend/xsrv.DockerFile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ FROM golang:1.23 AS builder

ARG ZYG_XSRV_PORT=8000
ARG DATABASE_URL
ARG REDIS_ADDR
ARG REDIS_PASSWORD
ARG REDIS_TLS_ENABLED=0
ARG SUPABASE_JWT_SECRET
ARG RESEND_API_KEY
ARG CF_ACCOUNT_ID
ARG R2_ACCESS_KEY_ID
ARG R2_ACCESS_SECRET_KEY
ARG ZYG_DB_QUERY_DEBUG=0

WORKDIR /usr/src/app
Expand All @@ -19,15 +25,21 @@ COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server ./cmd/xsrv/main.go

# Build the runtime container image from scratch, copying what is needed from the previous stage.
FROM alpine
FROM alpine:3.21

# Copy the binary to the production image from the builder stage.
COPY --from=builder /usr/src/app/server /usr/local/bin/server

ENV ZYG_XSRV_PORT=${ZYG_XSRV_PORT}
ENV ZYG_SRV_PORT=${ZYG_SRV_PORT}
ENV DATABASE_URL=${DATABASE_URL}
ENV REDIS_ADDR=${REDIS_ADDR}
ENV REDIS_USERNAME=${REDIS_USERNAME}
ENV REDIS_TLS_ENABLED=${REDIS_TLS_ENABLED}
ENV SUPABASE_JWT_SECRET=${SUPABASE_JWT_SECRET}
ENV RESEND_API_KEY=${RESEND_API_KEY}
ENV CF_ACCOUNT_ID=${CF_ACCOUNT_ID}
ENV R2_ACCESS_KEY_ID=${R2_ACCESS_KEY_ID}
ENV R2_ACCESS_SECRET_KEY=${R2_ACCESS_SECRET_KEY}
ENV ZYG_DB_QUERY_DEBUG=${ZYG_DB_QUERY_DEBUG}

EXPOSE ${ZYG_XSRV_PORT}
Expand Down
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ services:
ports:
- "5432:5432"

redis:
container_name: redis
image: redis
restart: always
command: redis-server --requirepass ${REDIS_PASSWORD} --user ${REDIS_USERNAME} on '>${REDIS_PASSWORD}' '~*' allcommands
env_file: .env
networks:
- stack
ports:
- "6379:6379"

srv:
container_name: srv
build:
Expand All @@ -33,6 +44,7 @@ services:
restart: always
depends_on:
- database
- redis
env_file: .env
networks:
- stack
Expand All @@ -53,8 +65,10 @@ services:
restart: always
depends_on:
- database
- redis
env_file: .env
networks:
- stack
ports:
- "${ZYG_XSRV_PORT}:${ZYG_XSRV_PORT}"

0 comments on commit 4b3dd58

Please sign in to comment.