Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-primitive.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Dan Q Consolidate duplicate QUICKSTARTs on the top-level file 503f830 7 months ago History
info.py · 1013 bytes · 37 lines
1"""
2Cluster Info
3
4GET /cluster/info - Get information about this station
5
6Returns station ID, role (master/worker), and cluster configuration.
7"""
8import json
9import os
10import socket
11from dbbasic_web.responses import json as json_response
12
13
14def 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 }))