slim-bindings 1.0.0__py3-none-manylinux_2_28_aarch64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
examples/slim.py ADDED
@@ -0,0 +1,146 @@
1
+ # Copyright AGNTCY Contributors (https://github.com/agntcy)
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """
4
+ Slim server example (extensively commented).
5
+
6
+ This module demonstrates:
7
+ * Initializing tracing (optionally enabling OpenTelemetry export)
8
+ * Spinning up a Slim service in server mode using the global service
9
+ * Graceful shutdown via SIGINT (Ctrl+C)
10
+
11
+ High-level flow:
12
+ main() -> asyncio.run(amain())
13
+ amain():
14
+ * Parse CLI flags (address, OTEL toggle)
15
+ * Initialize global state and tracing
16
+ * Start the Slim server (managed by Rust runtime)
17
+ * Register SIGINT handler for graceful shutdown
18
+ * Wait until Ctrl+C is pressed
19
+ * Stop the server
20
+
21
+ Tracing:
22
+ When --enable-opentelemetry is passed, OTEL export is enabled towards
23
+ localhost:4317 (default OTLP gRPC collector). If no collector is running,
24
+ tracing initialization will still succeed but spans may be dropped.
25
+ """
26
+
27
+ import argparse
28
+ import asyncio
29
+ import sys
30
+ from signal import SIGINT
31
+
32
+ import slim_bindings
33
+
34
+ from .common import setup_service
35
+ from .config import ServerConfig, load_config_with_cli_override
36
+
37
+
38
+ async def amain(config: ServerConfig):
39
+ """
40
+ Async entry point for server.
41
+
42
+ Steps:
43
+ 1. Initialize tracing and global service.
44
+ 2. Start the server (Rust manages the server lifecycle).
45
+ 3. Register SIGINT handler and wait for shutdown signal.
46
+ 4. Stop the server gracefully.
47
+
48
+ Args:
49
+ config: ServerConfig instance containing all configuration.
50
+ """
51
+ # Get the global service instance
52
+ service = setup_service()
53
+
54
+ # Launch the embedded server with insecure TLS (development setting).
55
+ # The server runs in the Rust runtime and is managed there.
56
+ server_config = slim_bindings.new_insecure_server_config(config.slim)
57
+ await service.run_server_async(server_config)
58
+
59
+ print(f"Slim server started on {config.slim}")
60
+ print("Press Ctrl+C to stop the server")
61
+
62
+ # Event used to signal shutdown from SIGINT.
63
+ stop_event = asyncio.Event()
64
+
65
+ def shutdown():
66
+ """
67
+ Signal handler callback.
68
+ Sets the stop_event to begin shutdown sequence.
69
+ """
70
+ print("\nShutting down server...")
71
+ stop_event.set()
72
+
73
+ # Register signal handler for Ctrl+C.
74
+ loop = asyncio.get_running_loop()
75
+ loop.add_signal_handler(SIGINT, shutdown)
76
+
77
+ # Block until shutdown is requested.
78
+ # The server is running in the Rust runtime, so we just wait here.
79
+ await stop_event.wait()
80
+
81
+ # Stop the server gracefully
82
+ try:
83
+ await service.shutdown_async()
84
+ print(f"Server stopped at {config.slim}")
85
+ except Exception as e:
86
+ print(f"Error stopping server: {e}")
87
+
88
+
89
+ def main():
90
+ """
91
+ CLI entry-point for the server example.
92
+
93
+ Parses command-line arguments and config file, then runs the server.
94
+ """
95
+ parser = argparse.ArgumentParser(
96
+ description="SLIM Server Example\n\n"
97
+ "Start a SLIM server for handling client connections.",
98
+ formatter_class=argparse.RawDescriptionHelpFormatter,
99
+ )
100
+
101
+ parser.add_argument(
102
+ "-s",
103
+ "--slim",
104
+ type=str,
105
+ default="127.0.0.1:12345",
106
+ help="SLIM server address (host:port) (default: 127.0.0.1:12345)",
107
+ )
108
+
109
+ parser.add_argument(
110
+ "--enable-opentelemetry",
111
+ "-t",
112
+ action="store_true",
113
+ help="Enable OpenTelemetry tracing",
114
+ )
115
+
116
+ parser.add_argument(
117
+ "--config",
118
+ type=str,
119
+ help="Path to configuration file (JSON, YAML, or TOML)",
120
+ )
121
+
122
+ # Parse arguments
123
+ args = parser.parse_args()
124
+
125
+ # Convert to dictionary
126
+ args_dict = vars(args)
127
+
128
+ # Load configuration (CLI args override env vars and config file)
129
+ try:
130
+ config = load_config_with_cli_override(ServerConfig, args_dict)
131
+ except Exception as e:
132
+ print(f"Configuration error: {e}", file=sys.stderr)
133
+ sys.exit(1)
134
+
135
+ # Run the server
136
+ try:
137
+ asyncio.run(amain(config))
138
+ except KeyboardInterrupt:
139
+ print("\nServer terminated by user.")
140
+ except Exception as e:
141
+ print(f"Error: {e}", file=sys.stderr)
142
+ sys.exit(1)
143
+
144
+
145
+ if __name__ == "__main__":
146
+ main()
@@ -0,0 +1 @@
1
+ from .slim_bindings import * # NOQA
Binary file