Merge branch 'development' into development_actions007
This commit is contained in:
27
lib/schemas/jsonrpc/testdata/handler_code.v
vendored
Normal file
27
lib/schemas/jsonrpc/testdata/handler_code.v
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
@[heap]
|
||||
struct TesterHandler {
|
||||
state Tester
|
||||
}
|
||||
|
||||
// handle handles an incoming JSON-RPC encoded message and returns an encoded response
|
||||
pub fn (handler TesterHandler) handle(msg string) string {
|
||||
method := jsonrpc.jsonrpcrequest_decode_method(msg)!
|
||||
match method {
|
||||
'test_notification_method' {
|
||||
jsonrpc.notify[string](msg, handler.state.test_notification_method)!
|
||||
}
|
||||
'test_invocation_method' {
|
||||
return jsonrpc.invoke[string](msg, handler.state.test_invocation_method)!
|
||||
}
|
||||
'test_method' {
|
||||
return jsonrpc.call[string, string](msg, handler.state.test_method)!
|
||||
}
|
||||
'test_method_structs' {
|
||||
return jsonrpc.call[Key, Value](msg, handler.state.test_method_structs)!
|
||||
}
|
||||
else {
|
||||
return error('method ${method} not handled')
|
||||
}
|
||||
}
|
||||
return error('this should never happen')
|
||||
}
|
||||
5
lib/schemas/jsonrpc/testdata/testfunction.v
vendored
Normal file
5
lib/schemas/jsonrpc/testdata/testfunction.v
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
module main
|
||||
|
||||
pub fn testfunction(param string) string {
|
||||
return param
|
||||
}
|
||||
14
lib/schemas/jsonrpc/testdata/testmodule/testfunctions.v
vendored
Normal file
14
lib/schemas/jsonrpc/testdata/testmodule/testfunctions.v
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
module testmodule
|
||||
|
||||
pub fn testfunction0(param string) string {
|
||||
return param
|
||||
}
|
||||
|
||||
pub struct Config {
|
||||
name string
|
||||
number int
|
||||
}
|
||||
|
||||
pub fn testfunction1(config Config) []string {
|
||||
return []string{len: config.number, init: config.name}
|
||||
}
|
||||
54
lib/schemas/jsonrpc/testdata/testserver/openrpc_server.v
vendored
Normal file
54
lib/schemas/jsonrpc/testdata/testserver/openrpc_server.v
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
module main
|
||||
|
||||
import log
|
||||
import json
|
||||
import freeflowuniverse.herolib.schemas.jsonrpc { JsonRpcHandler, jsonrpcrequest_decode }
|
||||
import freeflowuniverse.herolib.data.rpcwebsocket
|
||||
import data.jsonrpc.testdata.testmodule { Config, testfunction0, testfunction1 }
|
||||
|
||||
struct CustomJsonRpcHandler {
|
||||
JsonRpcHandler
|
||||
}
|
||||
|
||||
fn testfunction0_handler(data string) !string {
|
||||
request := jsonrpcrequest_decode[string](data)!
|
||||
result := testfunction0(request.params)
|
||||
response := jsonrpc.JsonRpcResponse[string]{
|
||||
jsonrpc: '2.0.0'
|
||||
id: request.id
|
||||
result: result
|
||||
}
|
||||
return response.to_json()
|
||||
}
|
||||
|
||||
fn testfunction1_handler(data string) !string {
|
||||
request := jsonrpcrequest_decode[Config](data)!
|
||||
result := testfunction1(request.params)
|
||||
response := jsonrpc.JsonRpcResponse[[]string]{
|
||||
jsonrpc: '2.0.0'
|
||||
id: request.id
|
||||
result: result
|
||||
}
|
||||
return response.to_json()
|
||||
}
|
||||
|
||||
// run_server creates and runs a jsonrpc ws server
|
||||
// handles rpc requests to reverse_echo function
|
||||
pub fn run_server() ! {
|
||||
mut logger := log.Logger(&log.Log{
|
||||
level: .debug
|
||||
})
|
||||
|
||||
mut handler := CustomJsonRpcHandler{
|
||||
JsonRpcHandler: jsonrpc.new_handler(&logger)!
|
||||
}
|
||||
|
||||
handler.state = &state
|
||||
// register rpc methods
|
||||
handler.register(testfunction0, testfunction0_handle)!
|
||||
handler.register(testfunction1, testfunction1_handle)!
|
||||
|
||||
// create & run rpc ws server
|
||||
mut jsonrpc_ws_server := rpcwebsocket.new_rpcwsserver(8080, handler, &logger)!
|
||||
jsonrpc_ws_server.run()!
|
||||
}
|
||||
Reference in New Issue
Block a user