Skip to content

Commit

Permalink
Merge pull request #17 from frafra/fastapi
Browse files Browse the repository at this point in the history
FastAPI instead of Hug
  • Loading branch information
frafra authored Feb 27, 2020
2 parents a930f48 + c0b29bf commit 4ed4a5b
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 176 deletions.
11 changes: 4 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
FROM fedora
FROM python:3.7-slim
LABEL maintainer="[email protected]"

WORKDIR /src
ADD poetry.lock pyproject.toml .

RUN dnf -y install poetry sqlite && \
dnf -y install gcc python3-devel && \
poetry install --no-root --no-dev && \
dnf -y remove gcc python3-devel && \
dnf clean all
RUN pip install poetry && \
poetry install --no-root --no-dev

ADD . .

EXPOSE 8000

CMD ["/usr/bin/poetry", "run", "uwsgi", "--ini", "uwsgi.ini"]
CMD ["/usr/local/bin/poetry", "run", "uvicorn", "gpapconv:app"]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Demo: https://gpapconv.frafra.eu/
## Dependencies

- [Python 3](https://www.python.org/)
- [hug](http://www.hug.rest/)
- [uWSGI](https://uwsgi-docs.readthedocs.io/)
- [FastAPI](https://fastapi.tiangolo.com/)
- [Uvicorn](https://www.uvicorn.org/)
- [Poetry](poetry.eustace.io/)
- [sqlite](https://sqlite.org/)

Expand All @@ -18,7 +18,7 @@ Demo: https://gpapconv.frafra.eu/

```
$ poetry install --no-root --no-dev
$ poetry run uwsgi --ini uwsgi.ini
$ poetry run uvicorn gpapconv:app
```

### Docker image
Expand Down
54 changes: 31 additions & 23 deletions gpapconv.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
#!/usr/bin/env python3

import hug

import output_format
from fastapi import FastAPI, File, UploadFile
from starlette.responses import FileResponse, Response

import functools
import anosql
import datetime
import json
import shutil
import sqlite3
import tempfile

@functools.lru_cache(maxsize=8)
def read_file(path):
with open(path) as fp:
return fp.read()
queries = anosql.from_path('queries.sql', 'sqlite3')

def run_query_on_db(db_path, query_path):
def run_query_on_db(db_path, query_name):
with sqlite3.connect(db_path) as con:
cur = con.cursor()
query = read_file(query_path)
result = cur.execute(query).fetchone()[0]
return result
return getattr(queries, query_name)(con)[0][0]

def run_query_on_form(body, field_name, query_path):
content = body[field_name]
def run_query_on_form(file, query_name):
with tempfile.NamedTemporaryFile() as tf:
tf.write(content)
result = run_query_on_db(tf.name, query_path)
return result
shutil.copyfileobj(file.file, tf)
return run_query_on_db(tf.name, query_name)

@hug.post('/gpap2osm', output=output_format.xml)
def gpap2osm(body):
return run_query_on_form(body, 'file', 'queries/gpap2osm.sql')
def generate_filename(extension):
return 'geopap-{}.{}'.format(datetime.date.today(), extension)

@hug.post('/gpap2geojson', output=output_format.geojson)
def gpap2osm(body):
return run_query_on_form(body, 'file', 'queries/gpap2geojson.sql')
app = FastAPI()

@app.get("/")
async def index():
return FileResponse('web/index.html')

@app.post("/gpap2osm")
async def gpap2osm(response: Response, file: UploadFile = File(...)):
response.headers['Content-Disposition'] = generate_filename('xml')
result = run_query_on_form(file, 'gpap2osm')
return Response(result, media_type="application/xml")

@app.post("/gpap2geojson")
async def gpap2geojson(response: Response, file: UploadFile = File(...)):
response.headers['Content-Disposition'] = generate_filename('geojson')
result = run_query_on_form(file, 'gpap2geojson')
return json.loads(result)
21 changes: 0 additions & 21 deletions output_format.py

This file was deleted.

Loading

1 comment on commit 4ed4a5b

@vercel
Copy link

@vercel vercel bot commented on 4ed4a5b Feb 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.