Add a /health function for ease of benchmarking (#412)

* Slightly more defensive API endpoints

* Add health endpoint for benchmarking trivial Firebase fns
This commit is contained in:
Marshall Polaris 2022-06-03 14:49:09 -07:00 committed by GitHub
parent 00f142ec7e
commit 7572e07489
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 4 deletions

View File

@ -137,11 +137,11 @@ export const validate = <T extends z.ZodTypeAny>(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}.`)

11
functions/src/health.ts Normal file
View File

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

View File

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