Spaces:
Sleeping
Sleeping
| from flask import Flask | |
| from flask_sqlalchemy import SQLAlchemy | |
| from flask_login import LoginManager | |
| from os import path | |
| import os | |
| from authlib.integrations.flask_client import OAuth | |
| import logging | |
| import jwt # Import PyJWT | |
| from datetime import datetime, timedelta | |
| from dotenv import load_dotenv | |
| db = SQLAlchemy() | |
| oauth = OAuth() | |
| # DB_NAME = "postoffice.db" | |
| load_dotenv() | |
| def create_app(): | |
| logging.basicConfig(level=logging.DEBUG) | |
| app = Flask(__name__, static_folder='static') | |
| # app.config['SECRET_KEY'] = 'vdjdvn224bc' | |
| app.config['SECRET_KEY'] = os.getenv('SECRET_KEY') | |
| # app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}' | |
| # app.config['JWT_SECRET_KEY'] = 'bfsfsjfbjh342' # Set your JWT secret key | |
| app.config['JWT_SECRET_KEY'] = os.getenv('JWT_SECRET_KEY') # Set your JWT secret key | |
| # app.config['SQLALCHEMY_DATABASE_URI'] = ('mysql+pymysql://root:%40Tomic123@localhost/postoffice') | |
| # app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://freedb_svaze:KC%2A4Mg%24GMZfu3W2@sql.freedb.tech/freedb_postoffice' | |
| app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://avnadmin:AVNS_fTFB8fx9pp0I4w18QMH@mysql-10993d7e-vazeswaroop-f46c.i.aivencloud.com:24112/defaultdb' | |
| # app.config['SQLALCHEMY_DATABASE_URI'] = (os.getenv('SQLALCHEMY_DATABASE_URI')) | |
| try: | |
| db.init_app(app) | |
| with app.app_context(): | |
| db.create_all() | |
| logging.info("Connected to Mysql") | |
| except Exception as e: | |
| logging.error("Connection Failed: ",e) | |
| # app.config['GOOGLE_CLIENT_ID'] = '6038375898-kb3j34c970gcn5ajbf85b9fnglu49er2.apps.googleusercontent.com' | |
| app.config['GOOGLE_CLIENT_ID'] = os.getenv('GOOGLE_CLIENT_ID') | |
| # app.config['GOOGLE_CLIENT_SECRET'] = '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' | |
| app.config['GOOGLE_CLIENT_SECRET'] = os.getenv('GOOGLE_CLIENT_SECRET') | |
| oauth.init_app(app) | |
| google = oauth.register( | |
| name='google', | |
| client_id=app.config['GOOGLE_CLIENT_ID'], | |
| client_secret=app.config['GOOGLE_CLIENT_SECRET'], | |
| access_token_url='https://accounts.google.com/o/oauth2/token', | |
| access_token_params=None, | |
| authorize_url='https://accounts.google.com/o/oauth2/auth', | |
| authorize_params=None, | |
| userinfo_endpoint='https://www.googleapis.com/oauth2/v1/userinfo', # For getting user info | |
| server_metadata_url='https://accounts.google.com/.well-known/openid-configuration', | |
| client_kwargs={'scope': 'openid profile email'}, | |
| redirect_uri='http://localhost:5000/google/callback', | |
| claims_options={ | |
| "iss": { | |
| # "essential": True, | |
| "values": ["https://accounts.google.com"] | |
| } | |
| } | |
| ) | |
| from .views import views | |
| from .auth import auth | |
| app.register_blueprint(views, url_prefix='/') | |
| app.register_blueprint(auth, url_prefix='/') | |
| # app.register_blueprint(auth, url_prefix='/newUser') | |
| # app.register_blueprint(auth, url_prefix='/register') | |
| # from .models import User, Pincodes, Offices, EF71929279 | |
| # create_db(app) | |
| # | |
| # print(f"Current working directory: {os.getcwd()}") | |
| login_manager = LoginManager() | |
| login_manager.login_view = 'auth.login' | |
| login_manager.init_app(app) | |
| def load_user(user_id): | |
| from .models import User # Import User model here to avoid circular imports | |
| return User.query.get(int(user_id)) | |
| return app | |
| # def create_db(app): | |
| # pat = f'instance/{DB_NAME}' | |
| # if not path.exists(pat): # Ensure the database file is checked in the current directory | |
| # with app.app_context(): | |
| # db.create_all() # Create the database tables | |
| # print("Database created.") | |
| # else: | |
| # print("Database already exists.") |