Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #501 from freeflowuniverse/development_openapi
Browse files Browse the repository at this point in the history
Development openapi
  • Loading branch information
timurgordon authored Dec 16, 2024
2 parents 3578c01 + fd102bd commit ee79fcf
Show file tree
Hide file tree
Showing 40 changed files with 3,410 additions and 283 deletions.
15 changes: 10 additions & 5 deletions crystallib/clients/redisclient/redisclient_rpc.v
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ pub fn (mut q RedisRpc) result(timeout u64, retqueue string) !string {
if r != '' {
res := json.decode(Response, r)!
if res.error != '' {
return error(res.error)
return res.error
}
return res.result
}

if u64(time.now().unix_milli()) > (start + timeout) {
break
}
Expand All @@ -86,9 +85,15 @@ pub fn (mut q RedisRpc) result(timeout u64, retqueue string) !string {
return error('timeout on returnqueue: ${retqueue}')
}

@[params]
pub struct ProcessParams {
pub:
timeout u64
}

// to be used by processor, to get request and execute, this is the server side of a RPC mechanism
// 2nd argument is a function which needs to execute the job: fn (string,string) !string
pub fn (mut q RedisRpc) process(timeout u64, op fn (string, string) !string) !string {
pub fn (mut q RedisRpc) process(op fn (string, string) !string, params ProcessParams) !string {
start := u64(time.now().unix_milli())
for {
r := q.redis.rpop(q.key) or { '' }
Expand Down Expand Up @@ -117,10 +122,10 @@ pub fn (mut q RedisRpc) process(timeout u64, op fn (string, string) !string) !st
q.redis.lpush(returnqueue, encoded)!
return returnqueue
}
if u64(time.now().unix_milli()) > (start + timeout) {
if (params.timeout != 0) && u64(time.now().unix_milli()) > (start + params.timeout) {
break
}
time.sleep(time.microsecond)
time.sleep(time.millisecond)
}
return error('timeout for waiting for cmd on ${q.key}')
}
Expand Down
49 changes: 0 additions & 49 deletions crystallib/core/openapi/gen/README.md

This file was deleted.

220 changes: 0 additions & 220 deletions crystallib/core/openapi/model.v

This file was deleted.

3 changes: 2 additions & 1 deletion crystallib/data/jsonschema/model.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub mut:
properties map[string]SchemaRef
additional_properties SchemaRef @[json: 'additionalProperties']
required []string
ref string
items Items
defs map[string]SchemaRef
one_of []SchemaRef @[json: 'oneOf']
format string
// todo: make fields optional upon the fixing of https://github.com/vlang/v/issues/18775
// from https://git.sr.ht/~emersion/go-jsonschema/tree/master/item/schema.go
// Validation for numbers
Expand All @@ -34,4 +34,5 @@ pub mut:
exclusive_maximum int @[json: 'exclusiveMaximum'; omitempty]
minimum int @[omitempty]
exclusive_minimum int @[json: 'exclusiveMinimum'; omitempty]
enum_ []string @[json: 'enum'; omitempty]
}
33 changes: 33 additions & 0 deletions crystallib/hero/processor/error.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module processor

// ProcedureError struct for error handling
pub struct ProcedureError {
reason ErrorReason
}

// Enum for different error reasons
pub enum ErrorReason {
timeout
serialization_failed
deserialization_failed
enqueue_failed
}

pub fn (err ProcedureError) code() int {
return match err.reason {
.timeout { 408 } // HTTP 408 Request Timeout
.serialization_failed { 500 } // HTTP 500 Internal Server Error
.deserialization_failed { 500 } // HTTP 500 Internal Server Error
.enqueue_failed { 503 } // HTTP 503 Service Unavailable
}
}

pub fn (err ProcedureError) msg() string {
explanation := match err.reason {
.timeout { 'The procedure call timed out.' }
.serialization_failed { 'Failed to serialize the procedure call.' }
.deserialization_failed { 'Failed to deserialize the procedure response.' }
.enqueue_failed { 'Failed to enqueue the procedure response.' }
}
return 'Procedure failed: $explanation'
}
15 changes: 15 additions & 0 deletions crystallib/hero/processor/procedure.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module processor

// ProcedureCall struct representing a procedure invocation
pub struct ProcedureCall {
pub:
method string // Method name (derived from OpenAPI path)
params string // Parameters for the procedure
}

// ProcedureResponse struct representing the result of a procedure call
pub struct ProcedureResponse {
pub:
result string // Response data
error string // Internal error message (if any)
}
42 changes: 42 additions & 0 deletions crystallib/hero/processor/processor.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module processor

import json
import freeflowuniverse.crystallib.clients.redisclient

// Processor struct for managing procedure calls
pub struct Processor {
pub mut:
rpc redisclient.RedisRpc // Redis RPC mechanism
}

// Parameters for processing a procedure call
@[params]
pub struct ProcessParams {
pub:
timeout int // Timeout in seconds
}

// Process the procedure call
pub fn (mut p Processor) process(call ProcedureCall, params ProcessParams) !ProcedureResponse {
// Use RedisRpc's `call` to send the call and wait for the response
response_data := p.rpc.call(redisclient.RPCArgs{
cmd: call.method
data: call.params
timeout: u64(params.timeout * 1000) // Convert seconds to milliseconds
wait: true
}) or {
// TODO: check error type
return ProcedureResponse{
error: err.msg()
}
// return ProcedureError{
// reason: .timeout
// }
}

println('resp data ${response_data}')

return ProcedureResponse{
result: response_data
}
}
2 changes: 1 addition & 1 deletion crystallib/osal/utils.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn memdb_exists(key string) bool {
}

// Returns a logger object and allows you to specify via environment argument OSAL_LOG_LEVEL the debug level
pub fn get_logger() log.Logger {
pub fn get_logger() log.Log {
log_level := env_get_default('OSAL_LOG_LEVEL', 'info')
mut logger := &log.Log{}
logger.set_level(match log_level.to_lower() {
Expand Down
1 change: 0 additions & 1 deletion crystallib/rpc/jsonrpc/client.v
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ pub fn (mut client IJsonRpcClient) send_json_rpc[T, D](method string, data T, ti
@[params]
pub struct ClientConfig {
address string // address of ws server
logger &log.Logger
}
9 changes: 3 additions & 6 deletions crystallib/rpc/jsonrpc/handler.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import net.websocket
pub struct JsonRpcHandler {
pub mut:
// rpcwebsocket.RpcWsServer // server for ws communication
logger &log.Logger
// map of method names to procedure handlers
procedures map[string]ProcedureHandler
state voidptr
Expand All @@ -19,10 +18,8 @@ pub mut:
// decodes payload, execute procedure function, return encoded result
type ProcedureHandler = fn (payload string) !string

pub fn new_handler(logger &log.Logger) !&JsonRpcHandler {
return &JsonRpcHandler{
logger: unsafe { logger }
}
pub fn new_handler() !&JsonRpcHandler {
return &JsonRpcHandler{}
}

// registers procedure handlers by method name
Expand All @@ -36,7 +33,7 @@ pub fn (mut handler JsonRpcHandler) handler(client &websocket.Client, message st

pub fn (mut handler JsonRpcHandler) handle(message string) !string {
method := jsonrpcrequest_decode_method(message)!
handler.logger.debug('handler-> handling remote procedure call to method: ${method}')
println('handler-> handling remote procedure call to method: ${method}')
procedure_func := handler.procedures[method]
response := procedure_func(message) or { panic(err) }
return response
Expand Down
Loading

0 comments on commit ee79fcf

Please sign in to comment.