-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (90 loc) · 3.9 KB
/
main.py
File metadata and controls
110 lines (90 loc) · 3.9 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
Weather Forecast Application
This software provides a weather forecast service using the WeatherAPI. Users can retrieve a 10-day weather forecast by providing a ZIP code. The application supports both JSON responses for CLI tools and HTML responses for browsers.
Author: John Wildes
Contact: johnwildes@decklatedev.com
License: Apache License 2.0
Created with GitHub Copilot
"""
# Import necessary modules for Flask application, environment variable handling, and HTTP requests
import os
import logging
from flask import Flask
from dotenv import load_dotenv
# Import blueprints
from routes.forecast import forecast_bp
from routes.home import home_bp
from routes.chat import chat_bp
from datetime import datetime, timedelta
# Load environment variables from a .env file if it exists
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Initialize the Flask application
app = Flask(__name__)
# Template filters for better date formatting
@app.template_filter('format_date')
def format_date_filter(date_string):
"""Format date string for display"""
try:
date_obj = datetime.strptime(date_string, '%Y-%m-%d')
today = datetime.now().date()
date_date = date_obj.date()
if date_date == today:
return 'Today'
elif date_date == today + timedelta(days=1):
return 'Tomorrow'
elif date_date == today - timedelta(days=1):
return 'Yesterday'
else:
return date_obj.strftime('%a, %b %d')
except:
return date_string
# Register blueprints
app.register_blueprint(forecast_bp, url_prefix='/forecast')
app.register_blueprint(home_bp, url_prefix='/')
app.register_blueprint(chat_bp, url_prefix='/api/chat')
# Register API routes directly to the main app for cleaner URLs
from routes.forecast import get_bulk_weather, validate_location, search_locations, get_detailed_forecast, get_hourly_forecast
app.add_url_rule('/api/weather/bulk', 'bulk_weather', get_bulk_weather, methods=['POST'])
app.add_url_rule('/api/validate-location', 'validate_location', validate_location, methods=['GET'])
app.add_url_rule('/api/search-locations', 'search_locations', search_locations, methods=['GET'])
app.add_url_rule('/api/detailed-forecast', 'detailed_forecast', get_detailed_forecast, methods=['GET'])
app.add_url_rule('/api/hourly-forecast', 'hourly_forecast', get_hourly_forecast, methods=['GET'])
# Context processor to inject debug_mode into all templates
@app.context_processor
def inject_debug_mode():
return {'debug_mode': os.getenv('WEATHER_DEBUG_MODE', 'false').lower() == 'true'}
# Debug API endpoint for environment and server info
@app.route('/api/debug/info')
def debug_info():
"""Returns debug-safe environment info (no secrets)"""
import sys
from importlib.metadata import version
# Only allow if debug mode is enabled
if os.getenv('WEATHER_DEBUG_MODE', 'false').lower() != 'true':
return {'error': 'Debug mode not enabled'}, 403
return {
'environment': {
'flask_debug': os.getenv('FLASK_DEBUG', 'false'),
'port': os.getenv('PORT', '5000'),
'default_zip_code': os.getenv('DEFAULT_ZIP_CODE', 'not set'),
'weather_debug_mode': os.getenv('WEATHER_DEBUG_MODE', 'false'),
},
'server': {
'python_version': sys.version,
'flask_version': version('flask'),
},
'timestamp': datetime.now().isoformat()
}
if __name__ == '__main__':
# Get the port from the environment variable or default to 5000
port = int(os.getenv('PORT', 5000))
# Determine whether to run in debug mode based on the environment variable
debug_mode = os.getenv('FLASK_DEBUG', 'false').lower() == 'true'
# Run the Flask application
app.run(host='0.0.0.0', port=port, debug=debug_mode)