Initial commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
"""
|
||||
Simbrella FirstAdvance API Flask Implementation
|
||||
This module serves as the entry point for the Flask application.
|
||||
"""
|
||||
from flask import Flask
|
||||
from flask.typing import ResponseReturnValue
|
||||
from config import Config
|
||||
from api.routes import register_blueprints
|
||||
from api.middleware import register_middleware
|
||||
import logging
|
||||
import socket
|
||||
import sys
|
||||
|
||||
def create_app(config_class=Config) -> Flask:
|
||||
"""
|
||||
Factory pattern to create the Flask application.
|
||||
|
||||
Args:
|
||||
config_class: Configuration class to use
|
||||
|
||||
Returns:
|
||||
Flask application instance
|
||||
"""
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(config_class)
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
|
||||
# Register middleware
|
||||
register_middleware(app)
|
||||
|
||||
# Register blueprints
|
||||
register_blueprints(app)
|
||||
|
||||
# Register error handlers
|
||||
@app.errorhandler(400)
|
||||
def bad_request(error) -> ResponseReturnValue:
|
||||
return {"resultCode": "400", "resultDescription": f"Bad request: {str(error)}"}, 400
|
||||
|
||||
@app.errorhandler(404)
|
||||
def not_found(error) -> ResponseReturnValue:
|
||||
return {"resultCode": "404", "resultDescription": "Resource not found"}, 404
|
||||
|
||||
@app.errorhandler(422)
|
||||
def validation_error(error) -> ResponseReturnValue:
|
||||
return {"resultCode": "422", "resultDescription": f"Validation error: {str(error)}"}, 422
|
||||
|
||||
@app.errorhandler(500)
|
||||
def server_error(error) -> ResponseReturnValue:
|
||||
return {"resultCode": "500", "resultDescription": "Internal server error"}, 500
|
||||
|
||||
return app
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = create_app()
|
||||
|
||||
# Get port from config or use a default
|
||||
port = app.config.get('PORT', 5000)
|
||||
|
||||
# Try to run the app, with better error handling for socket issues
|
||||
try:
|
||||
app.run(debug=app.config.get('DEBUG', False), host='0.0.0.0', port=port)
|
||||
except socket.error as e:
|
||||
if e.errno == 10013: # Permission denied error on Windows
|
||||
print(f"Error: Permission denied when trying to bind to port {port}.")
|
||||
print("Try one of the following solutions:")
|
||||
print(f"1. Use a different port (above 1024): set PORT=8080 in your environment variables")
|
||||
print("2. Run the application with administrator privileges")
|
||||
print("3. Check if another application is already using this port")
|
||||
else:
|
||||
print(f"Socket error: {e}")
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user