async def app_factory() -> web.Application: package_root = Path(__file__).parent website = web.Application() website.update( # pylint: disable=no-member client_session=None, # populated via parent app signal redis=None, # populated via parent app signal scheduler=None, # populated via parent app signal slack_invite_token=settings.SLACK_INVITE_TOKEN, slack_token=settings.SLACK_TOKEN, ) # aiohttp_jinja2 requires this values to be set. Sadly it does not work with subapplication. # We need to use request.app.router["static"].url_for(filename='images/pyslackers_small.svg') # in templates to get the correct url for static assets website["static_root_url"] = "DO-NOT-USE" jinja2_setup( website, context_processors=[request_processor], loader=FileSystemLoader(str(package_root / "templates")), filters={ "formatted_number": formatted_number, **FILTERS }, ) website.cleanup_ctx.extend([slack_client, background_jobs]) website.add_routes(routes) website.router.add_static("/static", package_root / "static", name="static") return website
def main(): template_root = join(dirname(realpath(__file__)), 'templates') DB_CONNECTION.connect() DB_CONNECTION.create_tables((Record, ), safe=True) Record.delete().execute() Record.insert_many(({} for _ in range(500))).execute() DB_CONNECTION.close() DB_CONNECTION.set_allow_sync(False) try: application = Application() jinja2_setup(application, loader=FileSystemLoader(template_root)) application.router.add_get('/', raw_response_handle) application.router.add_get('/template', html_response_handle) application.router.add_get('/database', database_response_handle) application.router.add_get('/service', service_response_handle) run_app(application) except Exception as exception: # TODO: error notification logic raise exception finally: DB_CONNECTION.close()
def app_factory() -> web.Application: app = web.Application() jinja2_setup(app, loader=jinja2.FileSystemLoader( [Path(__file__).parent / "templates"])) session_setup(app, SimpleCookieStorage()) app.add_subapp( "/auth/dataporten/", dataporten( FEIDE_CLIENT_ID, FEIDE_CLIENT_SECRET, on_login=on_dataporten_login, scopes=[ 'profile', 'userid', 'openid', 'groups', 'peoplesearch', 'email', 'userid-feide' ], json_data=False, ), ) app.add_routes([web.get("/", index), web.get("/auth/logout", logout)]) return app
def start(): app = Application() jinja2_setup(app, loader=FileSystemLoader(TEMPLATES_DIR)) routes_setup(app) app['ws_holder'] = WebsocketsHolder() run_app(app)
def register_extensions(app: web.Application) -> None: """ Register extensions on app """ jinja2_setup(app, loader=FileSystemLoader(CFG.TEMPLATE_FOLDER)) app['static_root_url'] = '/static' app.router.add_static('/static/', path=CFG.STATIC_FOLDER, name='static') return None
def init(loop): app = Application(loop=loop) jinja2_setup(app, loader=FileSystemLoader('templates')) app.router.add_route('GET', '/name/{name}', get_name) app.router.add_route('POST', '/post', post_name) host = '0.0.0.0' port = environ.get('PORT', 8000) srv = yield from loop.create_server(app.make_handler(), host, port) print('Server started at http://{0}:{1}'.format(host, port)) return srv
def app_factory() -> web.Application: app = web.Application() jinja2_setup( app, loader=jinja2.FileSystemLoader([Path(__file__).parent / "templates"]) ) session_setup(app, SimpleCookieStorage()) app.add_subapp( "/auth/github/", github( "a1b8c7904865ac38baba", "147d82d8ded7a74899fcc4da2b61f8d305bf81c5", on_login=on_github_login, ), ) app.add_routes([web.get("/", index), web.get("/auth/logout", logout)]) return app
def __init__(self, paths: dict): # Rutas de la aplicación self.paths = paths self.db_config = dict() with open(self.paths['config'].joinpath('db.json')) as db_cf: self.db_config = json.load(db_cf) self._router = Router() # Rutas a archivos estáticos self._router.add_static('/static', self.paths['static']) # Fernet key para encriptar las cookies de las sesiones self.fernet_key = fernet.Fernet.generate_key() self.secret_key = urlsafe_b64decode(self.fernet_key) # Interfaz de la base de datos self.db = Connection(self.db_config['host'], self.db_config['port'], self.db_config['user'], self.db_config['password'], self.db_config['database'], self.db_config['schema']) self.mvc = Mvc(self, self._router, self.db, path=self.paths['app']) super().__init__(router=self._router) # Inicializar el gestor de templates Jinja2 jinja2_setup( self, loader=FileSystemLoader( str(self.paths['resources'].joinpath('views').resolve()))) # Inicializar sesiones session_setup( self, EncryptedCookieStorage(self.secret_key, cookie_name='CAJAMARQUESO_SRL'))
def app_factory(client_id: str, client_secret: str) -> web.Application: fernet_key = cryptography.fernet.Fernet.generate_key() secret_key = base64.urlsafe_b64decode(fernet_key) app = web.Application() # session_setup(app, SimpleCookieStorage()) # used for testing purpose session_setup(app, EncryptedCookieStorage(secret_key)) jinja2_setup(app, loader=jinja2.FileSystemLoader( [Path(__file__).parent / "templates"])) app.add_subapp( "/google/", oauth2_app( # ..., client_id=client_id, client_secret=client_secret, authorize_url="https://accounts.google.com/o/oauth2/v2/auth", token_url="https://www.googleapis.com/oauth2/v4/token", on_login=on_google_login, on_error=on_google_error, scopes=['email', 'profile', 'openid'])) app.add_routes([web.get("/", index), web.get("/auth/logout", logout)]) return app
def setup_jinja2(app): jinja2_setup(app, loader=jinja2.FileSystemLoader(TEMPLATES_PATH))
from aiohttp_jinja2 import template from dotenv import load_dotenv from emoji import demojize, emojize from humanize import naturaltime from jinja2 import FileSystemLoader from admin import AdminApp, AdminRoutes from assets import CustomApp, Database from files import FileRoutes from middlewares import Middlewares # Create app app = CustomApp(middlewares=Middlewares) routes = web.RouteTableDef() app.router.add_static("/static", "./static") jinja2_setup(app, loader=FileSystemLoader("./templates")) load_dotenv() # Parse .env config app.args = dict( host=environ.get("HOST", None), port=int(environ.get("PORT", 80)), printmessages=True if environ.get("PRINT_MESSAGES", "True").upper() == "TRUE" else False, databaseurl=environ["DATABASE_URL"], maxcachemessagelegth=int(environ.get("MAX_CACHE_MESSAGE_LENGTH", 0)), logpings=True if environ.get("LOG_PINGS", "False").upper() == "TRUE" else False, development=True if environ.get("DEVELOPMENT", "False").upper() == "TRUE" else False,