Make rest api server optional

This commit is contained in:
Arnaud 2025-10-02 11:42:04 +02:00 committed by Eric
parent 34976c3c58
commit d9f5261b80
No known key found for this signature in database
4 changed files with 21 additions and 8 deletions

View File

@ -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,
)

View File

@ -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",

View File

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

View File

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