|
1 | 1 | from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler |
2 | | -import requests |
3 | 2 | import sys |
| 3 | +import time |
| 4 | + |
| 5 | +try: |
| 6 | + from version import VERSION, COMMIT, BUILD_DATE |
| 7 | +except ImportError: |
| 8 | + VERSION = COMMIT = BUILD_DATE = "unknown" |
| 9 | + |
| 10 | +print(f"\nVersion: {VERSION}, Commit: {COMMIT}, Built: {BUILD_DATE}\n") |
| 11 | +sys.stdout.flush() |
4 | 12 |
|
5 | 13 | class SimpleHandler(BaseHTTPRequestHandler): |
6 | 14 | def do_GET(self): |
7 | | - print(f"{self.client_address[0]} requested {self.path}", file=sys.stderr) |
| 15 | + start = time.time() |
| 16 | + client_ip = self.client_address[0] |
| 17 | + path = self.path |
8 | 18 |
|
9 | | - if self.path == "/favicon.ico": |
10 | | - self.send_response(204) # No Content |
| 19 | + print(f"{client_ip} requested {path}", file=sys.stderr) |
| 20 | + |
| 21 | + if path == "/favicon.ico": |
| 22 | + self.send_response(204) |
11 | 23 | self.end_headers() |
12 | 24 | return |
13 | 25 |
|
14 | | - # GitHub API call |
15 | | - response = requests.get("https://api.github.com") |
16 | | - try: |
17 | | - data = response.json() |
18 | | - user_related = {k: v for k, v in data.items() if 'user' in k.lower()} |
19 | | - print("🔍 Filtered 'user' keys:") |
20 | | - for key, value in user_related.items(): |
21 | | - print(f"{key}: {value}") |
22 | | - except ValueError: |
23 | | - print("❌ Not JSON. Content-Type:", response.headers.get("Content-Type")) |
24 | | - |
25 | | - # Response to client |
26 | 26 | self.send_response(200) |
27 | 27 | self.send_header("Content-type", "text/plain; charset=utf-8") |
28 | 28 | self.end_headers() |
29 | 29 | self.wfile.write(b"Hello from Python 3.12 HTTP server!\n") |
30 | 30 |
|
31 | | - def log_message(self, format, *args): |
32 | | - # Cleaner logging to stderr for Docker visibility |
33 | | - print(f"{self.client_address[0]} requested {self.path}", file=sys.stderr) |
| 31 | + duration = time.time() - start |
| 32 | + print(f"⏱️ Served {path} in {duration:.3f}s", file=sys.stderr) |
34 | 33 |
|
35 | | -try: |
36 | | - from version import VERSION, COMMIT, BUILD_DATE |
37 | | -except ImportError: |
38 | | - VERSION = COMMIT = BUILD_DATE = "unknown" |
39 | | - |
40 | | -print(f"\nVersion: {VERSION}, Commit: {COMMIT}, Built: {BUILD_DATE}\n") |
41 | | - |
42 | | -sys.stdout.flush() |
| 34 | + def log_message(self, format, *args): |
| 35 | + return |
43 | 36 |
|
44 | 37 | if __name__ == "__main__": |
45 | 38 | host = "0.0.0.0" |
|
0 commit comments