aaadevs.com is a website project built using FastAPI, combining backend and frontend using Jinja2Templates for dynamic HTML rendering.
This project serves as a foundation for further development and is designed to help developers learn and experiment with:
- Using FastAPI to build web applications.
- Rendering HTML pages server-side with Jinja2Templates.
- Setting up CI/CD pipelines with Docker and GitLab.
Feel free to use this project as an example or starting point for your own web applications, contribute to its development, or adapt it to fit your needs. Contributions are welcome!
Currently, the project includes the following components:
- Home Page: The main landing page of the website.
- Static Files:
robots.txt
: Provides instructions for web crawlers.sitemap.xml
: Helps search engines index the site's pages.favicon.ico
: The small icon that appears in the browser tab or bookmark bar.
- Contact Form Handler: Processes submissions from a contact form.
cd backend
python3 -m venv env
source env/bin/activate
pip install -r requirements.txt
deactivate
Create a .env file in the project root with the following variables:
PROFILES=dev # Use "prod" for production, "dev" for development
ALLOWED_ORIGINS=http://localhost,http://127.0.0.1 # For development
DB_URL=your_database_url # Database URL string. For "dev" can be ""
python main.py
To run tests, use the pytest framework. Ensure you have pytest installed:
pip install pytest
Run the tests with the following command:
pytest
To measure code coverage, use the pytest-cov plugin. Install it with:
pip install pytest-cov
Run tests with coverage reporting:
pytest --cov=.
To generate a more detailed HTML coverage report:
pytest --cov=. --cov-report=html
The coverage report will be generated in the htmlcov directory. Open the report in your browser:
open htmlcov/index.html
The process of setting up CI/CD using GitLab CI, GitLab Runners, Docker Compose, and Nginx.
Create a server (e.g., on AWS, DigitalOcean, Azure, or any cloud provider). Obtain the server’s IP address: xxx.xxx.xxx.xxx
Buy a domain name from a registrar (e.g., GoDaddy, Namecheap, or Google Domains). The domain is required to enable HTTPS.
Update the domain’s DNS settings to point to your server’s IP address: Set an A record for @ (root domain) pointing to xxx.xxx.xxx.xxx with a TTL of 1 hour.
ssh user@xxx.xxx.xxx.xxx
sudo apt-get update
sudo apt-get install git
Search for the official Docker installation guide (Google: “install docker ubuntu”). Run the two main like:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
git --version
docker --version
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
Stop the Nginx container temporarily if it’s running:
docker stop my-nginx-container
Obtain the SSL certificate:
sudo certbot certonly --standalone -d your_domain.com
Restart the Nginx container if needed:
docker start my-nginx-container
sudo crontab -e
Add the following line to renew certificates and restart Nginx automatically:
0 3 * * * certbot renew --quiet && docker stop my-nginx-container && docker start my-nginx-container
To test renewal manually:
sudo certbot renew --dry-run
Go to your GitLab Project Settings > CI/CD > Variables. Add environment variables required for the application.
In GitLab, go to Settings > CI/CD > Runners. Uncheck the Enable instance runners for this project option to ensure the runner is project-specific.
Create a New Project-Specific Runner: We use a Docker container to run the GitLab Runner instead of running it as a standalone service.
Add your user to the Docker group if needed:
sudo usermod -aG docker $USER
Start the GitLab Runner:
docker run -d --name gitlab-runner --restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:alpine
Register the Runner:
docker run --rm -it \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
gitlab/gitlab-runner:alpine register
During registration, you’ll need:
- Token: Available in the GitLab project’s CI/CD > Runners settings.
- Executor: Choose
docker
. - Default Docker Image: Use
docker:dind
.
Update config.toml for the Runner:
nano /srv/gitlab-runner/config/config.toml
Update the volumes section:
volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
Push your code to GitLab.
The .gitlab-ci.yml
pipeline uses the docker-compose.yml
file to build, test, and deploy the backend and Nginx services.