nova-web/web/app.py

103 lines
2.9 KiB
Python
Raw Normal View History

2023-07-25 03:38:53 +02:00
import os
import json
import flask
import random
import secrets
import logging
from dotenv import load_dotenv
from werkzeug.middleware.proxy_fix import ProxyFix
load_dotenv()
log = logging.getLogger('werkzeug')
log.disabled = True
def create_app() -> flask.Flask:
app = flask.Flask(__name__)
app.wsgi_app = ProxyFix(
app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1
)
@app.context_processor
def inject_variables():
return {
'contact_email': os.getenv('CONTACT_EMAIL')
}
@app.route('/')
def index():
return flask.render_template('index.html')
@app.route('/alt-design')
def alt_design():
return flask.render_template('alt-design.html')
2023-08-20 13:41:24 +02:00
@app.route('/go')
def go():
2023-07-25 03:38:53 +02:00
return flask.render_template('panel.html')
@app.route('/novacord')
2023-08-20 13:41:24 +02:00
def novacord_page():
2023-07-25 03:38:53 +02:00
return flask.render_template('novacord.html')
2023-08-20 13:41:24 +02:00
@app.route('/replit')
def replit_page():
return flask.render_template('replit.html')
2023-07-25 03:38:53 +02:00
@app.route('/favicon.ico')
def favicon():
return flask.send_file('static/img/fav.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/legal/<path:subpath>')
def legal_site(subpath):
emoji = None
if subpath == 'terms':
emoji = json.load(open('data/tos.json', encoding='utf8')).get(flask.request.args.get('verify'))
2023-08-06 21:25:29 +02:00
if emoji:
emoji = emoji.encode('utf8')
2023-07-25 03:38:53 +02:00
return flask.render_template(f'legal/{subpath}.html', verify=emoji)
@app.route('/api/tos-verification', methods=['POST'])
def tos_verification_api():
if not flask.request.headers.get('Authorization') == os.getenv('TOS_VERIFICATION_KEY'):
return 'Unauthorized', 401
code = secrets.token_urlsafe(6)
emoji = random.choice(open('config/emojis.txt', encoding='utf8').read())
if not os.path.exists('data/tos.json'):
open('data/tos.json', 'w', encoding='utf8').write('{}')
2023-07-25 12:47:26 +02:00
try:
data = json.load(open('data/tos.json', encoding='utf8'))
except json.decoder.JSONDecodeError:
data = {}
2023-07-25 03:38:53 +02:00
data[code] = emoji
json.dump(data, open('data/tos.json', 'w', encoding='utf8'))
return {
'code': code,
'emoji': emoji
}
@app.route('/api/tos-verification/<code>', methods=['DELETE'])
def tos_verification_api_delete(code):
if not flask.request.headers.get('Authorization') == os.getenv('TOS_VERIFICATION_KEY'):
return 'Unauthorized', 401
data = json.load(open('data/tos.json', encoding='utf8'))
del data[code]
2023-08-01 01:21:59 +02:00
json.dump(data, open('data/tos.json', 'w', encoding='utf8'))
2023-07-25 03:38:53 +02:00
return 'Removed.', 204
return app
production = create_app()
if __name__ == '__main__':
create_app().run(debug=True, use_evalex=False, port=2323, host='0.0.0.0', threaded=True)