From 94313c030030c953f8fb938c24277e230f7beda2 Mon Sep 17 00:00:00 2001 From: Shredder <110225819+EmperorOrokuSaki@users.noreply.github.com> Date: Fri, 9 Jun 2023 22:19:10 +0330 Subject: [PATCH] feat: verify hostname (load free certificate call to bunny cdn) (#271) * feat: initialize the handler and function. * feat: add call to the bunny cdn api. * refactor: rename access points to apps. * refactor: remove redundant catch and throw Co-authored-by: Felipe Mendes --------- Co-authored-by: Felipe Mendes --- serverless/serverless.yaml | 10 ++++++- serverless/src/functions/apps/handler.ts | 35 ++++++++++++++++++++++++ serverless/src/functions/apps/index.ts | 12 ++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/serverless/serverless.yaml b/serverless/serverless.yaml index bc43573..3be6f3c 100644 --- a/serverless/serverless.yaml +++ b/serverless/serverless.yaml @@ -92,4 +92,12 @@ functions: layers: - { Ref: TopicAwsNodeModulesLambdaLayer } - { Ref: TopicAwsLibsLambdaLayer } - - { Ref: TopicPrismaAwsPrismaClientLambdaLayer } \ No newline at end of file + - { Ref: TopicPrismaAwsPrismaClientLambdaLayer } + + verifyAccessPoint: + handler: src/functions/apps/handler.verifyApp + events: + - http: + path: verifyApp + method: post + cors: true \ No newline at end of file diff --git a/serverless/src/functions/apps/handler.ts b/serverless/src/functions/apps/handler.ts index d9b147f..b2e750d 100644 --- a/serverless/src/functions/apps/handler.ts +++ b/serverless/src/functions/apps/handler.ts @@ -7,8 +7,43 @@ import { BunnyCdn, BunnyCdnError, CreatePullZoneMethodArgs, + LoadFreeCertificateMethodArgs, } from '@libs/bunnyCDN'; +export const verifyApp = async ( + event: APIGatewayEvent +): Promise => { + try { + // Check the parameters and environment variables + dotenv.config(); + if (event.body === null || process.env.BUNNY_CDN_ACCESS_KEY == undefined) { + return formatJSONResponse({ + status: 422, + message: 'Required parameters were not passed.', + }); + } + + // Set up constants + const bunnyCdn = new BunnyCdn(process.env.BUNNY_CDN_ACCESS_KEY); + const hostname = JSON.parse(event.body).hostname; + + let args: LoadFreeCertificateMethodArgs = { + hostname, + }; + + await bunnyCdn.loadFreeCertificate(args); + + return formatJSONResponse({ + status: true, + }); + } catch (e) { + return formatJSONResponse({ + status: 500, + message: e, + }); + } +}; + export const submitAppInfo = async ( event: APIGatewayEvent ): Promise => { diff --git a/serverless/src/functions/apps/index.ts b/serverless/src/functions/apps/index.ts index 5db01f0..e9388fe 100644 --- a/serverless/src/functions/apps/index.ts +++ b/serverless/src/functions/apps/index.ts @@ -1,5 +1,17 @@ import { handlerPath } from '@libs/handler-resolver'; +export const verifyApp = { + handler: `${handlerPath(__dirname)}/handler.verifyApp`, + events: [ + { + http: { + method: 'post', + path: 'verifyApp', + }, + }, + ], +}; + export const submitAppInfo = { handler: `${handlerPath(__dirname)}/handler.submitAppInfo`, events: [