Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
FROM python:3.10-slim
# https://github.com/astral-sh/uv-docker-example/blob/main/multistage.Dockerfile
FROM ghcr.io/astral-sh/uv:python3.10-bookworm-slim AS builder

ENV UV_COMPILE_BYTECODE=1
ENV UV_LINK_MODE=copy
ENV UV_PYTHON_DOWNLOADS=0

WORKDIR /app

RUN pip install --no-cache-dir --upgrade gunicorn
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project --no-dev

ADD . /app

RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-dev

FROM python:3.10-slim-bookworm

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY --from=builder --chown=app:app /app /app

ENV PATH="/app/.venv/bin:$PATH"

WORKDIR /app

COPY pnogo_api pnogo_api
EXPOSE 8000

CMD ["gunicorn", "-w 2", "-b :8080", "pnogo_api.run:app"]
CMD ["granian", "--interface", "wsgi", "--workers", "2", "--host", "0.0.0.0", "--port", "8080", "pnogo_api.run:app"]
29 changes: 13 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,32 @@ Pnogo API

## Requirements

To use the API you need to install the dependencies specified inside `requirements.txt`. You can quickly install them using PIP:
This project uses [uv](https://github.com/astral-sh/uv) to handle dependencies. To install them in a virtual environment, simply run:

```bash
pip install -r requirements.txt
uv sync
```

The same can be done on Windows by opening the included `install.bat` file.

## Usage

To start the API we need to first define some environment variables used by Flask. By setting the `FLASK_ENV` variable to `development` we enable some very useful functionalities, such as auto refresh on save (so we don't need to reopen Flask every time we edit the code) and the included debugger.

This can be done in Windows using `set`, while on linacs `export` must be used. For example, in Windows:
To start the API run this command:

```shell
set FLASK_APP=run
set FLASK_ENV=development
```bash
uv run -m flask --app pnogo_api/run run --debug
```

Then, from the root of the project, we need to enter the directory `pnogo_api`, where the code is:
## Style

This project uses [ruff](https://github.com/astral-sh/ruff) for code formatting, and it's installed in the virtual environment automatically.

To format your code, simply run:

```bash
cd pnogo_api
ruff format
```

Finally, we can start serving the API using Flask by giving the following command:
To run the linter for any errors or issues:

```bash
python -m flask run
ruff check
```

A `start.bat` has been included to ease development when using Windows.
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
db:
image: postgres:14
environment:
POSTGRES_DB: pnogo
POSTGRES_USER: pnogo
POSTGRES_PASSWORD: pnogo
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:
1 change: 0 additions & 1 deletion install.bat

This file was deleted.

9 changes: 7 additions & 2 deletions pnogo_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def create_app(test_config=None):

if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
app.config.from_pyfile("config.py", silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
Expand All @@ -29,16 +29,21 @@ def create_app(test_config=None):
pass

from . import db

db.init_app(app)

from . import auth
from pnogo_api.auth import require_app_key

from . import auth

app.register_blueprint(auth.bp)

from . import common

app.register_blueprint(common.bp)

from . import api

app.register_blueprint(api.bp)

CORS(app)
Expand Down
Loading
Loading