Added an index page the hits the API for some data

deb
androiddrew 7 years ago
parent b048fe1254
commit a2e3ad84c1

@ -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)

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cookie API</title>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<h1>APIStar Cookies</h1>
<div id="app">
<ul>
<li v-for="cookie in cookies">
{{ cookie.name }}
</li>
</ul>
</div>
<script>
let vm = new Vue({
el: '#app',
data: {
cookies: []
},
created() {
axios.get("http://localhost:5000/cookies").then(
(response) => {
this.cookies = response.data.data
})
},
});
</script>
</body>
</html>
Loading…
Cancel
Save