Event-CRUD-Flask-python3-API/app.py

82 lines
2.8 KiB
Python
Raw Normal View History

from flask import Flask
from models import db
from flask_migrate import Migrate
2024-01-07 11:28:49 +00:00
from routes.userRoutes import userRoutes
from routes.eventRoutes import eventRoutes
import config
2024-01-07 11:28:49 +00:00
from flask_jwt_extended import JWTManager
2024-01-07 14:32:35 +00:00
from middlewares.errorHandlers import handle_auth_error, handle_invalid_token
2024-01-07 14:21:35 +00:00
from flask_jwt_extended.exceptions import NoAuthorizationError
from jwt.exceptions import InvalidTokenError
2024-01-08 10:19:55 +00:00
from datetime import timedelta
2024-01-08 20:03:51 +00:00
from flask_apscheduler import APScheduler
2024-01-09 08:23:57 +00:00
from services.EventNotifyerService import EventNotifyerService
2024-01-07 11:28:49 +00:00
class App:
def __init__(self):
self.app = Flask(__name__)
self.set_config()
self.set_up_db()
self.set_up_jwt()
self.register_blueprints()
2024-01-07 14:21:35 +00:00
self.setup_error_handlers()
2024-01-08 20:03:51 +00:00
self.scheduler = APScheduler()
def setup_scheduler(self):
self.app.config.from_object(self.SchadulerConfig())
self.scheduler.init_app(self.app)
self.scheduler.start()
# Schedule the job
self.scheduler.add_job(id='locate_upcoming_events', func=self.locate_upcoming_events, trigger='interval', seconds=1)
2024-01-07 11:28:49 +00:00
def set_config(self):
self.app.config.from_object(config.Config)
def set_up_db(self):
db.init_app(self.app)
self.migrate = Migrate(self.app, db)
2024-01-07 14:21:35 +00:00
def setup_error_handlers(self):
self.app.register_error_handler(NoAuthorizationError, handle_auth_error)
self.app.register_error_handler(InvalidTokenError, handle_invalid_token)
2024-01-07 11:28:49 +00:00
def set_up_jwt(self):
self.jwt_manager = JWTManager(self.app)
self.app.config['JWT_TOKEN_LOCATION'] = ['cookies']
self.app.config['JWT_COOKIE_NAME'] = 'access_token_cookie'
2024-01-07 11:42:48 +00:00
self.app.config['JWT_COOKIE_CSRF_PROTECT'] = False
2024-01-08 10:19:55 +00:00
self.app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(days=30)
2024-01-07 11:28:49 +00:00
def register_blueprints(self):
self.app.register_blueprint(userRoutes, url_prefix='/user')
self.app.register_blueprint(eventRoutes, url_prefix='/event')
def print_endpoints(self):
print("Endpoints and their functions:")
for rule in self.app.url_map.iter_rules():
print(f"Endpoint: {rule.endpoint}, Path: {rule}")
function_name = self.app.view_functions[rule.endpoint].__name__
print(f" Function: {function_name}")
2024-01-07 14:21:35 +00:00
2024-01-08 20:03:51 +00:00
def SchadulerConfig(object):
SCHEDULER_API_ENABLED = True
def locate_upcoming_events(self):
with self.app.app_context():
2024-01-09 08:23:57 +00:00
print(EventNotifyerService.locate_upcoming_events())
2024-01-09 08:14:28 +00:00
def run(self):
with self.app.app_context():
db.create_all()
self.setup_scheduler() # Setup scheduler
self.app.run(port=8000, debug=True)
2024-01-08 20:03:51 +00:00
2024-01-07 14:21:35 +00:00
app_class_instance = App()
app_instance = app_class_instance.app
if __name__ == '__main__':
app_class_instance.print_endpoints()
2024-01-07 14:21:35 +00:00
app_class_instance.run()