refactor: integrate heromodels RPC with heroserver

- Integrate heromodels RPC as a handler within heroserver
- Update API endpoint to use standard JSON-RPC format
- Add generated curl examples with copy button to docs
- Improve error handling to return JSON-RPC errors
- Simplify heromodels server example script
This commit is contained in:
Mahmoud-Emad
2025-09-17 21:08:17 +03:00
parent 380a8dea1b
commit 386fae3421
10 changed files with 314 additions and 122 deletions

1
examples/hero/heromodels/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
heroserver_example

View File

@@ -0,0 +1,17 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals -no-skip-unused run
import freeflowuniverse.herolib.hero.heromodels.rpc
import freeflowuniverse.herolib.hero.heromodels
import time
fn main() {
// Start the server in a background thread
spawn fn () {
rpc.start(port: 8080) or { panic('Failed to start HeroModels server: ${err}') }
}()
// Keep the main thread alive
for {
time.sleep(time.second)
}
}

View File

@@ -1,49 +0,0 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals -no-skip-unused run
import json
import freeflowuniverse.herolib.hero.heromodels.rpc
import freeflowuniverse.herolib.schemas.jsonrpc
import freeflowuniverse.herolib.hero.heromodels
import time
fn main() {
println('Starting RPC server on port 9090...')
// Start the server in a background thread
spawn fn () {
rpc.start(http_port: 9090) or { panic('Failed to start RPC server: ${err}') }
}()
// Wait for server to start
time.sleep(time.second * 2)
println('Server started, now testing with some requests...')
// Create a calendar object to test with
mut mydb := heromodels.new()!
mut my_calendar := mydb.calendar.new(
color: '#FF0000'
timezone: 'UTC'
is_public: true
events: []u32{}
)!
my_calendar.name = 'Test Calendar'
my_calendar.description = 'A test calendar for RPC'
// Test the calendar_set RPC method
request := jsonrpc.new_request('calendar_set', json.encode(my_calendar))
println('Sending request: ${request}')
// TODO: Add HTTP client to actually send the request to localhost:9090
// For now, just show what would be sent
// Keep the server running
println('Server is running on http://localhost:9090')
println('You can test it with curl or other HTTP clients')
println('Press Ctrl+C to stop the server')
// Keep main thread alive so server continues running
for {
time.sleep(time.second * 10)
println('Server still running...')
}
}

View File

@@ -31,8 +31,7 @@ if http_port == 0 {
curl -X POST -H "Content-Type: application/json" -d \'\{"jsonrpc":"2.0","method":"comment_set","params":{"comment":"Hello world!","parent":0,"author":42},"id":1\}\' http://localhost:9933
'
)
')
}
rpc.start(http_port: http_port)!
rpc.start(port: http_port)!