# coding=utf-8 from flask import Flask, jsonify from flask_bcrypt import Bcrypt from flask_cors import CORS from flask_marshmallow import Marshmallow import os from flask_sqlalchemy import SQLAlchemy from flask_session import Session from flask_login import LoginManager from sqlalchemy.orm import sessionmaker from werkzeug.routing import BaseConverter import stripe try: from local_settings import ApplicationConfig except Exception as e: from settings import ApplicationConfig app = Flask(__name__, static_url_path='', static_folder='static', template_folder='templates') app.config.from_object(ApplicationConfig) session = sessionmaker() check_enviroment = ApplicationConfig.CURRENT_SETTINGS print(f"starting server with {check_enviroment} settings") class RegexConverter(BaseConverter): def __init__(self, url_map, *items): super(RegexConverter, self).__init__(url_map) self.regex = items[0] app.url_map.converters['regex'] = RegexConverter app.jinja_env.autoescape = True # configure stripe # stripe_keys = { # 'secret_key': os.environ['STRIPE_SECRET_KEY'], # 'publishable_key': os.environ['STRIPE_PUBLISHABLE_KEY'], # } stripe_keys = { 'secret_key': 'sk_test_51OUbSMJznCGgUo9kWM2Uv0UjM0Ai6etCOOHVKkgFBVxO66VtIqlOFL6lpWcEA7zgVFICrdQSjSRVQH58NRlYeIpC00T5Jvw9wQ', 'public_key': ' pk_test_51OUbSMJznCGgUo9krwqaJkCtdnROJ2gyTcUWQGOHcaREDqP8dPGhMmLTbI1sFiyiKiK3BOPasTayBnFFth0pb81g00qlPdABbC', } stripe.api_key = stripe_keys['secret_key'] app.config['SECRET_KEY'] = ApplicationConfig.SECRET_KEY app.config['SESSION_TYPE'] = ApplicationConfig.SESSION_TYPE app.config['SESSION_COOKIE_NAME'] = ApplicationConfig.SESSION_COOKIE_NAME app.config['SESSION_COOKIE_SECURE'] = ApplicationConfig.SESSION_COOKIE_SECURE app.config['SESSION_COOKIE_HTTPONLY'] = ApplicationConfig.SESSION_COOKIE_HTTPONLY app.config['SESSION_COOKIE_SAMESITE'] = ApplicationConfig.SESSION_COOKIE_SAMESITE app.config['SESSION_PERMANENT'] = ApplicationConfig.SESSION_PERMANENT app.config['SESSION_USE_SIGNER'] = ApplicationConfig.SESSION_USE_SIGNER app.config['SESSION_REDIS'] = ApplicationConfig.SESSION_REDIS session.configure(bind=ApplicationConfig.SQLALCHEMY_DATABASE_URI) db = SQLAlchemy(app) bcrypt = Bcrypt(app) server_session = Session(app) ma = Marshmallow(app) login_manager = LoginManager(app) login_manager.session_protection = 'strong' login_manager.anonymous_user = "Guest" @login_manager.request_loader def load_user_from_request(request): from app.classes.auth import Auth_User # first, try to log in using the api_key url arg api_key = request.args.get('api_key') if api_key: user = db.session\ .query(Auth_User)\ .filter_by(api_key=api_key)\ .first() if user: return user # next, try to log in using Basic Auth api_key_auth = request.headers.get('Authorization') if api_key_auth: api_key = api_key_auth.replace('bearer ', '', 1) if api_key.startswith('"') and api_key.endswith('"'): api_key = api_key[1:-1] user = db.session\ .query(Auth_User)\ .filter_by(api_key=api_key)\ .first() if user: return user return None api_main = { "origins": [ApplicationConfig.ORIGIN_URL], "methods": ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"], "allow_headers": ['Authorization', 'application/json', 'authorization', 'Content-Type', 'Access-Control-Allow-Headers', 'Origin,Accept', 'X-Requested-With', 'Content-Type', 'Access-Control-Request-Method', 'Access-Control-Request-Headers'] } cors = CORS(app, supports_credentials=True, resources={r'/*': api_main}) # bind a function after each request, even if an exception is encountered. @app.teardown_request def teardown_request(error): db.session.remove() @app.teardown_appcontext def teardown_appcontext(error): db.session.remove() @app.errorhandler(500) def internal_error500(): return jsonify({"error": "Internal Error 500"}), 500 @app.errorhandler(502) def internal_error502(): return jsonify({"error": "Internal Error 502"}), 502 @app.errorhandler(404) def internal_error404(): return jsonify({"error": "Internal Error 400"}), 400 @app.errorhandler(401) def internal_error404(): return jsonify({"error": "Internal Error 401"}), 401 @app.errorhandler(400) def internal_error400(): return jsonify({"error": "Internal Error 400"}), 400 @app.errorhandler(413) def to_large_file(): return jsonify({"error": "File is too large. Use a smaller image/file."}), 413 @app.errorhandler(403) def internal_error403(): return jsonify({"error": "Internal Error 403"}), 403 @app.errorhandler(405) def internal_error(): return jsonify({"error": "Internal Error 405"}), 405 # link locations from .main import main as main_blueprint app.register_blueprint(main_blueprint, url_prefix='/main') from .pay import pay as pay_blueprint app.register_blueprint(pay_blueprint, url_prefix='/pay') from .update import update as update_blueprint app.register_blueprint(update_blueprint, url_prefix='/update') from .auth import auth as auth_blueprint app.register_blueprint(auth_blueprint, url_prefix='/auth') with app.app_context(): db.configure_mappers() db.create_all() db.session.commit()