75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
from flask import Flask, request, jsonify, redirect, url_for
|
||
from flask_cors import CORS
|
||
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
|
||
from db import init_db, get_db
|
||
from routes.api import api
|
||
from routes.dashboard import dashboard
|
||
from routes.auth import auth
|
||
import logging
|
||
|
||
logging.basicConfig(
|
||
level=logging.WARNING,
|
||
format='%(asctime)s [%(name)s] %(levelname)s: %(message)s'
|
||
)
|
||
|
||
# Initialise DB schema when the app starts
|
||
init_db()
|
||
|
||
app = Flask(__name__)
|
||
app.secret_key = 'grc-super-secret-key-change-in-production'
|
||
|
||
# ----------------------------------------------------------------
|
||
# Flask-Login setup
|
||
# ----------------------------------------------------------------
|
||
login_manager = LoginManager()
|
||
login_manager.init_app(app)
|
||
login_manager.login_view = 'auth.login'
|
||
login_manager.login_message = 'Παρακαλώ συνδεθείτε για να συνεχίσετε.'
|
||
|
||
class User(UserMixin):
|
||
def __init__(self, id, username, clan_id):
|
||
self.id = id
|
||
self.username = username
|
||
self.clan_id = clan_id
|
||
|
||
@login_manager.user_loader
|
||
def load_user(user_id):
|
||
conn = get_db()
|
||
row = conn.execute('SELECT id, username, clan_id FROM users WHERE id = ?', (user_id,)).fetchone()
|
||
conn.close()
|
||
if row:
|
||
return User(row['id'], row['username'], row['clan_id'])
|
||
return None
|
||
|
||
# Make current_user available in all templates
|
||
@app.context_processor
|
||
def inject_user():
|
||
return dict(current_user=current_user)
|
||
|
||
# ----------------------------------------------------------------
|
||
# CORS
|
||
# ----------------------------------------------------------------
|
||
CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=False)
|
||
|
||
@app.after_request
|
||
def add_cors_headers(response):
|
||
response.headers['Access-Control-Allow-Origin'] = '*'
|
||
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, DELETE, OPTIONS'
|
||
response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization, X-Clan-Key'
|
||
return response
|
||
|
||
@app.route('/', defaults={'path': ''}, methods=['OPTIONS'])
|
||
@app.route('/<path:path>', methods=['OPTIONS'])
|
||
def handle_options(path):
|
||
return jsonify({}), 200
|
||
|
||
# ----------------------------------------------------------------
|
||
# Blueprints
|
||
# ----------------------------------------------------------------
|
||
app.register_blueprint(api)
|
||
app.register_blueprint(dashboard)
|
||
app.register_blueprint(auth)
|
||
|
||
if __name__ == '__main__':
|
||
print("✅ Grepolis Remote — DB initialised")
|
||
app.run(host='0.0.0.0', port=5050, debug=True) |