Skip to content

Commit

Permalink
Create second service for delivery pdf.
Browse files Browse the repository at this point in the history
  • Loading branch information
Djapy committed Feb 21, 2019
0 parents commit d3aa3be
Show file tree
Hide file tree
Showing 14 changed files with 659 additions and 0 deletions.
120 changes: 120 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Created by .ignore support plugin (hsz.mobi)
### VirtualEnv template
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
.Python
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
.venv
pip-selfcheck.json
### Python template
# 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

# 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/
.idea
19 changes: 19 additions & 0 deletions Docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "3"
services:
aiohttp:
build: .
volumes:
- ./:/app
command: gunicorn delivery_pdf.main:init -b :8500 --worker-class aiohttp.GunicornWebWorker --reload --access-logfile -
ports:
- "8500:8500"
networks:
- host
athena:
image: arachnysdocker/athenapdf-service:latest
ports:
- "8080:8080"
networks:
- host
networks:
host:
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.7

WORKDIR /app

ADD . /app

RUN python setup.py develop

EXPOSE 8500
14 changes: 14 additions & 0 deletions config/delivery_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[server]

host = 'aiohttp'
port = '8500'

[service]

host = 'athena'
port = '8080'

[external]

host = '0.0.0.0'
port = '8500'
Empty file added config/test_config.yml
Empty file.
1 change: 1 addition & 0 deletions delivery_pdf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.0.1'
5 changes: 5 additions & 0 deletions delivery_pdf/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from main import main


if __name__ == '__main__':
main()
24 changes: 24 additions & 0 deletions delivery_pdf/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from aiohttp import web

from .settings import config_t
from .routes import setup_routes
from .views import CreatePDF


async def init(argv=None):
app = web.Application()
server = config_t['server']
service = config_t['service']
handler = CreatePDF(server, service)
setup_routes(app, handler)
app['config'] = config_t['external']
# host = config_t['external']['host']
# port = config_t['external']['port']

return app


def main():
app = init()
web.run_app(app)

9 changes: 9 additions & 0 deletions delivery_pdf/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .views import index


def setup_routes(app, handler):
router = app.router
h = handler
router.add_get('/', index)
router.add_get('/raw/{uuid}', h.fetch_html)
router.add_post('/generate', h.convert_html)
15 changes: 15 additions & 0 deletions delivery_pdf/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pathlib
import toml


BASE_DIR = pathlib.Path(__file__).parent.parent
config = BASE_DIR / 'config' / 'delivery_config.toml'


def get_config(path):
with open(path) as f:
conf = toml.load(f)
return conf


config_t = get_config(config)
49 changes: 49 additions & 0 deletions delivery_pdf/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from aiohttp import web, ClientSession, ClientTimeout
import uuid


async def index(request):
return web.Response(text="aiohttp status: on-line")


class CreatePDF:

def __init__(self, server, servise):
self.uuid_dict = {}
self.server = server
self.service = servise
self.add_uuid = None

def get_uuid(self):
return str(uuid.uuid4())

async def fetch_pdf(self, uuid):
host_service = self.service['host']
port_service = self.service['port']
host_server = self.server['host']
port_server = self.server['port']

url = 'http://{}:{}/convert'.format(host_service, port_service)
url_server = 'http://{}:{}/raw/{}'.format(host_server, port_server, uuid)
params = {'auth': 'arachnys-weaver', 'url': url_server}
timeout = ClientTimeout(total=5*60, connect=2*60, sock_connect=60, sock_read=60)
async with ClientSession(timeout=timeout) as session:
async with session.get(url=url, params=params) as resp:
return resp.content

async def convert_html(self, request):
if request.method == 'POST':
content = await request.read()
add_uuid = self.get_uuid()
self.uuid_dict[add_uuid] = content
pdf_content = await self.fetch_pdf(add_uuid)
self.uuid_dict.pop(add_uuid)
return web.Response(body=pdf_content, content_type='application/pdf')

async def fetch_html(self, request):
str_uuid = request.match_info['uuid']
html = self.uuid_dict.get(str_uuid)
if not html:
web.HTTPNotFound()
html_str = html.decode('utf-8')
return web.Response(text=html_str)
Loading

0 comments on commit d3aa3be

Please sign in to comment.