wowlet-backend/fapi/factory.py

81 lines
3.2 KiB
Python
Raw Normal View History

2020-10-03 03:45:45 +01:00
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2020, The Monero Project.
# Copyright (c) 2020, dsc@xmr.pm
import json
import asyncio
from quart import Quart
from quart_session import Session
import aioredis
import settings
app = None
cache = None
connected_websockets = set()
api_data = {}
user_agents = None
txfiatdb = None
print("""\033[91m
\033[0m
""".strip())
async def _setup_cache(app: Quart):
global cache
data = {
2020-11-14 18:35:31 +00:00
"address": settings.redis_address
2020-10-03 03:45:45 +01:00
}
if settings.redis_password:
data['password'] = settings.redis_password
cache = await aioredis.create_redis_pool(**data)
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_REDIS'] = cache
Session(app)
def create_app():
global app
app = Quart(__name__)
@app.before_serving
async def startup():
2020-11-21 08:52:31 +00:00
global txfiatdb, user_agents
2020-10-03 03:45:45 +01:00
await _setup_cache(app)
loop = asyncio.get_event_loop()
2020-11-21 08:52:31 +00:00
with open('data/nodes.json', 'r') as f:
nodes = json.loads(f.read())
cache.execute('JSON.SET', 'nodes', '.', json.dumps(nodes))
2020-10-03 03:45:45 +01:00
2020-11-21 08:52:31 +00:00
with open('data/user_agents.txt', 'r') as f:
user_agents = [l.strip() for l in f.readlines() if l.strip()]
2020-10-03 03:45:45 +01:00
from fapi.fapi import FeatherApi
from fapi.utils import loopyloop, TxFiatDb, XmrRig
2020-10-03 03:45:45 +01:00
txfiatdb = TxFiatDb(settings.crypto_name, settings.crypto_block_date_start)
loop.create_task(loopyloop(20, FeatherApi.xmrto_rates, FeatherApi.after_xmrto))
loop.create_task(loopyloop(120, FeatherApi.crypto_rates, FeatherApi.after_crypto))
loop.create_task(loopyloop(600, FeatherApi.fiat_rates, FeatherApi.after_fiat))
loop.create_task(loopyloop(300, FeatherApi.ccs, FeatherApi.after_ccs))
loop.create_task(loopyloop(900, FeatherApi.reddit, FeatherApi.after_reddit))
loop.create_task(loopyloop(60, FeatherApi.blockheight, FeatherApi.after_blockheight))
loop.create_task(loopyloop(60, FeatherApi.check_nodes, FeatherApi.after_check_nodes))
loop.create_task(loopyloop(43200, txfiatdb.update))
loop.create_task(loopyloop(43200, XmrRig.releases, XmrRig.after_releases))
2020-10-03 03:45:45 +01:00
import fapi.routes
return app