This is a Go-based backend API that uses MongoDB, Redis, and Elasticsearch. The project includes endpoints for user authentication, profile management, and access control with roles and permissions.
- User registration, login, and email verification
- JWT-based authentication
- Role-based access control
- Redis for caching and storing verification codes
- Elasticsearch for logging
- Swagger for API documentation
- Docker
- Docker Compose
-
Clone the repository:
git clone https://github.com/yourusername/yourproject.git cd yourproject
-
Create a
.env
file:touch .env
Add the following environment variables to the
.env
file (update with your actual values):MONGO_URI=mongodb://mongo:27017/mdmdb REDIS_ADDR=redis:6379 SMTP_HOST=smtp.mailtrap.io SMTP_PORT=587 SMTP_USER=your_mailtrap_user SMTP_PASSWORD=your_mailtrap_password JWT_SECRET=your_jwt_secret ELASTICSEARCH_URL=http://elasticsearch:9200
-
Build and run the Docker containers:
docker-compose up --build
This will start the application along with MongoDB, Redis, and Elasticsearch services.
-
Access the application:
- API:
http://localhost:8080
- Swagger Documentation:
http://localhost:8080/swagger/index.html
- API:
-
Update the system:
sudo apt update && sudo apt upgrade -y
-
Install required packages:
sudo apt install curl git -y
-
Install Docker:
curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh
-
Add user to Docker group:
sudo usermod -aG docker ${USER}
Log out and log back in to apply the group changes.
-
Verify Docker installation:
docker --version
-
Download Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
-
Apply executable permissions:
sudo chmod +x /usr/local/bin/docker-compose
-
Verify Docker Compose installation:
docker-compose --version
-
Clone your project:
git clone https://github.com/manvfx/unified-go-backend.git cd yourproject
-
Create a
.env
file:nano .env
Add the following environment variables to the
.env
file (update with your actual values):MONGO_URI=mongodb://mongo:27017/mdmdb REDIS_ADDR=redis:6379 SMTP_HOST=smtp.mailtrap.io SMTP_PORT=587 SMTP_USER=your_mailtrap_user SMTP_PASSWORD=your_mailtrap_password JWT_SECRET=your_jwt_secret ELASTICSEARCH_URL=http://elasticsearch:9200
-
Build and start the containers:
docker-compose up --build -d
The
-d
flag runs the containers in detached mode. -
Verify the deployment:
docker-compose ps
Ensure all containers are running. Check the logs if there are any issues:
docker-compose logs
Your application should now be running on http://<your-vps-ip>:8080
. Verify by visiting the URL or using a tool like Postman to test the endpoints.
For production deployment, it's a good practice to use a reverse proxy like Nginx to manage SSL certificates and handle requests.
-
Install Nginx:
sudo apt install nginx -y
-
Configure Nginx:
Create a new Nginx configuration file:
sudo nano /etc/nginx/sites-available/unified-go-backend
Add the following configuration (update
server_name
with your domain or VPS IP):server { listen 80; server_name your_domain_or_ip; location / { proxy_pass http://localhost:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
Enable the configuration and restart Nginx:
sudo ln -s /etc/nginx/sites-available/unified-go-backend /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl restart nginx
-
Optional: Set Up SSL with Let's Encrypt
If you have a domain, secure your application with SSL using Let's Encrypt.
-
Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
-
Obtain and install SSL certificate:
sudo certbot --nginx -d your_domain
Follow the prompts to complete the installation. Certbot will automatically configure Nginx to use the SSL certificate.
-
- Mohammad Amin Najafi - manvfx
This project is licensed under the MIT License - see the LICENSE file for details.