This commit is contained in:
2025-09-02 09:09:21 +02:00
parent 418a38527a
commit 40455a8c2e
6 changed files with 104 additions and 104 deletions

Binary file not shown.

View File

@@ -4,8 +4,23 @@ import json
import freeflowuniverse.herolib.hero.heromodels import freeflowuniverse.herolib.hero.heromodels
import freeflowuniverse.herolib.core.redisclient 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 // 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 // Handle empty params
if params == 'null' || params == '{}' { if params == 'null' || params == '{}' {
return error('No valid search criteria provided. Please specify id, author, or parent.') 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 is provided, find comments by author
if author := args.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 is provided, find child comments
if parent := args.parent { 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.') return error('No valid search criteria provided. Please specify id, author, or parent.')
} }
// comment_set creates or updates a comment // 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)! comment_arg := json.decode(heromodels.CommentArg, params)!
id := heromodels.comment_set(comment_arg)! id := heromodels.comment_set(comment_arg)!
return json.encode({'id': id}) return json.encode({'id': id})
} }
// comment_delete removes a comment by 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)! args := json.decode(CommentDeleteArgs, params)!
// Check if comment exists // Check if comment exists
@@ -57,7 +72,7 @@ fn (mut server RPCServer) comment_delete(params string) !string {
} }
// comment_list returns all comment IDs // comment_list returns all comment IDs
fn (mut server RPCServer) comment_list() !string { fn comment_list() !string {
mut redis := redisclient.core_get()! mut redis := redisclient.core_get()!
keys := redis.hkeys('db:comments:data')! keys := redis.hkeys('db:comments:data')!
mut ids := []u32{} mut ids := []u32{}
@@ -70,7 +85,7 @@ fn (mut server RPCServer) comment_list() !string {
} }
// Helper function to get comments by author // 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()! mut redis := redisclient.core_get()!
all_data := redis.hgetall('db:comments:data')! all_data := redis.hgetall('db:comments:data')!
mut matching_comments := []heromodels.Comment{} 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 // 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()! mut redis := redisclient.core_get()!
all_data := redis.hgetall('db:comments:data')! all_data := redis.hgetall('db:comments:data')!
mut matching_comments := []heromodels.Comment{} mut matching_comments := []heromodels.Comment{}

View File

@@ -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
}

View File

@@ -1,11 +1,39 @@
module openrpc module openrpc
import json import json
import x.json2 { Any } import x.json2
import net.unix import net.unix
import os import os
import freeflowuniverse.herolib.ui.console 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 struct RPCServer {
pub mut: pub mut:
listener &unix.StreamListener listener &unix.StreamListener
@@ -18,6 +46,14 @@ pub mut:
socket_path string = '/tmp/heromodels' 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 { pub fn new_rpc_server(args RPCServerArgs) !&RPCServer {
// Remove existing socket file if it exists // Remove existing socket file if it exists
if os.exists(args.socket_path) { 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 { fn (mut server RPCServer) process_request(request_data string) !string {
// Parse JSON-RPC request manually to handle params properly // Parse JSON-RPC request using json2 to handle Any types
request_map := json.decode(map[string]Any, request_data)! request := json2.decode[JsonRpcRequestRaw](request_data)!
// Convert params to string representation
jsonrpc := request_map['jsonrpc']!.str() params_str := request.params.json_str()
method := request_map['method']!.str() // Convert id to string
id := request_map['id']!.str() id_str := request.id.json_str()
r := request.method.trim_space().to_lower()
// 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'
}
// Route to appropriate method // Route to appropriate method
result := match method { result := server.process(r, params_str)!
'comment_get' { return server.create_success_response(result, id_str)
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)
} }
fn (mut server RPCServer) create_success_response(result string, id string) string { fn (mut server RPCServer) create_success_response(result string, id string) string {
@@ -150,4 +151,10 @@ fn (mut server RPCServer) create_error_response(code int, message string, id str
id: id id: id
} }
return json.encode(response) 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
} }

View 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
}

View File

@@ -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
}