Lab-Coleccionista-FastAPI-MongoDB-Ionic

This repository is for experimentation only, for the study of the infrastructure and development cycle of the portfolio project for DuocUC.

Table of contents

Notes

  • Bug: If I've updated the repository secrets, I need to update the environment variables in Deta Space because they are not updated automatically.

Published API

Deta Space

How to publish the API to DetaSpace

Needed Github Repository Secrets

  • ACCESS_TOKEN: deta access token
  • PROJECT_ID: deta project id
  • MONGO_USERNAME: mongo username
  • MONGO_PASSWORD: mongo password
  • MONGO_CLUSTER: mongo cluster

Publish the API

  • Make a pull request to the main branch:
    • The GitHub Action will run the tests for being able to merge the pull request.
    • When merged, the GitHub Action will publish the API to Deta Space.

How to set up the local environment

Set Up FastAPI and MongoDB with Docker

Requirements

  • Linux:

  • Windows:

    • Docker Desktop
    • Manage docker in wsl2 distro terminal (Optional but recommended because it's faster and enable hot reload):
      • After installing Docker Desktop, install a Linux distro with WSL2: Tutorial
      • Enable Docker support in your WSL 2 distro: Tutorial
      • (optional) configure the WSL2 for comfortable development:
      • Notes:
        • Clone the repository in the wsl2 distro filesystem, not in Windows filesystem.
        • You can manage the containers in Windows by referring to the WSL2 distro's filesystem..
        • If you don´t want to edit the files in the WSL2 distro, you can edit them in Windows(access Linux from Windows) and the changes will be reflected in the WSL2 distro filesystem.
  • MongoDB Compass

Run Docker Containers

  • Execute docker compose up -d at the root of the project (if you encounter an error about the compose command, try with docker-compose up -d, as this may occur with older versions of Docker).
  • When finished, navegate to localhost:8000/docs to view the api documentation

Connect to the Database via MongoDB Compass

  • Use the example below (the username and password are defined in the compose.yaml file): image

Set Up the Ionic APP Locally

Requirements

  • Node v20.11.1
  • Ionic 7.1.1: npm install -g @ionic/cli@7.1.1
  • Angular 17.1.1: npm install -g @angular/cli@17.1.1
  • Android Studio

Running the App

  • Ensure that the FastAPI and MongoDB containers are running.
  • Open your terminal and navigate to the ./mobile/coleccti-mate/ folder.
  • Execute the command: ionic serve
  • Optional: To test the app with the production configuration (which accesses the DetaSpace API instead of the local API), use: ionic serve --prod
  • When finished, navegate to localhost:8100/ to view the Ionic app.

How to Manage the Environment

Manage the containers (basic usage) - FastAPI and MongoDB

  • Execute docker compose start at the root of the project to start the containers.
  • Execute docker compose stop at the root of the project to stop the containers.
  • Note:
    • The API container have hot reload enabled, so you don't need to restart the containers when you make changes to the code.

Run the tests - FastAPI

  • When the containers are running, execute docker exec -it coleccionista-api-test bash to enter the container.
  • Once inside the container:
    • Execute pytest to run all the tests.
    • Execute pytest -k <filename>.py to run a specific test.
    • To see the verbose output of the tests, add the -v or -vv flags to the previous commands.

Project Structure

Route: ./

  • .github/ - GitHub Actions workflows (CI/CD)
  • api/ - API configuration files and source code
  • mobile/ - Ionic app configuration files and source code
  • .dockerignore - Files to ignore when building the Docker images
  • .gitignore - Files to ignore when pushing to the repository
  • .spaceignore - Files to ignore when deploying to Deta Space
  • README.md - Documentation of the project
  • Spacefile - Deta Space configuration file (for deployment)
  • compose.yaml - Docker Compose configuration file for local development

Route: ./api/

  • app/ - API source code (FastAPI)
  • Dockerfile - Docker configuration file for the FastAPI app
  • requirements.txt - Python dependencies for the FastAPI app

Route: ./api/app/

  • dependencies/ - Avaliable dependencies for the API (db, auth, etc.)
  • models/ - Pydantic models for be used in the endpoints
  • routers/ - API source code for each set of endpoints ( for separation of concerns)
  • tests/ - API tests (pytest)
  • README.md - Specific documentation for the API (not as a replacement for the Swagger docs)
  • main.py - API main file ( app creation, routers addition, definition of the lifespan events, etc. )
  • settings.py - Enums; environment variables, secrets, etc.

Route: ./mobile/

  • ./coleccti-mate/ - APP source code (Ionic)
  • Dockerfile - Docker configuration file for the Ionic app

References:

API

Database

Deployment

Continuous Integration/Continuous Deployment

Docker

Axios