From 7572e07489111e5f7c5d38a379ebee0f49d401b2 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Fri, 3 Jun 2022 14:49:09 -0700 Subject: [PATCH] Add a /health function for ease of benchmarking (#412) * Slightly more defensive API endpoints * Add health endpoint for benchmarking trivial Firebase fns --- functions/src/api.ts | 8 ++++---- functions/src/health.ts | 11 +++++++++++ functions/src/index.ts | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 functions/src/health.ts diff --git a/functions/src/api.ts b/functions/src/api.ts index 31f14257..36b63692 100644 --- a/functions/src/api.ts +++ b/functions/src/api.ts @@ -137,11 +137,11 @@ export const validate = (schema: T, val: unknown) => { export const newEndpoint = (methods: [string], fn: Handler) => functions.runWith({ minInstances: 1 }).https.onRequest(async (req, res) => { - await applyCors(req, res, { - origin: [CORS_ORIGIN_MANIFOLD, CORS_ORIGIN_LOCALHOST], - methods: methods, - }) try { + await applyCors(req, res, { + origin: [CORS_ORIGIN_MANIFOLD, CORS_ORIGIN_LOCALHOST], + methods: methods, + }) if (!methods.includes(req.method)) { const allowed = methods.join(', ') throw new APIError(405, `This endpoint supports only ${allowed}.`) diff --git a/functions/src/health.ts b/functions/src/health.ts new file mode 100644 index 00000000..944f8677 --- /dev/null +++ b/functions/src/health.ts @@ -0,0 +1,11 @@ +import { newEndpoint } from './api' + +export const health = newEndpoint(['GET'], async (_req, [user, _]) => { + return { + message: 'Server is working.', + user: { + id: user.id, + username: user.username, + }, + } +}) diff --git a/functions/src/index.ts b/functions/src/index.ts index f18b6109..81ea59e5 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -3,6 +3,7 @@ import * as admin from 'firebase-admin' admin.initializeApp() // export * from './keep-awake' +export * from './health' export * from './transact' export * from './place-bet' export * from './resolve-market'