Skip to main content

Overview

This guide covers the essential steps for deploying your Go + React scaffold application to production, including building both frontend and backend, environment configuration, and deployment considerations.

Building for Production

Backend Build

The backend uses a simple Makefile to build the Go application into a standalone binary.
cd backend
make build
This command compiles the Go application into a binary named main in the backend directory. The build command is defined as:
go build -o main main.go
For production builds, consider adding build flags for optimization:
go build -ldflags="-s -w" -o main main.go
The -s and -w flags strip debugging information and reduce binary size.

Frontend Build

The frontend uses Vite to create an optimized production build.
cd frontend
npm run build
This generates static assets in the frontend/dist/ directory, optimized and minified for production. To preview the production build locally:
npm run preview

Environment Setup

Backend Environment Variables

Create a production .env file in the backend/ directory:
APP_ENV=production
MONGODB_URI=mongodb://your-production-db:27017
PORT=8080
SESSION_KEY=your-secure-random-session-key
REDIS_URL=redis://your-redis-host:6379
Never commit .env files to version control. Use environment variables or secret management systems in production.

Required Variables

  • APP_ENV: Set to production to enable production mode
  • MONGODB_URI: Your production MongoDB connection string
  • PORT: Server port (default: 8080)
  • SESSION_KEY: Strong random secret for JWT signing (use a cryptographically secure random generator)

Optional Integration Variables

  • REDIS_URL: Redis connection string for caching/sessions
  • PAYPACK_CLIENT_ID: Paypack payment integration client ID
  • PAYPACK_CLIENT_SECRET: Paypack API secret
  • USE_PLUNK: Enable Plunk email integration
  • TELEGRAM_BOT_ID: Telegram bot token for notifications
  • TELEGRAM_CHAT_ID: Telegram chat ID

Frontend Environment

For frontend environment variables, create a .env.production file in the frontend/ directory:
VITE_API_URL=https://api.yourproduction.com
Vite only exposes variables prefixed with VITE_ to the client-side code.

Deployment Strategies

Container Deployment (Docker)

Create a Dockerfile for the backend:
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -ldflags="-s -w" -o main main.go

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/main .
EXPOSE 8080
CMD ["./main"]
For the frontend, serve the built static files with nginx:
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Platform-as-a-Service (PaaS)

Common PaaS options:
  • Backend: Railway, Fly.io, Render, Google Cloud Run
  • Frontend: Vercel, Netlify, Cloudflare Pages
  • Database: MongoDB Atlas (managed MongoDB service)

Virtual Private Server (VPS)

  1. Build the backend binary locally or on the server
  2. Build the frontend and serve with nginx or Apache
  3. Use a process manager like systemd to keep the backend running
  4. Set up reverse proxy (nginx) to route traffic to the backend

Database Deployment

Ensure MongoDB is properly secured:
  • Enable authentication
  • Use TLS/SSL for connections
  • Restrict network access with firewall rules
  • Regular backups
MongoDB Atlas provides a fully managed MongoDB service:
  1. Create a cluster on MongoDB Atlas
  2. Whitelist your application server IPs
  3. Create a database user with appropriate permissions
  4. Copy the connection string to your MONGODB_URI environment variable

Self-Hosted MongoDB

If self-hosting MongoDB:
# Start MongoDB with authentication enabled
docker run -d \
  --name mongodb \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=secure_password \
  -p 27017:27017 \
  -v /data/db:/data/db \
  mongo:latest

Health Checks

Implement health check endpoints for monitoring:
e.GET("/health", func(c echo.Context) error {
    return c.JSON(http.StatusOK, map[string]string{
        "status": "healthy",
    })
})

Pre-Deployment Checklist

  • Build and test both frontend and backend locally
  • Run make test for backend tests
  • Run npm run lint for frontend code quality
  • Set all required environment variables
  • Configure CORS for production domain
  • Set up SSL/TLS certificates
  • Configure logging and monitoring
  • Set up database backups
  • Test with production-like data
  • Review security settings

Next Steps

Production Configuration

Configure security, performance, and optimization settings

Contributing

Learn how to contribute to the project

Build docs developers (and LLMs) love