Skip to content

App for managing endpoints dynamically at runtime

Notifications You must be signed in to change notification settings

AddChew/dynamic-routing

Repository files navigation

linting: pylint Pytest

Dynamic Routing

Application to add, edit or remove FastAPI endpoints dynamically at runtime without having to restart the application.

Built with:

  • FastAPI
  • Tortoise ORM
  • PostgreSQL

Motivation

The UAT environment has insufficient compute resources for every machine learning model to host its own separate model serving application. In most cases, only a mock model serving API is required for integration tests. One potential solution is to have a centralized application to host all the mock model serving APIs. This project thus seeks to provide a centralized mock API hosting application, with an admin service, that allows users to self-service the management of their mock APIs (i.e. add, edit, remove) without having to restart the main application.

Demo

How the app works

Each uploaded script functions as an independent sub application, with its own authorization method and Swagger UI, mounted onto the main application at the path /{username}/{project_name}.

Installation

Run via docker compose

  1. Set POSTGRES_PASSWORD environment variable
export POSTGRES_PASSWORD=<your postgres password>
  1. From project root folder (i.e. dynamic-routing), run the following command:
./docker_setup.sh
  1. Navigate to the following urls to access the respective services:
URL Service
http://localhost:8000/docs SwaggerUI
http://localhost:8000/redoc Redoc

Run locally

  1. From project root folder (i.e. dynamic-routing), run the following command:
./local_setup.sh
  1. Navigate to the following urls to access the respective services:
URL Service
http://localhost:8000/docs SwaggerUI
http://localhost:8000/redoc Redoc

Usage

Launch the application via one of the two installation methods. Navigate to http://localhost:8000/docs to access the Swagger UI, which we will be using to call the APIs. Refer to the demo video on how to use the APIs.

Limitations

Shared conda environment

Each sub application can only use libraries that are installed in the shared conda environment. This could be resolved by building the main application with Ray Serve, which supports independent runtime environments per serve deployment.

Single app file

All of the logic of a sub application has to be confined within one file (i.e. app.py). In most cases, this should suffice, given that each sub application only hosts mock APIs with simple logic.

Fixed variable name for FastAPI app

The FastAPI app variable in each sub application has to be named "app" (i.e. app = FastAPI()).

How to run tests

From project root folder (i.e. dynamic-routing), execute the following command:

./run_tests.sh