...
This commit is contained in:
BIN
lib/hero/heromodels/examples/server_example
Executable file
BIN
lib/hero/heromodels/examples/server_example
Executable file
Binary file not shown.
@@ -4,8 +4,23 @@ import json
|
||||
import freeflowuniverse.herolib.hero.heromodels
|
||||
import freeflowuniverse.herolib.core.redisclient
|
||||
|
||||
// Comment-specific argument structures
|
||||
@[params]
|
||||
pub struct CommentGetArgs {
|
||||
pub mut:
|
||||
id ?u32
|
||||
author ?u32
|
||||
parent ?u32
|
||||
}
|
||||
|
||||
@[params]
|
||||
pub struct CommentDeleteArgs {
|
||||
pub mut:
|
||||
id u32
|
||||
}
|
||||
|
||||
// comment_get retrieves comments based on the provided arguments
|
||||
fn (mut server RPCServer) comment_get(params string) !string {
|
||||
fn comment_get(params string) !string {
|
||||
// Handle empty params
|
||||
if params == 'null' || params == '{}' {
|
||||
return error('No valid search criteria provided. Please specify id, author, or parent.')
|
||||
@@ -21,26 +36,26 @@ fn (mut server RPCServer) comment_get(params string) !string {
|
||||
|
||||
// If author is provided, find comments by author
|
||||
if author := args.author {
|
||||
return server.get_comments_by_author(author)!
|
||||
return get_comments_by_author(author)!
|
||||
}
|
||||
|
||||
// If parent is provided, find child comments
|
||||
if parent := args.parent {
|
||||
return server.get_comments_by_parent(parent)!
|
||||
return get_comments_by_parent(parent)!
|
||||
}
|
||||
|
||||
return error('No valid search criteria provided. Please specify id, author, or parent.')
|
||||
}
|
||||
|
||||
// comment_set creates or updates a comment
|
||||
fn (mut server RPCServer) comment_set(params string) !string {
|
||||
fn comment_set(params string) !string {
|
||||
comment_arg := json.decode(heromodels.CommentArg, params)!
|
||||
id := heromodels.comment_set(comment_arg)!
|
||||
return json.encode({'id': id})
|
||||
}
|
||||
|
||||
// comment_delete removes a comment by ID
|
||||
fn (mut server RPCServer) comment_delete(params string) !string {
|
||||
fn comment_delete(params string) !string {
|
||||
args := json.decode(CommentDeleteArgs, params)!
|
||||
|
||||
// Check if comment exists
|
||||
@@ -57,7 +72,7 @@ fn (mut server RPCServer) comment_delete(params string) !string {
|
||||
}
|
||||
|
||||
// comment_list returns all comment IDs
|
||||
fn (mut server RPCServer) comment_list() !string {
|
||||
fn comment_list() !string {
|
||||
mut redis := redisclient.core_get()!
|
||||
keys := redis.hkeys('db:comments:data')!
|
||||
mut ids := []u32{}
|
||||
@@ -70,7 +85,7 @@ fn (mut server RPCServer) comment_list() !string {
|
||||
}
|
||||
|
||||
// Helper function to get comments by author
|
||||
fn (mut server RPCServer) get_comments_by_author(author u32) !string {
|
||||
fn get_comments_by_author(author u32) !string {
|
||||
mut redis := redisclient.core_get()!
|
||||
all_data := redis.hgetall('db:comments:data')!
|
||||
mut matching_comments := []heromodels.Comment{}
|
||||
@@ -86,7 +101,7 @@ fn (mut server RPCServer) get_comments_by_author(author u32) !string {
|
||||
}
|
||||
|
||||
// Helper function to get comments by parent
|
||||
fn (mut server RPCServer) get_comments_by_parent(parent u32) !string {
|
||||
fn get_comments_by_parent(parent u32) !string {
|
||||
mut redis := redisclient.core_get()!
|
||||
all_data := redis.hgetall('db:comments:data')!
|
||||
mut matching_comments := []heromodels.Comment{}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
module openrpc
|
||||
|
||||
// discover returns the OpenRPC specification for the HeroModels service
|
||||
fn (mut server RPCServer) discover() !string {
|
||||
spec_json := $tmpl("openrpc.json")
|
||||
return spec_json
|
||||
}
|
||||
@@ -1,11 +1,39 @@
|
||||
module openrpc
|
||||
|
||||
import json
|
||||
import x.json2 { Any }
|
||||
import x.json2
|
||||
import net.unix
|
||||
import os
|
||||
import freeflowuniverse.herolib.ui.console
|
||||
|
||||
//THIS IS DEFAULT NEEDED FOR EACH OPENRPC SERVER WE MAKE
|
||||
|
||||
pub struct JsonRpcRequest {
|
||||
pub:
|
||||
jsonrpc string = '2.0'
|
||||
method string
|
||||
params string
|
||||
id string
|
||||
}
|
||||
|
||||
// JSON-RPC 2.0 response structure
|
||||
pub struct JsonRpcResponse {
|
||||
pub:
|
||||
jsonrpc string = '2.0'
|
||||
result string
|
||||
error ?JsonRpcError
|
||||
id string
|
||||
}
|
||||
|
||||
// JSON-RPC 2.0 error structure
|
||||
pub struct JsonRpcError {
|
||||
pub:
|
||||
code int
|
||||
message string
|
||||
data string
|
||||
}
|
||||
|
||||
|
||||
pub struct RPCServer {
|
||||
pub mut:
|
||||
listener &unix.StreamListener
|
||||
@@ -18,6 +46,14 @@ pub mut:
|
||||
socket_path string = '/tmp/heromodels'
|
||||
}
|
||||
|
||||
// Temporary struct for parsing incoming JSON-RPC requests using json2
|
||||
struct JsonRpcRequestRaw {
|
||||
jsonrpc string
|
||||
method string
|
||||
params json2.Any
|
||||
id json2.Any
|
||||
}
|
||||
|
||||
pub fn new_rpc_server(args RPCServerArgs) !&RPCServer {
|
||||
// Remove existing socket file if it exists
|
||||
if os.exists(args.socket_path) {
|
||||
@@ -82,51 +118,16 @@ fn (mut server RPCServer) handle_connection(mut conn unix.StreamConn) {
|
||||
}
|
||||
|
||||
fn (mut server RPCServer) process_request(request_data string) !string {
|
||||
// Parse JSON-RPC request manually to handle params properly
|
||||
request_map := json.decode(map[string]Any, request_data)!
|
||||
|
||||
jsonrpc := request_map['jsonrpc']!.str()
|
||||
method := request_map['method']!.str()
|
||||
id := request_map['id']!.str()
|
||||
|
||||
// Handle params - convert to string representation
|
||||
params_str := if 'params' in request_map {
|
||||
params_any := request_map['params']!
|
||||
match params_any {
|
||||
string {
|
||||
params_any
|
||||
}
|
||||
else {
|
||||
json.encode(params_any)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
'null'
|
||||
}
|
||||
|
||||
// Parse JSON-RPC request using json2 to handle Any types
|
||||
request := json2.decode[JsonRpcRequestRaw](request_data)!
|
||||
// Convert params to string representation
|
||||
params_str := request.params.json_str()
|
||||
// Convert id to string
|
||||
id_str := request.id.json_str()
|
||||
r := request.method.trim_space().to_lower()
|
||||
// Route to appropriate method
|
||||
result := match method {
|
||||
'comment_get' {
|
||||
server.comment_get(params_str)!
|
||||
}
|
||||
'comment_set' {
|
||||
server.comment_set(params_str)!
|
||||
}
|
||||
'comment_delete' {
|
||||
server.comment_delete(params_str)!
|
||||
}
|
||||
'comment_list' {
|
||||
server.comment_list()!
|
||||
}
|
||||
'discover' {
|
||||
server.discover()!
|
||||
}
|
||||
else {
|
||||
return server.create_error_response(-32601, 'Method not found', id)
|
||||
}
|
||||
}
|
||||
|
||||
return server.create_success_response(result, id)
|
||||
result := server.process(r, params_str)!
|
||||
return server.create_success_response(result, id_str)
|
||||
}
|
||||
|
||||
fn (mut server RPCServer) create_success_response(result string, id string) string {
|
||||
@@ -151,3 +152,9 @@ fn (mut server RPCServer) create_error_response(code int, message string, id str
|
||||
}
|
||||
return json.encode(response)
|
||||
}
|
||||
|
||||
// discover returns the OpenRPC specification for the HeroModels service
|
||||
fn (mut server RPCServer) discover() !string {
|
||||
spec_json := $tmpl("openrpc.json")
|
||||
return spec_json
|
||||
}
|
||||
29
lib/hero/heromodels/openrpc/processor.v
Normal file
29
lib/hero/heromodels/openrpc/processor.v
Normal file
@@ -0,0 +1,29 @@
|
||||
module openrpc
|
||||
|
||||
// processes the JSON-RPC request
|
||||
pub fn (mut server RPCServer) process(r string, params_str string)! string {
|
||||
|
||||
// Route to appropriate method
|
||||
result := match r {
|
||||
'comment_get' {
|
||||
comment_get(params_str)!
|
||||
}
|
||||
'comment_set' {
|
||||
comment_set(params_str)!
|
||||
}
|
||||
'comment_delete' {
|
||||
comment_delete(params_str)!
|
||||
}
|
||||
'comment_list' {
|
||||
comment_list()!
|
||||
}
|
||||
'rpc.discover' {
|
||||
server.discover()!
|
||||
}
|
||||
else {
|
||||
return server.create_error_response(-32601, 'Method not found', r)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
module openrpc
|
||||
|
||||
import json
|
||||
|
||||
// JSON-RPC 2.0 request structure
|
||||
pub struct JsonRpcRequest {
|
||||
pub:
|
||||
jsonrpc string = '2.0'
|
||||
method string
|
||||
params string
|
||||
id string
|
||||
}
|
||||
|
||||
// JSON-RPC 2.0 response structure
|
||||
pub struct JsonRpcResponse {
|
||||
pub:
|
||||
jsonrpc string = '2.0'
|
||||
result string
|
||||
error ?JsonRpcError
|
||||
id string
|
||||
}
|
||||
|
||||
// JSON-RPC 2.0 error structure
|
||||
pub struct JsonRpcError {
|
||||
pub:
|
||||
code int
|
||||
message string
|
||||
data string
|
||||
}
|
||||
|
||||
// Comment-specific argument structures
|
||||
@[params]
|
||||
pub struct CommentGetArgs {
|
||||
pub mut:
|
||||
id ?u32
|
||||
author ?u32
|
||||
parent ?u32
|
||||
}
|
||||
|
||||
@[params]
|
||||
pub struct CommentDeleteArgs {
|
||||
pub mut:
|
||||
id u32
|
||||
}
|
||||
Reference in New Issue
Block a user