-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
82 lines (71 loc) · 2.71 KB
/
Copy pathserver.py
File metadata and controls
82 lines (71 loc) · 2.71 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
#!/usr/bin/env python3
"""
GREEN KARMA — Smart Waste Management & Rewards Platform
Local Development & REST API Server
"""
import http.server
import socketserver
import os
import sys
import json
import urllib.parse
from datetime import datetime
# Configure UTF-8 for windows console
if sys.platform == 'win32':
try:
sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')
except Exception:
pass
PORT = 8081
DIRECTORY = os.path.dirname(os.path.abspath(__file__))
class GreenKarmaHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type, Authorization')
self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate')
super().end_headers()
def do_OPTIONS(self):
self.send_response(200)
self.end_headers()
def do_GET(self):
parsed_url = urllib.parse.urlparse(self.path)
# API Health Check
if parsed_url.path == '/api/health':
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
response = {
"status": "healthy",
"platform": "GREEN KARMA — Smart Waste Management",
"tagline": "EARN. RECYCLE. REWARD.",
"timestamp": datetime.now().isoformat(),
"databases": {
"municipal_waste_db": "CONNECTED",
"citizen_ledger_db": "CONNECTED",
"smart_city_api": "CONNECTED",
"mrf_recyclers_net": "CONNECTED"
}
}
self.wfile.write(json.dumps(response, indent=2).encode('utf-8'))
return
return super().do_GET()
def run_server():
os.chdir(DIRECTORY)
socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer(("", PORT), GreenKarmaHandler) as httpd:
print("==================================================")
print(" GREEN KARMA Web Platform Running")
print(" Tagline: EARN. RECYCLE. REWARD.")
print(f" URL: http://127.0.0.1:{PORT}")
print(f" Serving directory: {DIRECTORY}")
print("==================================================")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server...")
if __name__ == "__main__":
run_server()