This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
from sqlalchemy import create_engine | |
from sqlalchemy.orm import sessionmaker | |
import settings as app_settings | |
engine = create_engine(app_settings.DB_CONNECTION) | |
Session = sessionmaker(bind=engine) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from db.session import open_db_session | |
def with_db_session(fn): | |
@wraps(fn) | |
def wrapper(*args, **kwargs): | |
if kwargs.get('db_session') is None: | |
with open_db_session() as db_session: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import falcon | |
import settings as app_settings | |
from api.resources import ( | |
SettingsResource, | |
) | |
from middlewares import ( | |
ContentEncodingMiddleware, | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
'accounts_max_friends': 3, | |
'urls': { | |
'accounts': '/api/accounts/<%= accountId %>', | |
'contacts': '/api/contacts/', | |
'profile': '/api/profile/' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import settings as app_settings | |
class SettingsResource: | |
@staticmethod | |
def on_get(_, resp): | |
frontend_urls_prefix = 'FURL_' | |
business_logic_keys = ( | |
'accounts_max_friends', | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ACCOUNTS_MAX_FRIENDS = 3 | |
# FRONTEND URL SECTION | |
FURL_CONTACTS = '/api/contacts/' | |
FURL_PROFILE = '/api/profile/' | |
FURL_ACCOUNTS = '/api/accounts/<%= accountId %>' | |
# BACKEND SECTION | |
SETTINGS_ROUTE = '/settings' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
from middlewares import ( | |
ContentEncodingMiddleware, | |
) | |
app = falcon.API(middleware=[ | |
ContentEncodingMiddleware(), | |
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# api.py | |
from datetime import datetime | |
class RestResource: | |
def on_get(self, _, resp): | |
resp.body = { | |
'result': 'Wow! Resul!:)', | |
'timestamp': datetime.utcnow(), | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gunicorn.conf.py | |
import multiprocessing | |
bind = '0.0.0.0:8000' | |
workers = multiprocessing.cpu_count() * 2 + 1 | |
timeout = 30 | |
worker_connections = 1000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# middlewares.py | |
import json | |
class JSONEncoder(json.JSONEncoder): | |
def default(self, obj): | |
if isinstance(obj, bytes): | |
return obj.decode("utf-8") | |
return super(JSONEncoder, self).default(obj) | |