Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

FastAPI initial commit

Endurance Idehen6f58e38a77ea
diff --git a/.gitignore b/.gitignore
index b8e5841e..f0a21633 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
# Byte-compiled / optimized / DLL files
+.idea/*
__pycache__/
Data/
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 00000000..8b4bd1f2
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,40 @@
+# syntax=docker/dockerfile:1.2
+
+# Good base image to start from for most development
+FROM python:3.8-slim
+
+# Please remember, the base image we use /must be as small as possible/ for the best
+# production deployments. This is not optional.
+
+WORKDIR /workspace
+
+# Allows python to stream logs rather than buffer them for output.
+ENV PYTHONUNBUFFERED=1
+
+# The official Debian/Ubuntu Docker Image automatically removes the cache by default!
+# Removing the docker-clean file manages that issue.
+RUN rm -rf /etc/apt/apt.conf.d/docker-clean
+
+COPY ./bin/builds/ .
+
+RUN --mount=type=cache,target=/var/cache/apt ./install_packages \
+ dumb-init
+
+# Install pip packages
+ENV PATH=/root/.local/bin:$PATH
+COPY requirements.txt .
+RUN --mount=type=cache,target=/root/.cache pip install --upgrade pip \
+ && pip install -r requirements.txt
+
+# Switch to non-root user
+RUN useradd -m appuser && chown -R appuser /workspace
+USER appuser
+
+# Copy project files
+COPY src/api ./api
+COPY freemocap ./freemocap
+COPY ./bin ./bin
+COPY ./src ./src
+
+ENTRYPOINT ["/usr/bin/dumb-init", "--"]
+CMD ["./bin/run_web_server.sh"]
diff --git a/bin/build.sh b/bin/build.sh
new file mode 100755
index 00000000..02ae771c
--- /dev/null
+++ b/bin/build.sh
@@ -0,0 +1,6 @@
+#!/usr/bin/env bash
+#
+# Run tests meant to run in CI
+
+# https://github.com/moby/moby/issues/39003#issuecomment-852915070 - use buildkit inline cache with --build-arg (in docker-compose file)
+COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose build
diff --git a/bin/builds/install_packages b/bin/builds/install_packages
new file mode 100755
index 00000000..daaf15b2
--- /dev/null
+++ b/bin/builds/install_packages
@@ -0,0 +1,27 @@
+#!/usr/bin/env bash
+# Automating package installation and cleanup!
+# Sourced from minideb:
+# https://github.com/bitnami/minideb/blob/59c31e40dd1c79930aaa6f95ba0e7744d68d8366/mkimage
+set -e
+set -u
+export DEBIAN_FRONTEND=noninteractive
+n=0
+max=2
+until [ $n -gt $max ]; do
+ set +e
+ (
+ apt-get update -qq &&
+ apt-get install -y --no-install-recommends "$@"
+ )
+ CODE=$?
+ set -e
+ if [ $CODE -eq 0 ]; then
+ break
+ fi
+ if [ $n -eq $max ]; then
+ exit $CODE
+ fi
+ echo "apt failed, retrying"
+ n=$(($n + 1))
+done
+rm -r /var/lib/apt/lists /var/cache/apt/archives
diff --git a/bin/run_uvicorn_server.sh b/bin/run_uvicorn_server.sh
new file mode 100644
index 00000000..4a79a398
--- /dev/null
+++ b/bin/run_uvicorn_server.sh
@@ -0,0 +1,6 @@
+#!/usr/bin/env bash
+#
+# Start the webserver
+
+
+uvicorn main:app --reload
\ No newline at end of file
diff --git a/bin/run_web_server.sh b/bin/run_web_server.sh
new file mode 100755
index 00000000..42cad764
--- /dev/null
+++ b/bin/run_web_server.sh
@@ -0,0 +1,6 @@
+#!/usr/bin/env bash
+#
+# Start the webserver
+
+# shellcheck disable=SC2086
+gunicorn -b 0.0.0.0:8080 -w 2 -k uvicorn.workers.UvicornWorker 'src.api.app_factory:create_app()'
\ No newline at end of file
diff --git a/bin/shell.sh b/bin/shell.sh
new file mode 100755
index 00000000..db942981
--- /dev/null
+++ b/bin/shell.sh
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+#
+# Run docker container in bash shell session.
+
+docker-compose run --rm --service-ports app bash
diff --git a/bin/test.sh b/bin/test.sh
new file mode 100755
index 00000000..83831a4b
--- /dev/null
+++ b/bin/test.sh
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+#
+# Run unit tests.
+#
+# -s Show all output, do not capture
+# -v Verbose
+# -q Less verbose
+# -x Stop after first failure
+# -l Show local variables in tracebacks
+# --lf Only run tests that failed last run (or all if none failed)
+# -k "expression" Only run tests that match expession
+# -r chars Show extra test summary info as specified by chars:
+# (f)failed,
+# (E)error
+# (s)skipped
+# (x)failed
+# (X)passed
+# (w)pytest-warnings
+# (p)passed
+# (P)passed with output
+# (a)all except (p) and (P)
+
+docker-compose run --rm app python -m pytest -p no:warnings evaluator/tests/
diff --git a/bin/up.sh b/bin/up.sh
new file mode 100755
index 00000000..75c39aa7
--- /dev/null
+++ b/bin/up.sh
@@ -0,0 +1,7 @@
+#!/usr/bin/env bash
+#
+# Start docker services.
+
+source bin/set_docker_runtime.sh
+
+docker-compose up "$@"
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 00000000..49804e9a
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,11 @@
+version: "3.8"
+
+services:
+ app:
+ build:
+ context: .
+ dockerfile: Dockerfile
+ ports:
+ - "57000:8080"
+ volumes:
+ - .:/workspace
diff --git a/requirements.txt b/requirements.txt
index b4f1baa1..c6465a83 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -12,3 +12,8 @@ numpy<=1.19
typed-ast<1.5,>=1.4.0
moviepy ~=1.0.3
ipython
+pydantic==1.9.0
+uvicorn==0.16.0
+fastapi==0.71.0
+gunicorn==20.1.0
+requests==2.27.1
\ No newline at end of file
diff --git a/src/__init__.py b/src/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/api/__init__.py b/src/api/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/api/app_factory.py b/src/api/app_factory.py
new file mode 100644
index 00000000..0e1974d2
--- /dev/null
+++ b/src/api/app_factory.py
@@ -0,0 +1,12 @@
+from fastapi import FastAPI
+
+from src.api.routes import enabled_routers
+
+
+def create_app():
+ app = FastAPI()
+
+ for router in enabled_routers:
+ app.include_router(router)
+
+ return app
diff --git a/src/api/models/__init__.py b/src/api/models/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/api/routes/__init__.py b/src/api/routes/__init__.py
new file mode 100644
index 00000000..caacecd2
--- /dev/null
+++ b/src/api/routes/__init__.py
@@ -0,0 +1,3 @@
+from src.api.routes.health_check_route import healthcheck_router
+
+enabled_routers = [healthcheck_router]
diff --git a/src/api/routes/health_check_route.py b/src/api/routes/health_check_route.py
new file mode 100644
index 00000000..a9109039
--- /dev/null
+++ b/src/api/routes/health_check_route.py
@@ -0,0 +1,19 @@
+import requests
+from fastapi import APIRouter
+from pydantic import BaseModel
+
+healthcheck_router = APIRouter()
+
+
+class HealthCheckResponse(BaseModel):
+ message: str = "OK"
+
+
+@healthcheck_router.get('/health')
+def route():
+ try:
+ requests.get('http://google.com')
+ except:
+ raise ValueError("Unhealthy")
+ else:
+ return HealthCheckResponse()
diff --git a/src/api/services/__init__.py b/src/api/services/__init__.py
new file mode 100644
index 00000000..e69de29b