-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.py
188 lines (137 loc) · 5.98 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Quart's flask-patch (intended for extensions) adds a loop.sync_wait function used in QuartDecoratorProvider.
import quart.flask_patch
from quart import Quart, jsonify, send_from_directory, request
# Hypercorn (Quart ASGI webserver)
from hypercorn.config import Config
from hypercorn.asyncio import serve
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from routes import Routes
from providers import LoggingProvider
from providers.MetricsProvider import MetricsProvider
from providers.XblDecoratorProvider import XblDecorator
from providers.caching.DiskCacheProvider import DiskCacheProvider
# from flask_cors import CORS
import asyncio
import subprocess
import os
import logging
import main
logger = LoggingProvider.getLogger("server")
loop = asyncio.get_event_loop()
logger.info("Using loop: %s" % str(loop))
app = Quart(__name__, static_folder=None)
xbl_client, session = loop.run_until_complete(main.authenticate(loop))
# Get a MetricsProvider
metrics = MetricsProvider(app)
# Get a cacheprovider
cache = DiskCacheProvider("/tmp/xbl-web-api")
# Setup & start the scheduler
scheduler = AsyncIOScheduler(event_loop=loop)
scheduler.start()
# Define scheduled tasks
sched_logger = logging.getLogger("sched.timed_reauth")
@scheduler.scheduled_job("interval", minutes=1)
async def job_timed_reauth():
is_same_loop = asyncio.get_running_loop() is loop
if is_same_loop:
sched_logger.debug("In the same event loop! Refreshing tokens if needed...")
issueTimeBeforeRefresh = xbl_client._auth_mgr.user_token.issue_instant
# This async function will refresh the tokens only *if* they are unavailable or expired
await xbl_client._auth_mgr.refresh_tokens()
if xbl_client._auth_mgr.user_token.issue_instant != issueTimeBeforeRefresh:
sched_logger.debug("Tokens have been refreshed!")
# Token has changed, save to disk
with open(os.getenv("XBL_TOKENS_PATH"), mode="w") as f:
f.write(xbl_client._auth_mgr.oauth.json())
sched_logger.info("Saved refreshed tokens to disk!")
else:
sched_logger.debug("Tokens have NOT been refreshed")
else:
sched_logger.error("Not in the same event loop!")
raise RuntimeError("Timed Reauth not in same loop!")
@scheduler.scheduled_job("interval", minutes=5)
async def job_cache_cleanup():
# This job will remove expired items from the cache
number_removed = cache.remove_expired()
# Get a logger and print details on how much we cleared
logger = logging.getLogger("sched.cache_cleanup")
logger.info("Removed %i expired items from cache." % number_removed)
logger.info("New cache size is %i items." % cache.len())
# If we removed anything, update the cache size metric
if number_removed != 0:
metrics.cache_size_gauge.set({}, cache.len())
# Get the short SHA and return as string
def get_sha():
if "GIT_COMMIT" in os.environ:
return os.getenv("GIT_COMMIT")[0:7]
else:
return str(
subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).strip()
).split("'")[1::2][0]
def get_routes():
routes = []
for rule in app.url_map.iter_rules():
routes.append("%s" % rule)
return routes
# Import routes from routes/ directory
Routes(app, loop, xbl_client, cache, metrics)
# Define routes for the homepage
@app.route("/")
def index():
return send_from_directory("static", "index.html")
@app.route("/readme")
async def readme():
res = await send_from_directory("./", "README.md")
# Windows sets the wrong mimetype
res.mimetype = "text/markdown"
return res
@app.route("/info")
def info():
return jsonify({"sha": get_sha(), "routes": get_routes()})
# Define routes that don't fit into any other categories
# Create a XblDecorator instance
r = XblDecorator(app, loop, cache, metrics)
@r.openXboxRoute("/titleinfo/<int:titleid>", r.cache.constants.SECONDS_ONE_DAY)
async def titleinfo(titleid):
return await xbl_client.titlehub.get_title_info(titleid)
# legacysearch (EDS) has been removed from xbox-webapi v2
# It may be possible to re-implement at a later date
@r.cachedRoute("/legacysearch/<query>", r.cache.constants.SECONDS_THIRTY_DAYS)
async def search360(query):
res = jsonify({"error": "legacysearch not currently available", "code": 410})
res.status_code = 410 # 410 (Gone)
return res
@r.cachedRoute("/gamertag/check/<gamertag>", r.cache.constants.SECONDS_ONE_DAY)
async def gamertagcheck(gamertag):
# Use .value to get the int instead of the enum
code = (await xbl_client.account.claim_gamertag(1, gamertag)).value
return jsonify(
{"available": "true" if code == 200 else "false" if code == 409 else "unknown"}
)
# Create hypercorn config object
port = int(os.getenv("PORT", 3000))
config = Config()
config.bind = ["0.0.0.0:%i" % port]
# Save the port so we can access it later
# Making loopback requests is a *bad* idea but we do get cached results this way!
xbl_client._xbl_web_api_current_port = port
# Run hypercorn in the same loop as xbl_client
loop.run_until_complete(serve(app, config))
# When we get here, hypercorn has finished so we can just close the ClientSession
logger.info("Serve future done! Closing session...")
loop.run_until_complete(session.close())
# Next up, let's gracefully stop the cache provider
logger.info("Shutting down cache provider... (had %i items)" % cache.len())
cache.shutdown()
logger.info("Waiting for metrics thread to shut down...")
# Let's set the shutdown event for the metrics web server thread...
# Setting the event directly doesn't work on Linux (not thread-safe?)
# metrics.metrics_app.shutdown_event.set()
# Instead we will set the event using the loop for the metrics app.
# see: https://stackoverflow.com/a/48839244
metrics.metrics_app.loop.call_soon_threadsafe(metrics.metrics_app.shutdown_event.set)
while metrics.metrics_app.thread.is_alive():
continue # Wait until shutdown
logger.info("Shut down metrics thread!")
# Cleanup done!
logger.info("Cleanup done!")