|
6 | 6 | import os |
7 | 7 | import sys |
8 | 8 |
|
9 | | -# Add the project root to Python path |
10 | | -try: |
11 | | - project_root = os.path.dirname(os.path.abspath(__file__)) |
12 | | -except NameError: |
13 | | - # Handle case when __file__ is not defined (exec context) |
14 | | - project_root = os.getcwd() |
| 9 | +def setup_environment(): |
| 10 | + """Setup environment and paths.""" |
| 11 | + # Add the project root to Python path |
| 12 | + try: |
| 13 | + project_root = os.path.dirname(os.path.abspath(__file__)) |
| 14 | + except NameError: |
| 15 | + # Handle case when __file__ is not defined (exec context) |
| 16 | + project_root = os.getcwd() |
15 | 17 |
|
16 | | -sys.path.insert(0, project_root) |
| 18 | + sys.path.insert(0, project_root) |
| 19 | + |
| 20 | + # Create necessary directories |
| 21 | + os.makedirs('data/eea/renewable-energy', exist_ok=True) |
| 22 | + os.makedirs('data/eea/pollution', exist_ok=True) |
| 23 | + os.makedirs('logs', exist_ok=True) |
| 24 | + os.makedirs('reference', exist_ok=True) |
| 25 | + |
| 26 | + return project_root |
17 | 27 |
|
18 | 28 | def main(): |
19 | 29 | """Main application entry point.""" |
| 30 | + project_root = setup_environment() |
| 31 | + |
| 32 | + # Import after path setup |
20 | 33 | from api.api_server import app |
21 | 34 |
|
22 | | - # Configure for production |
| 35 | + # Configure for production/cloud deployment |
23 | 36 | port = int(os.environ.get('PORT', 5000)) |
24 | 37 | debug = os.environ.get('FLASK_DEBUG', '0') == '1' |
25 | 38 | env = os.environ.get('FLASK_ENV', 'development') |
| 39 | + host = '0.0.0.0' |
26 | 40 |
|
27 | | - print(f"Starting Permit API on port {port}") |
28 | | - print(f"Environment: {env}") |
29 | | - print(f"Debug mode: {debug}") |
30 | | - print(f"Project root: {project_root}") |
| 41 | + print(f"🚀 Starting Permit API") |
| 42 | + print(f" Port: {port}") |
| 43 | + print(f" Environment: {env}") |
| 44 | + print(f" Debug mode: {debug}") |
| 45 | + print(f" Project root: {project_root}") |
| 46 | + print(f" Host: {host}") |
31 | 47 |
|
32 | 48 | # Run the Flask app |
33 | | - app.run( |
34 | | - host='0.0.0.0', |
35 | | - port=port, |
36 | | - debug=debug, |
37 | | - threaded=True |
38 | | - ) |
| 49 | + try: |
| 50 | + app.run( |
| 51 | + host=host, |
| 52 | + port=port, |
| 53 | + debug=debug, |
| 54 | + threaded=True |
| 55 | + ) |
| 56 | + except Exception as e: |
| 57 | + print(f"❌ Failed to start server: {e}") |
| 58 | + sys.exit(1) |
39 | 59 |
|
40 | 60 | if __name__ == '__main__': |
41 | 61 | main() |
0 commit comments