|
| 1 | +--- |
| 2 | +title: Deployment Guide |
| 3 | +description: Learn how to deploy the API server to a production environment. |
| 4 | +--- |
| 5 | + |
| 6 | +import { Steps, Aside } from '@astrojs/starlight/components'; |
| 7 | + |
| 8 | +This guide provides the steps to deploy the API server for production use. The recommended method is to build a Docker container and host it on a cloud service that supports containers. |
| 9 | + |
| 10 | +<Steps> |
| 11 | +1. **Configure Production Environment Variables** |
| 12 | + |
| 13 | + Before building your application for production, you must configure your environment variables. Unlike the `.env` file used for local development, production environments should use the hosting provider's system for managing secrets (e.g., GitHub Secrets for build pipelines, or environment variable settings in your hosting dashboard). |
| 14 | + |
| 15 | + The following variables are **required** for production: |
| 16 | + - `DATABASE_URL`: The connection string for your production MongoDB database. |
| 17 | + - `JWT_SECRET_KEY`: A long, complex, and secret string for signing authentication tokens. |
| 18 | + - `SENDGRID_API_KEY`: Your API key for the SendGrid service. |
| 19 | + - `DEFAULT_SENDER_EMAIL`: The email address used to send transactional emails (like OTP codes). |
| 20 | + - `OTP_TEMPLATE_ID`: The ID of your dynamic email template in SendGrid. |
| 21 | + |
| 22 | + You should also set `JWT_ISSUER` to the public URL of your API. |
| 23 | + |
| 24 | +2. **Build the Docker Container** |
| 25 | + |
| 26 | + The API server repository includes a `Dockerfile` that simplifies the process of creating a production-ready container. This file handles setting up the Dart environment, installing dependencies, and creating an optimized production build of the server. |
| 27 | + |
| 28 | + To build the container locally, run the following command from the root of the `flutter-news-app-api-server-full-source-code` directory: |
| 29 | + |
| 30 | + ```bash |
| 31 | + docker build -t your-api-server-image . |
| 32 | + ``` |
| 33 | + |
| 34 | + Replace `your-api-server-image` with a name for your Docker image. |
| 35 | + |
| 36 | +3. **Deploy to a Hosting Provider** |
| 37 | + |
| 38 | + Once you have built the Docker image, you can deploy it to any hosting provider that supports Docker containers. You will typically push your image to a container registry (like Docker Hub, Google Container Registry, or Amazon ECR) and then deploy it from there. |
| 39 | + |
| 40 | + Popular choices for hosting Docker containers include: |
| 41 | + - [Google Cloud Run](https://cloud.google.com/run/docs/deploying) |
| 42 | + - [Amazon Elastic Container Service (ECS)](https://aws.amazon.com/ecs/) |
| 43 | + - [DigitalOcean App Platform](https://www.digitalocean.com/docs/app-platform/how-to/deploy-from-dockerfile/) |
| 44 | + - [Render](https://render.com/docs/deploy-a-docker-image) |
| 45 | + |
| 46 | + Follow your chosen provider's documentation for deploying a container from an image or a `Dockerfile`. During the setup process on the hosting platform, you will need to configure the environment variables mentioned in Step 1. |
| 47 | + |
| 48 | +</Steps> |
| 49 | + |
| 50 | +<Aside type="caution" title="CORS Configuration"> |
| 51 | +For your web-based dashboard to communicate with the API in production, you must correctly configure Cross-Origin Resource Sharing (CORS). Set the `CORS_ALLOWED_ORIGIN` environment variable on your server to the exact URL of your deployed web dashboard (e.g., `https://dashboard.yourdomain.com`). |
| 52 | +</Aside> |
0 commit comments