-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·75 lines (61 loc) · 2.14 KB
/
example.py
File metadata and controls
executable file
·75 lines (61 loc) · 2.14 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
#!/usr/bin/env python
"""
Example usage of ApiFuncFramework
"""
import logging
import os
import sys
import json
# Add the src directory to the Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
# Import the necessary components from apifunc
from apifunc.components import DynamicgRPCComponent
from apifunc.apifunc import ApiFuncConfig, ApiFuncFramework
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
def json_to_html(json_data):
"""Convert JSON data to HTML"""
try:
# Parse JSON if it's a string
if isinstance(json_data, str):
data = json.loads(json_data)
else:
data = json_data
# Create a simple HTML representation
html = "<html><body><h1>JSON Data</h1><ul>"
for key, value in data.items():
html += f"<li><strong>{key}:</strong> {value}</li>"
html += "</ul></body></html>"
return html
except Exception as e:
logger.error(f"Error converting JSON to HTML: {e}")
return f"<html><body><h1>Error</h1><p>{str(e)}</p></body></html>"
def main():
"""Main function"""
try:
logger.info("Starting the apifunc example pipeline...")
# Create framework with configuration
config = ApiFuncConfig(
proto_dir="./proto",
generated_dir="./generated",
port=50051 # Set the port here in the config
)
framework = ApiFuncFramework(config)
# Register the function with the framework
# The framework will create a DynamicgRPCComponent internally
framework.register_function(json_to_html)
# Start the gRPC server
server = framework.start_server()
logger.info(f"Server running on port {config.port}. Press Ctrl+C to stop.")
server.wait_for_termination()
except KeyboardInterrupt:
logger.info("Server stopped by user.")
except Exception as e:
logger.error(f"Error processing: {str(e)}", exc_info=True)
if __name__ == "__main__":
main()