-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Webhook Config to DB & updated logic to cache data & reset when…
… config updated (#233)
- Loading branch information
Showing
11 changed files
with
140 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Static, Type } from "@sinclair/typebox"; | ||
import { FastifyInstance } from "fastify"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import { getConfiguration } from "../../../../src/db/configuration/getConfiguration"; | ||
|
||
export const ReplySchema = Type.Object({ | ||
result: Type.Object({ | ||
webhookUrl: Type.String(), | ||
webhookAuthBearerToken: Type.String(), | ||
}), | ||
}); | ||
|
||
export async function getWebhookConfiguration(fastify: FastifyInstance) { | ||
fastify.route<{ | ||
Reply: Static<typeof ReplySchema>; | ||
}>({ | ||
method: "GET", | ||
url: "/configuration/webhook", | ||
schema: { | ||
summary: "Get webhook configuration", | ||
description: "Get the engine configuration for webhook", | ||
tags: ["Configuration"], | ||
operationId: "getWebhookConfiguration", | ||
response: { | ||
[StatusCodes.OK]: ReplySchema, | ||
}, | ||
}, | ||
handler: async (req, res) => { | ||
const config = await getConfiguration(); | ||
res.status(200).send({ | ||
result: { | ||
webhookAuthBearerToken: config.webhookAuthBearerToken || "", | ||
webhookUrl: config.webhookUrl || "", | ||
}, | ||
}); | ||
}, | ||
}); | ||
} |
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,40 @@ | ||
import { Static, Type } from "@sinclair/typebox"; | ||
import { FastifyInstance } from "fastify"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import { updateConfiguration } from "../../../../src/db/configuration/updateConfiguration"; | ||
import { ReplySchema } from "./get"; | ||
|
||
const BodySchema = Type.Partial( | ||
Type.Object({ | ||
webhookUrl: Type.String(), | ||
webhookAuthBearerToken: Type.String(), | ||
}), | ||
); | ||
|
||
export async function updateWebhookConfiguration(fastify: FastifyInstance) { | ||
fastify.route<{ | ||
Body: Static<typeof BodySchema>; | ||
}>({ | ||
method: "POST", | ||
url: "/configuration/webhook", | ||
schema: { | ||
summary: "Update webhook configuration", | ||
description: "Update the engine configuration for webhook", | ||
tags: ["Configuration"], | ||
operationId: "updateWebhookConfiguration", | ||
body: BodySchema, | ||
response: { | ||
[StatusCodes.OK]: ReplySchema, | ||
}, | ||
}, | ||
handler: async (req, res) => { | ||
const config = await updateConfiguration({ ...req.body }); | ||
res.status(200).send({ | ||
result: { | ||
webhookUrl: config.webhookUrl, | ||
webhookAuthBearerToken: config.webhookAuthBearerToken, | ||
}, | ||
}); | ||
}, | ||
}); | ||
} |
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
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,29 @@ | ||
import { getConfiguration } from "../../../src/db/configuration/getConfiguration"; | ||
|
||
interface WebhookConfig { | ||
webhookUrl: string; | ||
webhookAuthBearerToken: string | null; | ||
} | ||
|
||
export const webhookCache = new Map<string, WebhookConfig>(); | ||
|
||
export const getWebhookConfig = async (): Promise<WebhookConfig> => { | ||
const cacheKey = `webhookConfig`; | ||
if (webhookCache.has(cacheKey)) { | ||
return webhookCache.get(cacheKey)! as WebhookConfig; | ||
} | ||
|
||
const config = await getConfiguration(); | ||
|
||
if (config.webhookAuthBearerToken || config.webhookUrl) { | ||
webhookCache.set(cacheKey, { | ||
webhookUrl: config.webhookUrl!, | ||
webhookAuthBearerToken: config.webhookAuthBearerToken, | ||
}); | ||
} | ||
|
||
return { | ||
webhookUrl: config.webhookUrl!, | ||
webhookAuthBearerToken: config.webhookAuthBearerToken, | ||
}; | ||
}; |
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
3 changes: 3 additions & 0 deletions
3
src/prisma/migrations/20231017214123_webhook_config/migration.sql
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,3 @@ | ||
-- AlterTable | ||
ALTER TABLE "configuration" ADD COLUMN "webhookAuthBearerToken" TEXT, | ||
ADD COLUMN "webhookUrl" TEXT; |
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