import os from apistar.renderers import HTMLRenderer, JSONRenderer from common.auth.auth import SessionAuthentication, TokenAuthentication, BasicAuthentication from apistar.hooks import render_response from common.hooks import handle_options, AcceptOrigin settings = { "MONGO": { "host": os.environ.get("ABI_MONGO_HOST", "localhost"), "user": os.environ.get("ABI_MONGO_USER", "abigale"), "password": os.environ.get("ABI_MONGO_PASSWORD", "abigale"), }, "AUTHENTICATION": [ SessionAuthentication(), TokenAuthentication(), BasicAuthentication(), ], 'STATICS': { 'ROOT_DIR': 'web/dist', # Include the 'statics/' directory. 'PACKAGE_DIRS': ['apistar'] # Include the built-in apistar static files. }, "AFTER_REQUEST": [ render_response, handle_options, AcceptOrigin, ], 'RENDERERS': [JSONRenderer(), HTMLRenderer()] }
import os import tempfile import pytest from apistar import Route, TestClient, annotate from apistar.exceptions import TemplateNotFound from apistar.frameworks.asyncio import ASyncIOApp from apistar.frameworks.wsgi import WSGIApp from apistar.interfaces import Templates from apistar.renderers import HTMLRenderer @annotate(renderers=[HTMLRenderer()]) def get_and_render_template(username: str, templates: Templates): index = templates.get_template('index.html') return index.render(username=username) routes = [ Route('/get_and_render_template/', 'GET', get_and_render_template), ] @pytest.mark.parametrize('app_class', [WSGIApp, ASyncIOApp]) def test_get_and_render_template(app_class): with tempfile.TemporaryDirectory() as tempdir: path = os.path.join(tempdir, 'index.html') with open(path, 'w') as index: index.write('<html><body>Hello, {{ username }}</body></html>')
# interact with the API. This is automatically made available (if built) every # time Cadastre starts. # # Most of the heavy lifting for this is done by Webpack, so see # `webpack.config.js` for pointers. Webpack places its output in `static` in # this application; the following rules ensure that the files found there are # mapped to predictable and unsurprising URLs. # ## WEB APPLICATION CONFIGURATION # The UI needs one extra route, to serve the `index.html` page at the `/` URL. from apistar import Route, annotate from apistar.renderers import HTMLRenderer @annotate(renderers=[HTMLRenderer()], exclude_from_schema=True) def ui(): # This could be cached on first rendering, and in production that'd be a # somewhat smart thing to do, but during development, the page content # changes fairly regularly. Refresh-driven development is a nice way to # build HTML5 apps, so reload on each pageview just in case the file on disk # has changed. with open('static/index.html', 'r') as index: return index.read() routes = [ Route('/', 'GET', ui), ]