Skip to content

Commit

Permalink
feat: add basic fastapi setup (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
allisson authored Aug 27, 2022
1 parent 803b70c commit 12e71a5
Show file tree
Hide file tree
Showing 11 changed files with 582 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.git/
.github/
.pytest_cache/
.venv/
.env
52 changes: 52 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
##### Builder Stage #####
FROM python:3.10-slim as builder

# Set default path
ENV PATH="/app/.venv/bin:${PATH}"

# Set default workdir
WORKDIR /app

# Create virtualenv and install Python packages
RUN pip install --no-cache-dir pip -U && \
pip install --no-cache-dir poetry && \
poetry config virtualenvs.in-project true
COPY ./poetry.lock poetry.lock
COPY ./pyproject.toml pyproject.toml
RUN poetry install --no-dev

# Copy app files and add directory to workdir
COPY fastqueue ./fastqueue

##### Final Stage #####
FROM python:3.10-slim

# Disable Prompt During Packages Installation
ARG DEBIAN_FRONTEND=noninteractive

# Set default path
ENV PATH="/app/.venv/bin:${PATH}"
ENV PYTHONPATH /app

# Copy content from builder stage
COPY --from=builder /app /app

# Install packages
RUN apt-get update && \
apt-get install --no-install-recommends -y tini && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Add fastqueue user and create directories
RUN useradd -m fastqueue && mkdir -p /app

# Set permissions
RUN chown -R fastqueue:fastqueue /app

# Set workdir and XAPO User
WORKDIR /app
USER fastqueue

# Set entrypoint and cmd
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["python", "/app/fastqueue/main.py"]
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ rm-db:
docker kill $$(docker ps -aqf name=postgres-fastqueue)
docker container rm $$(docker ps -aqf name=postgres-fastqueue)

.PHONY: test lint run-db rm-db
build-image:
docker build --rm -t fastqueue .

run-server:
poetry run python fastqueue/main.py

.PHONY: test lint run-db rm-db build-image run-server
6 changes: 6 additions & 0 deletions env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fastqueue_debug='true'
fastqueue_server_host='127.0.0.1'
fastqueue_server_port='8000'
fastqueue_server_reload='true'
fastqueue_server_num_workers='1'
fastqueue_log_level='info'
18 changes: 18 additions & 0 deletions fastqueue/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pydantic import BaseSettings


class Settings(BaseSettings):
debug: bool = False
server_host: str = "0.0.0.0"
server_port: int = 8000
server_reload: bool = False
server_num_workers: int = 1
log_level: str = "info"

class Config:
env_file = ".env"
env_file_encoding = "utf-8"
env_prefix = "fastqueue_"


settings = Settings()
27 changes: 27 additions & 0 deletions fastqueue/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import uvicorn
from fastapi import FastAPI

from fastqueue.config import settings

app = FastAPI(debug=settings.debug)


@app.get("/")
def read_root():
return {"Hello": "World"}


def run_server():
uvicorn.run(
"fastqueue.main:app",
debug=settings.debug,
host=settings.server_host,
port=settings.server_port,
log_level=settings.log_level.lower(),
reload=settings.server_reload,
workers=settings.server_num_workers,
)


if __name__ == "__main__":
run_server()
Loading

0 comments on commit 12e71a5

Please sign in to comment.