From 3ab97b171a5069f59d8d69a43f1be04f2928f968 Mon Sep 17 00:00:00 2001 From: Oleg Silkin Date: Tue, 21 May 2019 15:13:08 -0400 Subject: [PATCH] Refactor + fault-tolerance --- config/nginx.conf | 27 ++++++++++++++++++++++++ schema/db_helpers.py | 2 +- {lbry_comment_server => src}/__init__.py | 0 src/data.conf | 17 +++++++++++++++ {lbry_comment_server => src}/database.py | 2 +- {lbry_comment_server => src}/handles.py | 8 +++---- {lbry_comment_server => src}/main.py | 16 ++++++++------ {lbry_comment_server => src}/settings.py | 0 {lbry_comment_server => src}/writes.py | 2 +- tests/database_test.py | 4 ++-- tests/testcase.py | 4 ++-- 11 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 config/nginx.conf rename {lbry_comment_server => src}/__init__.py (100%) create mode 100644 src/data.conf rename {lbry_comment_server => src}/database.py (99%) rename {lbry_comment_server => src}/handles.py (91%) rename {lbry_comment_server => src}/main.py (89%) rename {lbry_comment_server => src}/settings.py (100%) rename {lbry_comment_server => src}/writes.py (93%) diff --git a/config/nginx.conf b/config/nginx.conf new file mode 100644 index 0000000..0c82c89 --- /dev/null +++ b/config/nginx.conf @@ -0,0 +1,27 @@ +worker_processes 1; +daemon off; +error_log /dev/stdout warn; + +events { + worker_connections 1024; + accept_mutex off; +} + +http { + default_type application/json; + access_log off; + + server { + listen 5921; + location /api { + proxy_set_header HOST $host; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + + proxy_redirect off; + proxy_pass http://localhost:2903; + + } + } +} diff --git a/schema/db_helpers.py b/schema/db_helpers.py index 37f8717..ddc7b2e 100644 --- a/schema/db_helpers.py +++ b/schema/db_helpers.py @@ -1,7 +1,7 @@ import logging import sqlite3 -from lbry_comment_server.settings import config +from src.settings import config logger = logging.getLogger(__name__) diff --git a/lbry_comment_server/__init__.py b/src/__init__.py similarity index 100% rename from lbry_comment_server/__init__.py rename to src/__init__.py diff --git a/src/data.conf b/src/data.conf new file mode 100644 index 0000000..baeb5b4 --- /dev/null +++ b/src/data.conf @@ -0,0 +1,17 @@ +location / { + limit_except GET HEAD { + deny all; + } +} + +location /api/ { + limit_except GET HEAD { + deny all; + } +} + +location /api { + limit_except GET HEAD POST { + + } +} \ No newline at end of file diff --git a/lbry_comment_server/database.py b/src/database.py similarity index 99% rename from lbry_comment_server/database.py rename to src/database.py index da231d5..a16e9af 100644 --- a/lbry_comment_server/database.py +++ b/src/database.py @@ -7,7 +7,7 @@ import typing import aiosqlite import nacl.hash -from lbry_comment_server.settings import config +from src.settings import config logger = logging.getLogger(__name__) diff --git a/lbry_comment_server/handles.py b/src/handles.py similarity index 91% rename from lbry_comment_server/handles.py rename to src/handles.py index 37fed6f..c25d49a 100644 --- a/lbry_comment_server/handles.py +++ b/src/handles.py @@ -5,10 +5,10 @@ import asyncio from aiohttp import web from aiojobs.aiohttp import atomic -from lbry_comment_server.writes import write_comment -from lbry_comment_server.database import get_claim_comments -from lbry_comment_server.database import get_comments_by_id, get_comment_ids -from lbry_comment_server.database import obtain_connection +from src.writes import write_comment +from src.database import get_claim_comments +from src.database import get_comments_by_id, get_comment_ids +from src.database import obtain_connection logger = logging.getLogger(__name__) diff --git a/lbry_comment_server/main.py b/src/main.py similarity index 89% rename from lbry_comment_server/main.py rename to src/main.py index 829b729..9ef240a 100644 --- a/lbry_comment_server/main.py +++ b/src/main.py @@ -5,13 +5,13 @@ import asyncio from aiohttp import web import schema.db_helpers -from lbry_comment_server.database import obtain_connection -from lbry_comment_server.handles import api_endpoint -from lbry_comment_server.settings import config -from lbry_comment_server.writes import create_comment_scheduler, DatabaseWriter +from src.database import obtain_connection +from src.handles import api_endpoint +from src.settings import config +from src.writes import create_comment_scheduler, DatabaseWriter logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) +# logger.setLevel(logging.DEBUG) formatter = logging.Formatter(config['LOGGING_FORMAT']) debug_handler = logging.FileHandler(config['PATH']['LOG']) @@ -86,14 +86,16 @@ async def stop_app(runner): await runner.cleanup() -async def run_app(app, duration=3600): +async def run_app(app): runner = None try: runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner, config['HOST'], config['PORT']) await site.start() - await asyncio.sleep(duration) + while True: + + await asyncio.sleep(2**63) except asyncio.CancelledError as cerr: pass finally: diff --git a/lbry_comment_server/settings.py b/src/settings.py similarity index 100% rename from lbry_comment_server/settings.py rename to src/settings.py diff --git a/lbry_comment_server/writes.py b/src/writes.py similarity index 93% rename from lbry_comment_server/writes.py rename to src/writes.py index 020eac2..8556880 100644 --- a/lbry_comment_server/writes.py +++ b/src/writes.py @@ -4,7 +4,7 @@ import logging import aiojobs from asyncio import coroutine -from lbry_comment_server.database import obtain_connection, create_comment +from src.database import obtain_connection, create_comment logger = logging.getLogger(__name__) diff --git a/tests/database_test.py b/tests/database_test.py index 98673bf..ac50fa0 100644 --- a/tests/database_test.py +++ b/tests/database_test.py @@ -5,10 +5,10 @@ from faker.providers import internet from faker.providers import lorem from faker.providers import misc -from lbry_comment_server.database import get_comments_by_id, create_comment, get_comment_ids, create_comment_async, \ +from src.database import get_comments_by_id, create_comment, get_comment_ids, create_comment_async, \ get_claim_comments from schema.db_helpers import setup_database, teardown_database -from lbry_comment_server.settings import config +from src.settings import config from tests.testcase import DatabaseTestCase, AsyncioTestCase fake = faker.Faker() diff --git a/tests/testcase.py b/tests/testcase.py index 8a2c66f..d6fbef6 100644 --- a/tests/testcase.py +++ b/tests/testcase.py @@ -4,9 +4,9 @@ from unittest.case import _Outcome import asyncio -from lbry_comment_server.database import obtain_connection +from src.database import obtain_connection from schema.db_helpers import setup_database, teardown_database -from lbry_comment_server.settings import config +from src.settings import config class AsyncioTestCase(unittest.TestCase):