Skip to content

Microservice demo with FastApi - Production Ready

License

Notifications You must be signed in to change notification settings

myshy93/math-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Math Microservice

Simple math operations done in a microservice.

Tech stack:

  • FastAPI
  • Uvicorn
  • AioKafka

Install requirements

pip -m venv venv
. venv/bin/activate
pip install -r requirements.txt

Configurable environment settings

Variable name Description Default
API_V1_STR API V1 prefix /api/v1
SECRET_KEY Secret key for JWT Randon generated
ACCESS_TOKEN_EXPIRE_MINUTES JWT Expire minutes 11520 (8 days)
PROJECT_NAME Project name Math Microservice
DEBUG Enable debugging False
DATABASE_URL Database URL sqlite:///./db-files/math.db
SERVER_URL Trusted hostname localhost
SENTRY_DSN Sentry DSN None
KAFKA_SERVER Kafka broker address localhost
KAFKA_PORT Kafka broker port 9092

NOTE: When DEBUG is True logging verbosity will increase and default users will be created.

NOTE: In DEBUG mode all hosts will be treated as trusted.

Run the server for development

python -m uvicorn app.main:app --reload

Run tests

# run in root dir
export DEBUG=True
python -m pytest -vv

Deploy with docker

  1. Copy .env.sample and rename it .env. Here you will place all configuration variables.

     cp .env.sample .env
  2. Run docker compose

    cd kafka
    docker-compose up -d
    cd ..
    docker-compose up -d
  3. [Optional] Rebuild image

    docker-compose up -d --build

How to use

Default user/password

email: [email protected]
password: test

Read events from Kafka

docker exec --interactive --tty broker \
kafka-console-consumer --bootstrap-server broker:9092 \
                       --topic quickstart \
                       --from-beginning

Known issues

  1. ELK stack is not stable and needs configuration adjustments.
  2. Documentation UI will return null (as represented result) for large int numbers returned by endpoints.