From a2e3ad84c179c0ade8ef80e15f8f11c0f8269d38 Mon Sep 17 00:00:00 2001 From: androiddrew Date: Tue, 8 May 2018 19:34:10 -0400 Subject: [PATCH] Added an index page the hits the API for some data --- cookie_api/app.py | 29 ++++++++++++++++++++++++++--- cookie_api/static/index.html | 33 +++++++++++++++++++++++++++++++++ cookie_api/templates/.gitignore | 0 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 cookie_api/static/index.html create mode 100644 cookie_api/templates/.gitignore diff --git a/cookie_api/app.py b/cookie_api/app.py index bb36fc5..0361d93 100644 --- a/cookie_api/app.py +++ b/cookie_api/app.py @@ -1,11 +1,21 @@ +from apistar import Route +from apistar.http import HTMLResponse from apistar_jwt import JWT +import logbook from sqlalchemy import create_engine +from pathlib import Path from cookie_api.auth import auth_routes from cookie_api.logger import global_init from cookie_api.resources import cookie_routes from cookie_api.util import SQLAlchemyHook, SQLAlchemySession, MetaApp as App +BASEDIR = Path(__file__).parent +TEMPLATE_DIR = BASEDIR.joinpath('templates') +STATIC_DIR = BASEDIR.joinpath('static') + +logger = logbook.Logger(__name__) + engine = create_engine('postgresql://apistar@localhost:5432/apistar') app_settings = { @@ -14,7 +24,15 @@ app_settings = { } } -_routes = cookie_routes + auth_routes + +def return_index_html(): + with open(STATIC_DIR.joinpath('index.html'), mode='rb') as f: + return HTMLResponse(content=f.read()) + + +index = Route('/', 'GET', return_index_html) + +_routes = cookie_routes + auth_routes + [index] _hooks = [SQLAlchemyHook] @@ -27,12 +45,17 @@ _components = [ ] -def application_factory(routes=_routes, components=_components, hooks=_hooks, settings={}, ): +def application_factory(routes=_routes, components=_components, hooks=_hooks, settings={}, template_dir=TEMPLATE_DIR, + static_dir=STATIC_DIR): """Returns an instance of Cookie API""" _settings = {**app_settings, **settings} global_init(_settings) + logger.info("Template directory {}".format(TEMPLATE_DIR)) + logger.info("Static directory {}".format(STATIC_DIR)) return App(components=components, event_hooks=hooks, - routes=routes) + routes=routes, + template_dir=template_dir.name, # have to use name because of Jinja2 + static_dir=static_dir) diff --git a/cookie_api/static/index.html b/cookie_api/static/index.html new file mode 100644 index 0000000..6e8b546 --- /dev/null +++ b/cookie_api/static/index.html @@ -0,0 +1,33 @@ + + + + + Cookie API + + + + +

APIStar Cookies

+
+ +
+ + + \ No newline at end of file diff --git a/cookie_api/templates/.gitignore b/cookie_api/templates/.gitignore new file mode 100644 index 0000000..e69de29