Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sennierer committed Feb 2, 2024
0 parents commit b4c9275
Show file tree
Hide file tree
Showing 65 changed files with 4,245 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "fastapi-rdf-utils"]
path = rdf-fastapi-utils
url = https://github.com/acdh-oeaw/rdf-fastapi-utils.git
[submodule "fastapi-versioning"]
path = fastapi-versioning
url = https://github.com/acdh-oeaw/fastapi-versioning.git
49 changes: 49 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
FROM python:3.10
#RUN addgroup --system app && adduser --system --group app
ARG USERNAME=app
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Create the user
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME

WORKDIR /app/
# https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE
# Prevents Python from writing .pyc files to disk
ENV PYTHONDONTWRITEBYTECODE 1

# ensures that the python output is sent straight to terminal (e.g. your container log)
# without being first buffered and that you can see the output of your application (e.g. django logs)
# in real time. Equivalent to python -u: https://docs.python.org/3/using/cmdline.html#cmdoption-u
ENV PYTHONUNBUFFERED 1
ENV ENVIRONMENT prod
ENV TESTING 0

RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/opt/poetry python && \
cd /usr/local/bin && \
ln -s /opt/poetry/bin/poetry && \
poetry config virtualenvs.create false

#
COPY ./pyproject.toml ./poetry.lock* /app/

# Allow installing dev dependencies to run tests
ARG INSTALL_DEV=false

RUN bash -c "if [ $INSTALL_DEV == 'true' ] ; then poetry install --no-root ; else poetry install --no-root --without dev ; fi"

COPY . /app

ENV PYTHONPATH=/app

# chown all the files to the app user
RUN chown -R app:app $HOME && chown -R app /usr/local && chown -R app /app

# change to the app user
# Switch to a non-root user, which is recommended by Heroku.
USER app

#
#CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
CMD ["bash", "start.sh"]
12 changes: 12 additions & 0 deletions fastapi-versioning/.bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[bumpversion]
current_version = 0.10.0
commit = True
tag = True
tag_name = {new_version}
message = Bump version from {current_version} to {new_version}
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)((-rc(?P<rc>\d+))?)
serialize =
{major}.{minor}.{patch}-rc{rc}
{major}.{minor}.{patch}

[bumpversion:file:setup.py]
109 changes: 109 additions & 0 deletions fastapi-versioning/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
Pipfile.Lock

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

# editor config
.vscode/
.idea/
13 changes: 13 additions & 0 deletions fastapi-versioning/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: python
install: pip install tox-travis
script: tox
stages:
- name: test
jobs:
include:
- stage: test
python: 3.6
- stage: test
python: 3.7
- stage: test
python: 3.8
21 changes: 21 additions & 0 deletions fastapi-versioning/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Dean Way

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions fastapi-versioning/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
uvicorn = "*"
tox = "*"
pytest = "*"
typing_extensions = "*"
bumpversion = "*"
requests = "*"
mypy = "*"
black = "==20.8b1"
isort = "*"
flake8 = "*"

[packages]
fastapi = ">=0.56.0"
starlette = "==0.13.6"
fastapi-versioning = {path = ".",editable = true}
150 changes: 150 additions & 0 deletions fastapi-versioning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# fastapi-versioning
api versioning for fastapi web applications

# Installation

`pip install fastapi-versioning`

## Examples
```python
from fastapi import FastAPI
from fastapi_versioning import VersionedFastAPI, version

app = FastAPI(title="My App")


@app.get("/greet")
@version(1, 0)
def greet_with_hello():
return "Hello"


@app.get("/greet")
@version(1, 1)
def greet_with_hi():
return "Hi"


app = VersionedFastAPI(app)
```

this will generate two endpoints:
```
/v1_0/greet
/v1_1/greet
```
as well as:
```
/docs
/v1_0/docs
/v1_1/docs
/v1_0/openapi.json
/v1_1/openapi.json
```

There's also the possibility of adding a set of additional endpoints that
redirect the most recent API version. To do that make the argument
`enable_latest` true:

```python
app = VersionedFastAPI(app, enable_latest=True)
```

this will generate the following additional endpoints:
```
/latest/greet
/latest/docs
/latest/openapi.json
```
In this example, `/latest` endpoints will reflect the same data as `/v1.1`.

Try it out:
```sh
pip install pipenv
pipenv install --dev
pipenv run uvicorn example.annotation.app:app
# pipenv run uvicorn example.folder_name.app:app
```

## Usage without minor version
```python
from fastapi import FastAPI
from fastapi_versioning import VersionedFastAPI, version

app = FastAPI(title='My App')

@app.get('/greet')
@version(1)
def greet():
return 'Hello'

@app.get('/greet')
@version(2)
def greet():
return 'Hi'

app = VersionedFastAPI(app,
version_format='{major}',
prefix_format='/v{major}')
```

this will generate two endpoints:
```
/v1/greet
/v2/greet
```
as well as:
```
/docs
/v1/docs
/v2/docs
/v1/openapi.json
/v2/openapi.json
```

## Extra FastAPI constructor arguments

It's important to note that only the `title` from the original FastAPI will be
provided to the VersionedAPI app. If you have any middleware, event handlers
etc these arguments will also need to be provided to the VersionedAPI function
call, as in the example below

```python
from fastapi import FastAPI, Request
from fastapi_versioning import VersionedFastAPI, version
from starlette.middleware import Middleware
from starlette.middleware.sessions import SessionMiddleware

app = FastAPI(
title='My App',
description='Greet uses with a nice message',
middleware=[
Middleware(SessionMiddleware, secret_key='mysecretkey')
]
)

@app.get('/greet')
@version(1)
def greet(request: Request):
request.session['last_version_used'] = 1
return 'Hello'

@app.get('/greet')
@version(2)
def greet(request: Request):
request.session['last_version_used'] = 2
return 'Hi'

@app.get('/version')
def last_version(request: Request):
return f'Your last greeting was sent from version {request.session["last_version_used"]}'

app = VersionedFastAPI(app,
version_format='{major}',
prefix_format='/v{major}',
description='Greet users with a nice message',
middleware=[
Middleware(SessionMiddleware, secret_key='mysecretkey')
]
)
```
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions fastapi-versioning/example/annotation/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from fastapi import FastAPI

from example.annotation import item, store
from fastapi_versioning import VersionedFastAPI

app = FastAPI(title="My Online Store")
app.include_router(store.router)
app.include_router(item.router)
app = VersionedFastAPI(app, enable_latest=True)
Loading

0 comments on commit b4c9275

Please sign in to comment.