Allow cross origin requests for cid retreival

This commit is contained in:
Eric 2024-05-28 15:37:54 +10:00
parent e6a387e8e8
commit 3f7267d285
No known key found for this signature in database
1 changed files with 8 additions and 4 deletions

View File

@ -66,7 +66,8 @@ proc retrieveCid(
node: CodexNodeRef, node: CodexNodeRef,
cid: Cid, cid: Cid,
local: bool = true, local: bool = true,
resp: HttpResponseRef): Future[RestApiResponse] {.async.} = resp: HttpResponseRef,
allowedOrigin: ?string = string.none): Future[RestApiResponse] {.async.} =
## Download a file from the node in a streaming ## Download a file from the node in a streaming
## manner ## manner
## ##
@ -83,6 +84,8 @@ proc retrieveCid(
return RestApiResponse.error(Http500, error.msg) return RestApiResponse.error(Http500, error.msg)
resp.addHeader("Content-Type", "application/octet-stream") resp.addHeader("Content-Type", "application/octet-stream")
if corsOrigin =? allowedOrigin:
resp.addHeader("Access-Control-Allow-Origin", corsOrigin)
await resp.prepareChunked() await resp.prepareChunked()
while not stream.atEof: while not stream.atEof:
@ -107,6 +110,7 @@ proc retrieveCid(
await stream.close() await stream.close()
proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRouter) = proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRouter) =
let allowedOrigin = router.allowedOrigin
router.rawApi( router.rawApi(
MethodPost, MethodPost,
"/api/codex/v1/data") do ( "/api/codex/v1/data") do (
@ -166,7 +170,7 @@ proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRoute
Http400, Http400,
$cid.error()) $cid.error())
await node.retrieveCid(cid.get(), local = true, resp=resp) await node.retrieveCid(cid.get(), local = true, resp=resp, allowedOrigin)
router.api( router.api(
MethodGet, MethodGet,
@ -181,7 +185,7 @@ proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRoute
Http400, Http400,
$cid.error()) $cid.error())
await node.retrieveCid(cid.get(), local = false, resp=resp) await node.retrieveCid(cid.get(), local = false, resp=resp, allowedOrigin)
router.api( router.api(
MethodGet, MethodGet,
@ -637,7 +641,7 @@ proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) =
return RestApiResponse.error(Http500) return RestApiResponse.error(Http500)
proc initRestApi*(node: CodexNodeRef, conf: CodexConf, repoStore: RepoStore): RestRouter = proc initRestApi*(node: CodexNodeRef, conf: CodexConf, repoStore: RepoStore): RestRouter =
var router = RestRouter.init(validate) var router = RestRouter.init(validate, "*".some)
initDataApi(node, repoStore, router) initDataApi(node, repoStore, router)
initSalesApi(node, router) initSalesApi(node, router)