Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Kalashnikov committed Jan 17, 2019
2 parents 9000ef2 + 65cb61d commit 4ba5505
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
*.pyc
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM alpine:3.8
WORKDIR /opt/pipwa

ENV LIBRARY_PATH=/lib:/usr/lib \
PROJECT_DIR=/opt/pipwa \
APP_DIR=/opt/pipwa/uploader \
PYTHONPATH=/opt/pipwa \
WORKER_COUNT=3

RUN apk add --no-cache --progress python3 python3-dev build-base \
&& rm -rf /var/cache/apk/

RUN pip3 install -U pip gunicorn

COPY reqs/base.txt /tmp/requirements.txt
RUN pip3 install --upgrade -r /tmp/requirements.txt

ADD uploader ${APP_DIR}/

CMD gunicorn -w ${WORKER_COUNT} uploader.main:app -b 0.0.0.0:7777 --worker-class aiohttp.worker.GunicornWebWorker
EXPOSE 7777
3 changes: 3 additions & 0 deletions reqs/base.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
aiohttp==3.5.4
aiohttp_session==2.7.0
aiohttp_cors==0.7.0
Empty file added uploader/__init__.py
Empty file.
44 changes: 44 additions & 0 deletions uploader/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import argparse
import asyncio
import logging
from aiohttp import web

from uploader.router import setup_routes


logging.basicConfig(
format='[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s',
level=logging.DEBUG
)
logger = logging.getLogger('pipwa_uploader.main')


def get_cmd_params():
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', help='verbose output',
action='store_true', default=False)
parser.add_argument('-H', '--host', help='hostname or IP',
default='127.0.0.1')
parser.add_argument('-P', '--port', type=int, help='port',
default=7777)
return parser.parse_args()


def get_app():
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

application = web.Application(loop=loop)
setup_routes(application)
return application


app = get_app()
if __name__ == '__main__':
params = get_cmd_params()
if params.verbose:
print('Server started on {}:{}'.format(params.host, params.port))
web.run_app(app, host=params.host, port=params.port)
34 changes: 34 additions & 0 deletions uploader/router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import logging
import aiohttp_cors

from uploader import views


logger = logging.getLogger('pipwa_uploader.router')


def setup_routes(app):
"""Setup routes and appropriate CORS headers."""
resources = {
'/uploader/store': {
'POST': views.storage.upload_view
},
}
logger.info('Routes registered:')

cors = aiohttp_cors.setup(app, defaults={
# allow to read all resources for all
'*': aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers='*',
allow_headers='*',
allow_methods=['OPTIONS'],
max_age=3600),
})

for path, methods in resources.items():
res = cors.add(app.router.add_resource(path))
for method, handler in methods.items():
cors.add(res.add_route(method, handler))
logger.info('CORS: [{}] handler <{}:{}> on path "{}" wrapped.'.format(
method, handler.__module__, handler.__name__, path))
1 change: 1 addition & 0 deletions uploader/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import storage
10 changes: 10 additions & 0 deletions uploader/views/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import logging
from aiohttp import web


logger = logging.getLogger('pipwa_uploader.view')


async def upload_view(request):
logger.info(f'Uploading request')
return web.Response()

0 comments on commit 4ba5505

Please sign in to comment.