From a45a0c8ea01219c61b55203c51a129528032758e Mon Sep 17 00:00:00 2001 From: Alejo Thomas Ortega Date: Fri, 5 Jul 2024 11:20:05 -0300 Subject: [PATCH 1/2] feat: expose set-cursor endpoint --- src/controllers/handlers/set-cursor.ts | 36 ++++++++++++++++++++++++++ src/controllers/routes.ts | 20 ++++++++++++++ src/types.ts | 4 +-- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/controllers/handlers/set-cursor.ts create mode 100644 src/controllers/routes.ts diff --git a/src/controllers/handlers/set-cursor.ts b/src/controllers/handlers/set-cursor.ts new file mode 100644 index 0000000..5d17c66 --- /dev/null +++ b/src/controllers/handlers/set-cursor.ts @@ -0,0 +1,36 @@ +import { HandlerContextWithPath } from '../../types' +import { InvalidRequestError, NotFoundError, parseJson } from '@dcl/platform-server-commons' + +export async function setCursorHandler( + context: Pick< + HandlerContextWithPath<'producerRegistry', '/producers/:producer/set-since'>, + 'params' | 'request' | 'components' + > +) { + try { + context.components.producerRegistry.getProducer(context.params.producer) + } catch (error: any) { + throw new NotFoundError(`Invalid producer: ${context.params.producer}`) + } + + const body = await parseJson(context.request) + if (!body.since) { + throw new InvalidRequestError("Invalid request: missing 'since'.") + } + if (typeof body.since !== 'string') { + throw new InvalidRequestError(`Invalid request: invalid value for 'since': ${body.since}.`) + } + + const sinceDate = new Date(body.since) + if (sinceDate.toString() === 'Invalid Date') { + throw new InvalidRequestError(`Invalid request: invalid value for 'since': ${body.since} (not a date).`) + } + + const producer = context.components.producerRegistry.getProducer(context.params.producer) + await producer.runProducerSinceDate(sinceDate.getTime()) + + return { + status: 204, + body: {} + } +} diff --git a/src/controllers/routes.ts b/src/controllers/routes.ts new file mode 100644 index 0000000..a6223e0 --- /dev/null +++ b/src/controllers/routes.ts @@ -0,0 +1,20 @@ +import { Router } from '@well-known-components/http-server' +import { bearerTokenMiddleware, errorHandler } from '@dcl/platform-server-commons' +import { GlobalContext } from '../types' +import { setCursorHandler } from './handlers/set-cursor' + +// We return the entire router because it will be easier to test than a whole server +export async function setupRouter({ components }: GlobalContext): Promise> { + const router = new Router() + + const { config } = components + + const signingKey = await config.getString('SIGNING_KEY') + if (signingKey) { + router.post('/producers/:producer/set-since', bearerTokenMiddleware(signingKey), setCursorHandler) + } + + router.use(errorHandler) + + return router +} diff --git a/src/types.ts b/src/types.ts index a567566..42cd39a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -19,14 +19,14 @@ export type BaseComponents = { config: IConfigComponent logs: ILoggerComponent metrics: IMetricsComponent + fetch: IFetchComponent + producerRegistry: IProducerRegistry } // components used in runtime export type AppComponents = BaseComponents & { - fetch: IFetchComponent database: DatabaseComponent eventPublisher: IEventPublisher - producerRegistry: IProducerRegistry l2CollectionsSubGraph: ISubgraphComponent landManagerSubGraph: ISubgraphComponent marketplaceSubGraph: ISubgraphComponent From eb988c150d7c9685e2c76ba487594cae240304a3 Mon Sep 17 00:00:00 2001 From: Alejo Thomas Ortega Date: Fri, 5 Jul 2024 11:22:00 -0300 Subject: [PATCH 2/2] chore: limit set-cursor endpoint to zone environment --- src/controllers/routes.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controllers/routes.ts b/src/controllers/routes.ts index a6223e0..f2436a0 100644 --- a/src/controllers/routes.ts +++ b/src/controllers/routes.ts @@ -10,7 +10,8 @@ export async function setupRouter({ components }: GlobalContext): Promise