This is a Golang web application that uses:
- PostgreSQL for persistent storage
- Redis for session management
- AWS S3 for media storage
- CloudFront (optional) for CDN-backed media delivery
- golang-migrate for database migrations
The application can be run locally using Docker Compose and deployed to production as a single Go executable, with migrations executed separately.
- Go 1.20+
- Docker & Docker Compose
- AWS CLI
- Make
- Generate an application secret key
openssl rand -hex 32e7ca0377e91a43a5b0a2b30978339e11cbcb1dd4578da7a00fad89bf085acd3b
Set it as an environment variable:
APP_SECRET=<generated_secret>- Configure AWS S3 and CloudFront (optional)
- Create an S3 bucket for storing profile images.
- (Optional) Set up a CloudFront distribution with read access to the bucket.
- Ensure you have an IAM user with
s3:PutObjectpermission.
Set the environment variables:
AWS_REGION=<aws_region>
AWS_S3_BUCKET=<s3_bucket_name>
MEDIA_BASE_URL=<https://cloudfront_or_s3_base_url>
MEDIA_BASE_URLmust includehttps://.
- Install and configure AWS CLI
aws configureVerify credentials:
aws sts get-caller-identityA successful response includes UserId, Account, Arn.
- Start all services
docker compose up- Run database migrations
make migrate-upThis step does the following
- Apply the migrations.
- Remove the container of the migrate service since it is no longer needed.
- Verify the application
Visit in a browser:
- PostgreSQL
Provision a database and set:
DATABASE_URL=postgres://user:password@host:port/dbname?sslmode=disable- Redis
Provision Redis and set:
REDIS_ADDR=<host:port>
REDIS_PASSWORD=<password>
REDIS_DB=<db_number>
SESSION_TTL_HOURS=<hours>- AWS S3 & CloudFront
- Create an S3 bucket for media storage.
- (Optional) Configure CloudFront distribution for read access.
- Ensure IAM user has
s3:PutObjectpermission. - Configure AWS CLI on production:
aws configure- Transfer migration files to the server
scp -r migrations user@server:/opt/myapp/- Run database migrations
migrate -path /opt/myapp/migrations -database "$DATABASE_URL" up- Set AWS-related environment variables
AWS_REGION=<production_region>
AWS_S3_BUCKET=<production_bucket>
MEDIA_BASE_URL=<https://cloudfront_or_s3_base_url>- Build the Go executable
go build -o myapp ./cmd/web- Transfer the binary to the server
scp myapp user@server:/opt/myapp/- Run the application
./myapp- Verify deployment
Check all API endpoints and web routes to ensure they work.
- Migrations are not run automatically; run them before starting the app.
- Media URLs are stored as absolute HTTPS URLs.
- AWS credentials are resolved via the standard AWS chain (
~/.aws/credentials, IAM roles). - The app binary is stateless and safe for horizontal scaling.