Trying to create a traffic mapping API kinda thing to give to the local government...wish me luck
app.py
```python
import os import json from flask import Flask, request, jsonify, render_template import boto3 from datetime import datetime from flask_cors import CORS
app = Flask(name) CORS(app) # Enable CORS for all routes
Health check endpoint
@app.route('/health') def health(): return 'OK', 200
Load S3 credentials from environment variables
S3_KEY = os.environ.get('FILEBASE_KEY') S3_SECRET = os.environ.get('FILEBASE_SECRET') S3_BUCKET = os.environ.get('FILEBASE_BUCKET', 'harford-accidents') S3_ENDPOINT = 'https://s3.filebase.com' LOG_FILE = 'accident_log.json'
Helper: upload file to Filebase and return IPFS CID
def upload_to_filebase(filename): s3 = boto3.client('s3', endpoint_url=S3_ENDPOINT, aws_access_key_id=S3_KEY, aws_secret_access_key=S3_SECRET ) s3.upload_file(filename, S3_BUCKET, filename) # Get the CID from the file's metadata obj = s3.head_object(Bucket=S3_BUCKET, Key=filename) return obj['Metadata'].get('ipfs-hash', 'Unknown')
@app.route('/') def index(): return render_template('index.html')
@app.route('/report', methods=['POST']) def report(): data = request.json # Add timestamp data['timestamp'] = datetime.utcnow().isoformat() # Load or create log if os.path.exists(LOG_FILE): with open(LOG_FILE, 'r') as f: log = json.load(f) else: log = [] log.append(data) with open(LOG_FILE, 'w') as f: json.dump(log, f, indent=2) # Upload to Filebase cid = upload_to_filebase(LOG_FILE) return jsonify({'status': 'ok', 'cid': cid, 'log_length': len(log)})
@app.route('/log') def get_log(): if os.path.exists(LOG_FILE): with open(LOG_FILE) as f: return jsonify(json.load(f)) return jsonify([])
if name == 'main': app.run(host='0.0.0.0', port=5000)
```
This report was published via Actifit app (Android | iOS). Check out the original version here on actifit.io