-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helo-server.py
84 lines (70 loc) · 2.18 KB
/
helo-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
# HeLO.py
import json
import logging
import os
from flask import Flask, render_template, jsonify
from flask_bcrypt import Bcrypt
from flask_jwt_extended import JWTManager
from flask_restful import Api
from database._db import initialize_db
from discord.auth import initialize_discord_auth
from rest._routes import initialize_routes
# init components
logging.basicConfig(
encoding="utf-8",
level=logging.INFO,
format=f"%(filename)20s:%(lineno)-3s - %(funcName)-30s %(message)s",
)
app = Flask(__name__)
DB = {
"USERNAME": os.environ.get("DB_USERNAME"),
"PASSWORD": os.environ.get("DB_PASSWORD"),
"HOST": os.environ.get("DB_HOST"),
"NAME_PC": os.environ.get("DB_NAME_PC"),
"NAME_CONSOLE": os.environ.get("DB_NAME_CONSOLE"),
}
if DB["HOST"] == "localhost":
app.config["MONGODB_HOST"] = (
"mongodb://%(USERNAME)s:%(PASSWORD)s@%(HOST)s/%(NAME_PC)s?authSource=admin" % DB
)
else:
app.config["MONGODB_SETTINGS"] = [
{
"host": "mongodb+srv://%(USERNAME)s:%(PASSWORD)s@%(HOST)s/%(NAME_PC)s" % DB,
"alias": "default",
},
{
"host": "mongodb+srv://%(USERNAME)s:%(PASSWORD)s@%(HOST)s/%(NAME_CONSOLE)s"
% DB,
"alias": "console",
},
]
app.config["JWT_SECRET_KEY"] = os.environ.get("JWT_SECRET_KEY")
app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY")
app.config["DISCORD_REPORT_MATCH_WEBHOOK"] = os.environ.get(
"DISCORD_REPORT_MATCH_WEBHOOK"
)
app.config["IS_CONSOLE_API"] = os.environ.get("IS_CONSOLE_API") == "true"
# needs to be true for custom error messagess
app.config["PROPAGATE_EXCEPTIONS"] = True
initialize_db(app)
discord = initialize_discord_auth(app)
initialize_routes(Api(app), discord)
bcrypt = Bcrypt(app)
jwt = JWTManager(app)
# custom error message for wrong JWTs
@jwt.invalid_token_loader
def invalid_token_callback(s):
return jsonify("Wrong Token or no Admin"), 401
# home page: offer some explanation how to use the API
@app.route("/")
def home():
return (
render_template(
"home.html", apis=json.loads(open("static/apis.json", "r").read())
),
200,
)
# start app
if __name__ == "__main__":
app.run()