-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* api: add `fastify` * clean up * address related PR review
- Loading branch information
1 parent
b0cb123
commit 0e029fc
Showing
8 changed files
with
653 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as Sentry from '@sentry/node' | ||
import Fastify from 'fastify' | ||
|
||
/** | ||
* @param {object} args | ||
* @param {Repository} args.repository | ||
* @param {string|boolean} args.domain | ||
* @param {Fastify.FastifyLoggerOptions} args.logger | ||
*/ | ||
export function createApp ({ repository, domain, logger }) { | ||
const app = Fastify({ logger }) | ||
Sentry.setupFastifyErrorHandler(app) | ||
|
||
if (typeof domain === 'string') { | ||
app.addHook('onRequest', async (request, reply) => { | ||
if (request.headers.host.split(':')[0] !== domain) { | ||
reply.redirect(`https://${domain}${request.url}`, 301) | ||
} | ||
}) | ||
} | ||
|
||
app.get('/sample/:providerId/:pieceCid', async (request, reply) => { | ||
const { providerId, pieceCid } = request.params | ||
const payloadCids = await repository.getPiecePayloadBlocks(providerId, pieceCid) | ||
const body = {} | ||
if (payloadCids.length) { | ||
body.samples = payloadCids.slice(0, 1) | ||
reply.header('cache-control', `public, max-age=${24 * 3600 /* 24 hours */}, immutable`) | ||
} else { | ||
body.error = 'PROVIDER_OR_PIECE_NOT_FOUND' | ||
reply.header('cache-control', `public, max-age=${60 /* 1min */}`) | ||
} | ||
reply.send(body) | ||
}) | ||
|
||
app.get('/ingestion-status/:providerId', async (request, reply) => { | ||
const { providerId } = request.params | ||
const walkerState = await repository.getWalkerState(providerId) | ||
reply.header('cache-control', `public, max-age=${60 /* 1min */}`) | ||
|
||
if (!walkerState) { | ||
return reply.send({ | ||
providerId, | ||
ingestionStatus: 'Unknown provider ID' | ||
}) | ||
} | ||
|
||
return reply.send({ | ||
providerId, | ||
// Discussion point: | ||
// We don't have providerAddress in the walker state. | ||
// Is it a problem if our observability API does not tell the provider address? | ||
ingestionStatus: walkerState.status, | ||
lastHeadWalkedFrom: walkerState.lastHead ?? walkerState.head, | ||
adsMissingPieceCID: walkerState.adsMissingPieceCID ?? 0, | ||
entriesNotRetrievable: walkerState.entriesNotRetrievable ?? 0, | ||
piecesIndexed: await repository.countPiecesIndexed(providerId) | ||
}) | ||
}) | ||
|
||
return app | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
export { RedisRepository as Repository } from '@filecoin-station/spark-piece-indexer-repository' | ||
|
||
export interface Logger { | ||
info: typeof console.info; | ||
error: typeof console.error; | ||
request: typeof console.info; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.