SocialShare / backend /app.py
NitinBot002's picture
Initial commit with all project files
f4854a1
"""
Social Share and Care Foundation β€” Flask Backend
"""
import os
from flask import Flask, jsonify, send_from_directory
from flask_cors import CORS
from config.settings import Config
# Path to the built React frontend (relative to this file's directory)
FRONTEND_DIST = os.path.join(os.path.dirname(__file__), '..', 'frontend', 'dist')
def create_app():
"""Application factory."""
app = Flask(__name__)
app.config.from_object(Config)
# CORS β€” allow frontend origin
CORS(app, resources={r"/api/*": {"origins": [Config.FRONTEND_URL, "http://localhost:5173"]}})
# Register blueprints
from routes.auth_routes import auth_bp
from routes.donation_routes import donation_bp
from routes.campaign_routes import campaign_bp
from routes.volunteer_routes import volunteer_bp, activity_bp
from routes.help_request_routes import help_request_bp
from routes.content_routes import content_bp
from routes.contact_routes import contact_bp
from routes.newsletter_routes import newsletter_bp
from routes.upload_routes import upload_bp
app.register_blueprint(auth_bp)
app.register_blueprint(donation_bp)
app.register_blueprint(campaign_bp)
app.register_blueprint(volunteer_bp)
app.register_blueprint(activity_bp)
app.register_blueprint(help_request_bp)
app.register_blueprint(content_bp)
app.register_blueprint(contact_bp)
app.register_blueprint(newsletter_bp)
app.register_blueprint(upload_bp)
# Set max upload size from config
app.config['MAX_CONTENT_LENGTH'] = app.config.get('MAX_CONTENT_LENGTH', 50 * 1024 * 1024)
# Health check
@app.route('/api/health', methods=['GET'])
def health():
return jsonify({
'status': 'ok',
'service': 'Social Share and Care Foundation API',
'version': '1.0.0',
})
# Error handlers
@app.errorhandler(404)
def not_found(e):
return jsonify({'error': 'Endpoint not found'}), 404
@app.errorhandler(500)
def server_error(e):
return jsonify({'error': 'Internal server error'}), 500
@app.errorhandler(400)
def bad_request(e):
return jsonify({'error': 'Bad request'}), 400
@app.errorhandler(403)
def forbidden(e):
return jsonify({'error': 'Forbidden'}), 403
# ── Serve React SPA in production ─────────────────────────
if os.path.isdir(FRONTEND_DIST):
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve_spa(path):
"""Serve the React SPA. API routes are handled by blueprints
(registered before this catch-all), so they take priority."""
# If a real file exists in dist (JS, CSS, images, etc.), serve it
file_path = os.path.join(FRONTEND_DIST, path)
if path and os.path.isfile(file_path):
return send_from_directory(FRONTEND_DIST, path)
# Otherwise, return index.html for client-side routing
return send_from_directory(FRONTEND_DIST, 'index.html')
return app
# Run the app
app = create_app()
if __name__ == '__main__':
port = int(os.getenv('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)