Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion backend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# intentionally left empty — makes backend a package for making
# it as importable from the outside
# it as importable from the outside

from flask import Flask
from flask_caching import Cache

app = Flask(__name__)

# Simple in‑memory cache, 5 min default timeout
cache = Cache(app, config={'CACHE_TYPE': 'SimpleCache', 'CACHE_DEFAULT_TIMEOUT': 300})

# Import routes after app + cache are ready
from backend import alertsystem
32 changes: 32 additions & 0 deletions backend/alertsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import sys
import requests

from flask import request, jsonify
from backend import app, cache
from dotenv import load_dotenv

load_dotenv()
Expand Down Expand Up @@ -107,6 +109,36 @@ def frontend_files(filename):
CYCLONE_RISK_THRESHOLD = 0.60
DROUGHT_RISK_THRESHOLD = 0.70


OPENWEATHER_API_KEY = "YOUR_KEY"

@app.route("/weather")
def weather():
city = request.args.get("city")
state = request.args.get("state")
country = request.args.get("country")

if not city or not country:
return jsonify({"error": "Missing required fields"}), 400

# Build cache key
cache_key = f"weather:{city}:{state}:{country}"

# Check cache first
cached = cache.get(cache_key)
if cached:
return jsonify(cached)

# If not cached, call OpenWeatherMap
url = f"http://api.openweathermap.org/data/2.5/weather?q={city},{state},{country}&appid={OPENWEATHER_API_KEY}"
resp = requests.get(url)
data = resp.json()

# Store in cache
cache.set(cache_key, data)

return jsonify(data)

# =========================================================
# GET LOCATION COORDINATES (Nominatim / OpenStreetMap)
# =========================================================
Expand Down