def main(): """启动 sanic server """ list(map(lambda u: app.add_route(*u), urls)) app.static('/static', './static') app.centrifugo = AioClient(app.config.CENTRIFUGO_URL, app.config.CENTRIFUGO_SECRET) app.run(host=app.config.HOST, port=app.config.PORT, debug=app.config.DEBUG, workers=app.config.WORKERS)
def start_up(self): @app.listener('before_server_start') async def instantiate_scheduler(app, loop): self.sched = AsyncIOScheduler(timezone='UTC') self.sched.add_job(self.init_data, misfire_grace_time=86300) self.sched.add_job(self.data.token_update, 'interval', hours=1) self.sched.add_job(self.data.get_seasonal_eververse, 'cron', day_of_week='tue', hour='17', minute='1', second='40', misfire_grace_time=86300) self.sched.add_job(self.data.get_weekly_eververse, 'cron', day_of_week='tue', hour='17', minute='1', second='40', misfire_grace_time=86300) # self.sched.add_job(self.data.get_weekly_rotations, 'cron', day_of_week='tue', hour='17', minute='0', # second='40', misfire_grace_time=86300) # self.sched.add_job(self.data.get_daily_rotations, 'cron', hour='17', minute='0', second='40', # misfire_grace_time=86300) self.sched.start() app.static('/static', './static') app.error_handler = CustomErrorHandler() # app.url_for('static', filename='style.css', name='style') if self.args.production: app.run(host='0.0.0.0', port=1423, workers=1, debug=False, access_log=False ) # ssl={'cert': self.args.cert, 'key': self.args.key}) else: app.run()
js_head_end='<script defer>window.setTimeout(function(){ window.location = "admin"; },3000);' '</script>') @auth.login_required async def logout(request): page = dict() auth.logout_user(request) page['title'] = 'Logging Out' page['header'] = 'You have been successfully logged out' page['text'] = 'Redirecting in 3 seconds...' return jrender('page.html', request, page=page, js_head_end='<script defer>window.setTimeout(function(){ window.location = "/"; },3000);</script>') async def redirect_index(request): return redirect('/') # Static Files app.static('images/', './app/static/images/') app.static('css/', './app/static/css/') # Routes app.add_route(setup, 'setup', methods=['GET', 'POST']) app.add_route(index, '/') app.add_route(post, '/<name>') app.add_route(dashboard, 'admin') app.add_route(login, 'login', methods=['GET', 'POST']) app.add_route(logout, 'logout') app.add_route(redirect_index, '/index.html')
from jinja2_sanic import template, render_template, setup from .lib import * from .lib.database import DbTable, table_enum from app import ( app, config, dbase, PokerGame, PokerGameDatabase, DatabaseBrowser ) import asyncio from sanic.log import logger app.static('/static', 'app/static') css_files = ['main', 'base', 'navbar', 'forms', 'database', 'pokertables', 'pokertable'] getHeaders = { 'css_urls': [app.url_for('static', filename=f'css/{file}.css') for file in css_files], 'favicon_url': app.url_for('static', filename='favicon.ico') } setup(app, loader=DictLoader({ "template_base": open('app/templates/base.html').read(), "template_signin": open('app/templates/signin.html').read(), "template_signup": open('app/templates/signup.html').read(), "template_database": open('app/templates/database.html').read(), "template_table": open('app/templates/table.html').read(), "template_pokertables": open('app/templates/pokertables.html').read() }))
import ujson import hashlib, binascii import discord from functools import wraps import time ################ ## app config ## ################ redis = Redis() app.config['SECRET_KEY'] = 'top secret !!!' env = Environment(loader=PackageLoader('app', 'templates')) session_interface = MyRedisInterface(redis.get_redis_pool, expiry=604800) app.static('/static', './app/static') app.config.AUTH_LOGIN_ENDPOINT = 'login' show_deploy = False with open('./app/config/config.json') as f: CONFIG = ujson.loads(f.read()) def template(tpl, *args, **kwargs): template = env.get_template(tpl) request = get_stack_variable('request') user = None if request['session'].get('logged_in'): user = request['session']['user'] kwargs['request'] = request
app.redis_pool = await aioredis.create_redis_pool( (app.config.REDIS_HOST, app.config.REDIS_PORT), minsize=5, maxsize=10, loop=loop, ) @app.listener('after_server_stop') async def after_server_stop(app, loop): app.redis_pool.close() await app.redis_pool.wait_closed() def get_app(): """ for using unittest :return: app """ routes.load_router() return app if __name__ == "__main__": routes.load_router() app.static('/', './') app.run(host="0.0.0.0", port=2080, debug=app.config.DEBUG, workers=int(app.config.WORKERS))
from sanic.response import json from app import app import os STATIC_FOLDER = os.path.join(os.path.dirname(__file__), 'static\\') app.static('/static', STATIC_FOLDER) app.static('/favicon.ico', os.path.join(STATIC_FOLDER, 'img', 'favicon.ico')) print(STATIC_FOLDER) @app.route("/scraper/api/v1.0/data/twitter") async def twiter(request): try: topic = request.args['topic'][0] min_number = int(request.args['min'][0]) except: return json({'response': False, 'message': 'arguments are missing', 'tweets': []}) if topic is None or topic is '' or min_number is None or min_number is '': return json({'response': False, 'message': 'arguments have wrong format', 'tweets': []}) from app.fun import parse_tweets return json({'response': True, 'message': 'done', 'tweets': parse_tweets(topic, min_number)}) @app.route('/scraper/api/v1.0/data/trends', methods=['GET']) async def google_trends(request): if request.method == 'GET': topic = request.args.get('topic').split(',') lang = request.args.get('lang')