Skip to content
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
43 changes: 43 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Git files
.git
.gitignore
.gitattributes

# Documentation
*.md
docs/

# Test files
test/
*_test.go
coverage.out

# Environment files
.env
.env.local
.env.*.local

# Build artifacts
bin/
tmp/
*.exe
*.dll
*.so
*.dylib

# IDE and editor files
.vscode/
.idea/
*.swp
*.swo
*~
.DS_Store

# CI/CD
.github/

# Makefile (optional - uncomment if you want to exclude it)
# Makefile

# License (optional - uncomment if you want to exclude it)
# LICENSE
43 changes: 43 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# GoChat Server Configuration
# Copy this file to .env and modify as needed

# Server Configuration
# Port on which the server will listen (default: :8080)
# Format: :PORT or HOST:PORT
SERVER_PORT=:8080

# Allowed Origins for CORS (comma-separated list)
# Use "*" to allow all origins (NOT recommended for production)
# Examples:
# - Single origin: http://localhost:3000
# - Multiple origins: http://localhost:3000,https://example.com,https://app.example.com
# - Allow all: *
ALLOWED_ORIGINS=http://localhost:8080,http://localhost:3000

# Maximum Message Size
# Maximum size in bytes for incoming WebSocket messages (default: 512)
# Helps prevent denial-of-service attacks from oversized messages
MAX_MESSAGE_SIZE=512

# Rate Limiting Configuration
# Maximum number of messages allowed in a burst (default: 5)
# Controls how many messages a client can send before being rate limited
RATE_LIMIT_BURST=5

# Rate limit refill interval in seconds (default: 1)
# How often the rate limit bucket refills
RATE_LIMIT_REFILL_INTERVAL=1

# Production Environment Example:
# SERVER_PORT=:8080
# ALLOWED_ORIGINS=https://chat.example.com,https://app.example.com
# MAX_MESSAGE_SIZE=1024
# RATE_LIMIT_BURST=10
# RATE_LIMIT_REFILL_INTERVAL=2

# Development Environment Example:
# SERVER_PORT=:8080
# ALLOWED_ORIGINS=http://localhost:3000,http://localhost:8080
# MAX_MESSAGE_SIZE=512
# RATE_LIMIT_BURST=5
# RATE_LIMIT_REFILL_INTERVAL=1
60 changes: 60 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Multi-stage Dockerfile for GoChat Server
# This creates a minimal, secure production image

# Stage 1: Build stage
FROM golang:1.25.1-alpine AS builder

# Install build dependencies
RUN apk add --no-cache ca-certificates git tzdata

# Set working directory
WORKDIR /build

# Copy go mod files first for better caching
COPY go.mod go.sum ./
RUN go mod download && go mod verify

# Copy source code
COPY . .

# Build the application with security optimizations
# -trimpath removes file system paths from the executable
# -ldflags="-s -w" strips debug information to reduce binary size
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-trimpath \
-ldflags="-s -w -X main.version=${VERSION:-dev}" \
-o gochat \
./cmd/server

# Stage 2: Runtime stage
FROM alpine:3.20

# Install runtime dependencies and create non-root user
RUN apk add --no-cache ca-certificates tzdata && \
addgroup -g 1000 gochat && \
adduser -D -u 1000 -G gochat gochat

# Set working directory
WORKDIR /app

# Copy the binary from builder
COPY --from=builder /build/gochat .

# Copy timezone data
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo

# Change ownership to non-root user
RUN chown -R gochat:gochat /app

# Switch to non-root user
USER gochat

# Expose the default port (can be overridden with environment variable)
EXPOSE 8080

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/ || exit 1

# Run the application
ENTRYPOINT ["/app/gochat"]
Loading
Loading