-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
135 lines (115 loc) · 4.3 KB
/
server.py
File metadata and controls
135 lines (115 loc) · 4.3 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
"""
Mini servidor local que serve o HTML e faz proxy das chamadas API do Hub 2.0.
Necessário porque a API não tem headers CORS.
Uso: python3 server.py
Abre: http://localhost:8080
"""
import http.server
import json
import os
import urllib.request
import urllib.error
from urllib.parse import urlparse
PORT = 8080
TARGETS = {
"/proxy/accounts": "https://api-accounts.hubert.com.br",
"/proxy/morador": "https://api-morador.hubert.com.br",
}
class ProxyHandler(http.server.SimpleHTTPRequestHandler):
def do_OPTIONS(self):
self.send_response(204)
self._cors_headers()
self.end_headers()
def do_GET(self):
if self.path.startswith("/proxy/"):
self._proxy("GET")
else:
super().do_GET()
def do_POST(self):
if self.path.startswith("/proxy/"):
self._proxy("POST")
else:
self.send_error(405)
def _cors_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", "Authorization, Content-Type, Origin")
def _proxy(self, method: str):
# Resolve target URL
target_url = None
api_path = ""
for prefix, base in TARGETS.items():
if self.path.startswith(prefix):
api_path = self.path[len(prefix):]
target_url = base + api_path
break
if not target_url:
self.send_error(404, "Proxy target not found")
return
# Read request body
body = None
content_length = int(self.headers.get("Content-Length", 0))
if content_length > 0:
body = self.rfile.read(content_length)
# Build upstream request
req = urllib.request.Request(target_url, data=body, method=method)
# Forward relevant headers
for header in ["Authorization", "Content-Type"]:
val = self.headers.get(header)
if val:
req.add_header(header, val)
# Always set Origin to app-morador (browser overrides Origin header)
req.add_header("Origin", "app-morador")
try:
with urllib.request.urlopen(req, timeout=30) as resp:
resp_body = resp.read()
self.send_response(resp.status)
self._cors_headers()
self.send_header("Content-Type", resp.headers.get("Content-Type", "application/json"))
self.end_headers()
self.wfile.write(resp_body)
except urllib.error.HTTPError as e:
resp_body = e.read()
self.send_response(e.code)
self._cors_headers()
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(resp_body)
except Exception as e:
self.send_response(502)
self._cors_headers()
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps({"error": str(e)}).encode())
def log_message(self, fmt, *args):
# Colorir logs
msg = fmt % args
if "POST" in msg and "proxy" in msg:
print(f" 🔄 {msg}")
elif "GET" in msg and "proxy" in msg:
print(f" 📡 {msg}")
elif "200" in msg or "204" in msg:
print(f" ✅ {msg}")
elif "400" in msg or "401" in msg or "500" in msg:
print(f" ❌ {msg}")
else:
print(f" {msg}")
def main():
os.chdir(os.path.dirname(os.path.abspath(__file__)))
server = http.server.HTTPServer(("0.0.0.0", PORT), ProxyHandler)
print(f"""
╔══════════════════════════════════════════╗
║ 🏢 HUB 2.0 - LOCAL SERVER ⚡ ║
╚══════════════════════════════════════════╝
🌐 http://localhost:{PORT}
📁 Servindo: {os.getcwd()}
⏹️ Ctrl+C para parar
""")
try:
server.serve_forever()
except KeyboardInterrupt:
print("\n Servidor parado.")
server.server_close()
if __name__ == "__main__":
main()