125 lines
3.6 KiB
GLSL
Executable File
125 lines
3.6 KiB
GLSL
Executable File
#!/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')
|