Skip to content

as10896/codepraise-app-python

Repository files navigation

CodePraise Python APP

pre-commit Code style: black Imports: isort


Documentation: https://as10896.github.io/codepraise-app-python

Source Code: https://github.com/as10896/codepraise-app-python


This is a Python reproduction of ISS-SOA/codepraise, a demo project for NTHU Service-Oriented Architecture course (for self practice only).

For API / Notifier, please visit codepraise-api-python / codepraise-clone-notifier-python.

The live version can be found here.

Architecture

Architecture

Prerequisite

Install Docker

Make sure you have the latest version of Docker 🐳 installed on your local machine.

Set up session secret for cookie-based session management

Put your session secret key under config/secrets/<env>/SESSION_SECRET.

Or just set the environment variable SESSION_SECRET:

export SESSION_SECRET=<your secret>

One way to generate a key is to use openssl rand

openssl rand -hex 32

Run with Docker

You can start the app easily with Docker Compose.

Before startup, remember to run API in advance and make sure you have all the configurations set up as mentioned before.

Development

Uvicorn (1 worker)

docker compose up -d  # run services in the background
docker compose run --rm console  # run application console
docker compose down  # shut down all the services

After startup, you can visit http://localhost:3000 to see the application's page.

Production

Gunicorn + Uvicorn (4 workers)

docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d  # run services in the background
docker compose -f docker-compose.yml -f docker-compose.prod.yml run --rm console  # run application console
docker compose -f docker-compose.yml -f docker-compose.prod.yml down  # shut down all the servicesvolumes

After startup, you can visit http://localhost:3000 to see the application's page.

BDD Testing

Before testing, remember to run API under test environment in advance.

For users of Intel or AMD64 devices, you can run BDD testing as follows:

docker compose -f docker-compose.test.yml run --rm spec

If you're testing on ARM64 devices (e.g. Apple M1), use the following command instead:

docker compose -f docker-compose.test.yml run --rm spec-arm

Invoke tasks

Here we use Invoke as our task management tool.

You can use the container's bash to test these commands.

docker compose run --rm bash

Commands

inv -l  # show all tasks
inv [task] -h  # show task help message
inv console -e [env]  # run application console (ipython)
inv spec  # run BDD testing (need to run `inv api.run.test` in another process, and make sure the API server has been started)
inv api.run -e [env] -p [port]  # run FastAPI server with specified settings (add `-r` or `--reload` to use auto-reload)
inv api.run.dev -p [port]  # rerun FastAPI server in development environment
inv api.run.test -p [port]  # run FastAPI server in test environment
inv api.run.prod -p [port] -h [host] -w [workers]  # run FastAPI server in production environment (with gunicorn)
inv quality.style  # examine coding style with flake8
inv quality.metric  # measure code metric with radon
inv quality.all  # run all quality tasks (style + metric)
inv quality.reformat  # reformat your code using isort and the black coding style
inv quality.typecheck  # check type with mypy
inv quality  # same as `inv quality.all`