Update git remote URL from git.ourworld.tf to git.threefold.info

This commit is contained in:
2025-06-15 16:21:00 +02:00
parent ae6966edb2
commit fab2d199d1
13 changed files with 195 additions and 2056 deletions

124
examples/zinit_client_example.vsh Executable file
View File

@@ -0,0 +1,124 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.heroweb.clients.zinit
import x.json2
// Create a new Zinit client with the default socket path
mut client := zinit.new_default_client()
println('Connected to Zinit via OpenRPC')
// Example 1: Get the OpenRPC API specification
println('\n=== Getting API Specification ===')
api_spec_response := client.rpc_discover() or {
println('Error getting API spec: ${err}')
return
}
println('API Specification (first 100 chars): ${json2.encode(api_spec_response.spec)[..100]}...')
// Example 2: List all services
println('\n=== Listing Services ===')
service_list_response := client.service_list() or {
println('Error listing services: ${err}')
return
}
println('Services:')
for name, state in service_list_response.services {
println('- ${name}: ${state}')
}
// Example 3: Get detailed status of a service (if any exist)
if service_list_response.services.len > 0 {
service_name := service_list_response.services.keys()[0]
println('\n=== Getting Status for Service: ${service_name} ===')
status := client.service_status(service_name) or {
println('Error getting status: ${err}')
return
}
println('Service Status:')
println('- Name: ${status.name}')
println('- PID: ${status.pid}')
println('- State: ${status.state}')
println('- Target: ${status.target}')
println('- Dependencies:')
for dep_name, dep_state in status.after {
println(' - ${dep_name}: ${dep_state}')
}
// Example 4: Get service stats
println('\n=== Getting Stats for Service: ${service_name} ===')
stats := client.service_stats(service_name) or {
println('Error getting stats: ${err}')
println('Note: Stats are only available for running services')
return
}
println('Service Stats:')
println('- Memory Usage: ${stats.memory_usage} bytes (${zinit.format_memory_usage(stats.memory_usage)})')
println('- CPU Usage: ${stats.cpu_usage}% (${zinit.format_cpu_usage(stats.cpu_usage)})')
if stats.children.len > 0 {
println('- Child Processes:')
for child in stats.children {
println(' - PID: ${child.pid}, Memory: ${zinit.format_memory_usage(child.memory_usage)}, CPU: ${zinit.format_cpu_usage(child.cpu_usage)}')
}
}
// Example 5: Get logs for a service
println('\n=== Getting Logs for Service: ${service_name} ===')
logs_response := client.stream_current_logs(service_name) or {
println('Error getting logs: ${err}')
return
}
println('Service logs:')
for log in logs_response.logs {
println('- ${log}')
}
} else {
println('\nNo services found to query')
}
// Example 6: Create a new service (commented out for safety)
/*
println('\n=== Creating a New Service ===')
new_service_config := zinit.ServiceConfig{
exec: '/bin/echo "Hello from Zinit"'
oneshot: true
after: []string{}
log: zinit.log_stdout
env: {
'ENV_VAR': 'value'
}
}
create_response := client.service_create('example_service', new_service_config) or {
println('Error creating service: ${err}')
return
}
println('Service created: ${create_response.path}')
// Start the service
client.service_start('example_service') or {
println('Error starting service: ${err}')
return
}
println('Service started')
// Delete the service when done
client.service_stop('example_service') or {
println('Error stopping service: ${err}')
return
}
client.service_forget('example_service') or {
println('Error forgetting service: ${err}')
return
}
delete_response := client.service_delete('example_service') or {
println('Error deleting service: ${err}')
return
}
println('Service deleted: ${delete_response.result}')
*/
println('\nZinit OpenRPC client example completed')

66
examples/zinit_rpc_example.vsh Executable file
View File

@@ -0,0 +1,66 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.schemas.jsonrpc
import freeflowuniverse.herolib.clients.zinit
import json
// We'll use the ServiceStatusResponse from the zinit module
// No need to define our own struct
// Create a client using the Unix socket transport
mut cl := jsonrpc.new_unix_socket_client("/tmp/zinit.sock")
// Example 1: Discover the API using rpc_discover
// Create a request for rpc_discover method with empty parameters
discover_request := jsonrpc.new_request_generic('rpc.discover', []string{})
// Send the request and receive the OpenRPC specification as a JSON string
println('Sending rpc_discover request...')
println('This will return the OpenRPC specification for the API')
// Use map[string]string for the result to avoid json2.Any issues
api_spec_raw := cl.send[[]string, string](discover_request)!
// Parse the JSON string manually
println('API Specification (raw):')
println(api_spec_raw)
// Example 2: List all services
// Create a request for service_list method with empty parameters
list_request := jsonrpc.new_request_generic('service_list', []string{})
// Send the request and receive a map of service names to states
println('\nSending service_list request...')
service_list := cl.send[[]string, map[string]string](list_request)!
// Display the service list
println('Service List:')
println(service_list)
// Example 3: Get status of a specific service
// First, check if we have any services to query
if service_list.len > 0 {
// Get the first service name from the list
service_name := service_list.keys()[0]
// Create a request for service_status method with the service name as parameter
// The parameter for service_status is a single string (service name)
status_request := jsonrpc.new_request_generic('service_status', service_name)
// Send the request and receive a ServiceStatusResponse object
println('\nSending service_status request for service: $service_name')
service_status := cl.send[string, zinit.ServiceStatusResponse](status_request)!
// Display the service status details
println('Service Status:')
println('- Name: ${service_status.name}')
println('- PID: ${service_status.pid}')
println('- State: ${service_status.state}')
println('- Target: ${service_status.target}')
println('- Dependencies:')
for dep_name, dep_state in service_status.after {
println(' - $dep_name: $dep_state')
}
} else {
println('\nNo services found to query status')
}