-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
70 lines (53 loc) · 2.24 KB
/
main.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
import datetime
import mimetypes
import os
import random
import string
from dotenv import load_dotenv
from quart import Quart, jsonify, make_response, render_template, request
from quart_rate_limiter import RateLimiter, limit_blueprint
from api.main import app as api_app
from captcha.main import app as captcha_app
from dashboard.main import app as dashboard_app
from general.main import app as general_app
mimetypes.add_type("image/webp", ".webp")
load_dotenv()
def make_random_str(length):
return "".join(random.choices(string.ascii_letters, k=length))
app = Quart(__name__, static_folder="./general/static")
limiter = RateLimiter(app)
app.config["SERVER_NAME"] = "sevenbot.jp" if os.getenv("heroku") else "localhost:5000"
app.secret_key = make_random_str(10)
app.register_blueprint(general_app)
app.register_blueprint(general_app, subdomain="www", name="www_general")
app.register_blueprint(api_app, subdomain="api")
app.register_blueprint(captcha_app, subdomain="captcha")
app.register_blueprint(dashboard_app, subdomain="dashboard")
limit_blueprint(api_app, 1, datetime.timedelta(seconds=5))
@app.errorhandler(404)
async def notfound(ex):
if request.host.startswith("api."):
return await make_response(
jsonify({"message": "Unknown endpoint. Make sure your url is correct.", "code": "not_found"}), 404
)
elif request.host.startswith("captcha."):
return await make_response(await render_template("captcha/404.html"), 404)
elif request.host.startswith("dashboard."):
return await make_response(await render_template("dashboard/404.html"), 404)
else:
return await make_response(await render_template("general/404.html"), 404)
@app.errorhandler(405)
async def methodnotallowed(_):
return await make_response(
jsonify(
{"message": "Invalid request type. Make sure your request type is correct.", "code": "wrong_request_type"}
),
405,
)
@app.errorhandler(429)
async def ratelimit_handler(e):
return await make_response(
jsonify({"error_description": "You are being rate limited.", "ratelimit": e.description}), 429
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=(5000 or os.getenv("PORT")), debug=not bool(os.getenv("heroku")))