From d9f5261b808d33bce062de35ffff8bc4278b4ade Mon Sep 17 00:00:00 2001 From: Arnaud Date: Thu, 2 Oct 2025 11:42:04 +0200 Subject: [PATCH] Make rest api server optional --- codex/codex.nim | 18 +++++++++++++----- codex/conf.nim | 6 ++++-- .../requests/node_lifecycle_request.nim | 2 ++ tests/integration/codexprocess.nim | 3 ++- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/codex/codex.nim b/codex/codex.nim index 840f50aa..5e66fb0d 100644 --- a/codex/codex.nim +++ b/codex/codex.nim @@ -184,20 +184,25 @@ proc start*(s: CodexServer) {.async.} = await s.bootstrapInteractions() await s.codexNode.start() - s.restServer.start() + + if s.restServer != nil: + s.restServer.start() proc stop*(s: CodexServer) {.async.} = notice "Stopping codex node" - let res = await noCancel allFinishedFailed[void]( + var futures = @[ - s.restServer.stop(), s.codexNode.switch.stop(), s.codexNode.stop(), s.repoStore.stop(), s.maintenance.stop(), ] - ) + + if s.restServer != nil: + futures.add(s.restServer.stop()) + + let res = await noCancel allFinishedFailed[void](futures) if res.failure.len > 0: error "Failed to stop codex node", failures = res.failure.len @@ -333,10 +338,13 @@ proc new*( taskPool = taskpool, ) + var restServer: RestServerRef = nil + + if config.apiBindAddress.isSome: restServer = RestServerRef .new( codexNode.initRestApi(config, repoStore, config.apiCorsAllowedOrigin), - initTAddress(config.apiBindAddress, config.apiPort), + initTAddress(config.apiBindAddress.get(), config.apiPort), bufferSize = (1024 * 64), maxRequestBodySize = int.high, ) diff --git a/codex/conf.nim b/codex/conf.nim index 149f8152..6294516e 100644 --- a/codex/conf.nim +++ b/codex/conf.nim @@ -203,8 +203,10 @@ type .}: string apiBindAddress* {. - desc: "The REST API bind address", defaultValue: "127.0.0.1", name: "api-bindaddr" - .}: string + desc: "The REST API bind address", + defaultValue: "127.0.0.1".some, + name: "api-bindaddr" + .}: Option[string] apiPort* {. desc: "The REST Api port", diff --git a/library/codex_thread_requests/requests/node_lifecycle_request.nim b/library/codex_thread_requests/requests/node_lifecycle_request.nim index 53fa9ec9..885a2a0e 100644 --- a/library/codex_thread_requests/requests/node_lifecycle_request.nim +++ b/library/codex_thread_requests/requests/node_lifecycle_request.nim @@ -141,6 +141,8 @@ proc createCodex( return err("Failed to create codex: unable to get the private key.") let pk = privateKey.get() + conf.apiBindAddress = string.none + let server = try: CodexServer.new(conf, pk) diff --git a/tests/integration/codexprocess.nim b/tests/integration/codexprocess.nim index 3eca5b04..351a78e2 100644 --- a/tests/integration/codexprocess.nim +++ b/tests/integration/codexprocess.nim @@ -51,7 +51,8 @@ proc ethAccount*(node: CodexProcess): Address = proc apiUrl*(node: CodexProcess): string = let config = CodexConf.load(cmdLine = node.arguments, quitOnFailure = false) - return "http://" & config.apiBindAddress & ":" & $config.apiPort & "/api/codex/v1" + return + "http://" & config.apiBindAddress.get() & ":" & $config.apiPort & "/api/codex/v1" proc client*(node: CodexProcess): CodexClient = if client =? node.client: