Added default application settings in application factory

email
AndroidDrew 7 years ago
parent e4a6ee8f83
commit 708bf23870

@ -46,6 +46,14 @@ def create_cookie(session: Session, json_data: http.RequestData, route: Router):
return http.Response(cookie.to_dict(), status=201, headers=headers) return http.Response(cookie.to_dict(), status=201, headers=headers)
def delete_cookie(session: Session, id: int):
cookie = session.query(Cookie).filter_by(id=id).one_or_none()
if cookie is None:
msg = {"error": "404 Not Found"}
return http.Response(msg, status=404)
session.delete(cookie)
return {"message": "200 OK"}
routes = [ routes = [
Route('/state', 'GET', get_state), Route('/state', 'GET', get_state),
Route('/cookies', 'GET', get_cookies), Route('/cookies', 'GET', get_cookies),
@ -55,6 +63,8 @@ routes = [
Include('/static', static_urls) Include('/static', static_urls)
] ]
app_settings = {}
routes = routes + auth_routes routes = routes + auth_routes
commands = sqlalchemy_backend.commands + commands commands = sqlalchemy_backend.commands + commands
@ -64,7 +74,7 @@ components = sqlalchemy_backend.components + auth_components
def application_factory(settings={}): def application_factory(settings={}):
"""Returns an instance of Cookie API""" """Returns an instance of Cookie API"""
return App(settings=settings, return App(settings={**app_settings, **settings},
commands=commands, commands=commands,
components=components, components=components,
routes=routes) routes=routes)

@ -1,6 +1,7 @@
import os
import sys
from cookie_api import JSONRenderer, Base, application_factory from cookie_api import JSONRenderer, Base, application_factory
settings = { settings = {
'DATABASE': { 'DATABASE': {
'URL': 'postgresql://apistar:local@localhost/apistar', 'URL': 'postgresql://apistar:local@localhost/apistar',
@ -12,6 +13,36 @@ settings = {
} }
} }
def prepare_import(path):
"""Given a filename this will try to calculate the python path, add it
to the search path and return the actual module name that is expected.
"""
path = os.path.realpath(path)
if os.path.splitext(path)[1] == '.py':
path = os.path.splitext(path)[0]
if os.path.basename(path) == '__init__':
path = os.path.dirname(path)
module_name = []
# move up until outside package structure (no __init__.py)
while True:
path, name = os.path.split(path)
module_name.append(name)
if not os.path.exists(os.path.join(path, '__init__.py')):
break
if sys.path[0] != path:
sys.path.insert(0, path)
return '.'.join(module_name[::-1])
app = application_factory(settings)
if __name__ == "__main__": if __name__ == "__main__":
app = application_factory(settings)
app.main() app.main()

Loading…
Cancel
Save