askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
info.py · 1013 bytes · 37 lines
| 1 | """ |
| 2 | Cluster Info |
| 3 | |
| 4 | GET /cluster/info - Get information about this station |
| 5 | |
| 6 | Returns station ID, role (master/worker), and cluster configuration. |
| 7 | """ |
| 8 | import json |
| 9 | import os |
| 10 | import socket |
| 11 | from dbbasic_web.responses import json as json_response |
| 12 | |
| 13 | |
| 14 | def GET(request): |
| 15 | """Get information about this station""" |
| 16 | station_id = os.environ.get('STATION_ID', 'unknown') |
| 17 | is_master = station_id == 'station1' |
| 18 | |
| 19 | # Try to get local IP address |
| 20 | try: |
| 21 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 22 | s.connect(('8.8.8.8', 80)) |
| 23 | local_ip = s.getsockname()[0] |
| 24 | s.close() |
| 25 | except: |
| 26 | local_ip = '127.0.0.1' |
| 27 | |
| 28 | return json_response(json.dumps({ |
| 29 | 'status': 'ok', |
| 30 | 'station_id': station_id, |
| 31 | 'is_master': is_master, |
| 32 | 'role': 'master' if is_master else 'worker', |
| 33 | 'host': local_ip, |
| 34 | 'port': 8001, |
| 35 | 'url': f'http://{local_ip}:8001', |
| 36 | 'cluster_endpoint': 'http://localhost:8001/cluster/stations' if is_master else None |
| 37 | })) |