File size: 2,831 Bytes
fab54be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7db73fd
 
 
fab54be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7db73fd
fab54be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Flask Application Factory.
"""

from flask import Flask
from flask_cors import CORS
from src import config
from src.database import mongo as mongo_service
from src.database import gee as gee_service
from src.services import model as model_service
from src.routes.health import health_bp
from src.routes.detection import detection_bp
from src.routes.feedback import feedback_bp
from src.routes.assets import assets_bp


def create_app():
    """
    Application factory function.
    """
    app = Flask(__name__)
    
    # Run dynamic environment sanity validation
    config.validate_environment()
    
    # ========================================================================
    # CONFIGURATION
    # ========================================================================
    app.config['JSON_SORT_KEYS'] = False
    
    # ========================================================================
    # CORS SETUP
    # ========================================================================
    CORS(app)
    
    # ========================================================================
    # INITIALIZATION: EARTH ENGINE
    # ========================================================================
    try:
        gee_service.initialize_earth_engine()
    except Exception as e:
        config.logger.warning(f"⚠️ Earth Engine initialization warning: {e}")
    
    # ========================================================================
    # INITIALIZATION: MONGODB
    # ========================================================================
    db, generations_col = mongo_service.initialize_mongodb()
    
    # ========================================================================
    # INITIALIZATION: MODEL
    # ========================================================================
    model = model_service.initialize_model()
    
    # ========================================================================
    # BLUEPRINT REGISTRATION
    # ========================================================================
    # Explicitly registering routing layers
    app.register_blueprint(health_bp)
    app.register_blueprint(detection_bp)
    app.register_blueprint(feedback_bp)
    
    # Keep assets mounted directly to the root url context /mask/<filename> 
    # to perfectly mirror the old working monolithic environment
    app.register_blueprint(assets_bp)
    
    # ========================================================================
    # ERROR HANDLERS
    # ========================================================================
    @app.errorhandler(404)
    def not_found(error):
        return {"error": "Not found"}, 404
    
    @app.errorhandler(500)
    def internal_error(error):
        return {"error": "Internal server error"}, 500
    
    return app