-
Notifications
You must be signed in to change notification settings - Fork 0
feat(deploy): #65 NestJS serverless handler entrypoint #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cd42caf
chore(deploy): remove Vite-era root vercel.json (#62)
fray-cloud 828920a
feat(deploy): proxy data.go.kr through Next Route Handler (#63)
fray-cloud 927c907
feat(deploy): migrate proxy and types to data.go.kr API v2 (#63)
fray-cloud b610789
chore(deps): drop unused @vercel/remote-nx (#64)
fray-cloud 777191f
feat(deploy): add NestJS serverless handler entrypoint (#65)
fray-cloud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,32 @@ | ||
| // Vercel Node Function entrypoint for the NestJS app. | ||
| // | ||
| // Local dev still uses apps/api/src/main.ts (`nx serve api`). This | ||
| // handler is only consumed by Vercel — it boots Nest once per cold | ||
| // start, caches the underlying express instance in a module-scope | ||
| // variable, and reuses it across warm invocations. | ||
|
|
||
| import { NestFactory } from '@nestjs/core'; | ||
| import { ExpressAdapter } from '@nestjs/platform-express'; | ||
| import serverlessExpress from '@codegenie/serverless-express'; | ||
| import express from 'express'; | ||
| import type { Handler } from 'aws-lambda'; | ||
| import { AppModule } from '../src/app/app.module'; | ||
|
|
||
| let cachedHandler: Handler | undefined; | ||
|
|
||
| async function bootstrap(): Promise<Handler> { | ||
| const expressApp = express(); | ||
| const app = await NestFactory.create(AppModule, new ExpressAdapter(expressApp)); | ||
| app.setGlobalPrefix('api'); | ||
| await app.init(); | ||
| return serverlessExpress({ app: expressApp }); | ||
| } | ||
|
|
||
| const handler: Handler = async (event, context, callback) => { | ||
| if (!cachedHandler) { | ||
| cachedHandler = await bootstrap(); | ||
| } | ||
| return cachedHandler(event, context, callback); | ||
| }; | ||
|
|
||
| export default handler; |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| # Korean public-data-portal service key for the abandonmentPublicSrvc API. | ||
| # Get one at https://www.data.go.kr/ and copy this file to apps/web/.env.local | ||
| # (which is git-ignored). | ||
| # (git-ignored). | ||
| # | ||
| # NEXT_PUBLIC_ prefix = exposed to the browser. A server-side proxy via a | ||
| # Next Route Handler is tracked under #9 (Vercel deploy); that will let us | ||
| # drop the NEXT_PUBLIC_ prefix and keep the key server-only. | ||
| NEXT_PUBLIC_DATA_GO_KR_SERVICE_KEY= | ||
| # Server-only — no NEXT_PUBLIC_ prefix. Read by the Next Route Handler | ||
| # at apps/web/app/api/data-go-kr/[...path]/route.ts; never shipped to | ||
| # the browser bundle. | ||
| DATA_GO_KR_SERVICE_KEY= |
This file contains hidden or 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,45 @@ | ||
| import { NextRequest, NextResponse } from 'next/server'; | ||
|
|
||
| const UPSTREAM_BASE = | ||
| 'https://apis.data.go.kr/1543061/abandonmentPublicService_v2'; | ||
|
|
||
| export async function GET( | ||
| request: NextRequest, | ||
| context: { params: Promise<{ path: string[] }> } | ||
| ) { | ||
| const serviceKey = process.env.DATA_GO_KR_SERVICE_KEY; | ||
| if (!serviceKey) { | ||
| return NextResponse.json( | ||
| { error: 'DATA_GO_KR_SERVICE_KEY is not configured on the server.' }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
|
|
||
| const { path } = await context.params; | ||
| const upstreamPath = path.join('/'); | ||
|
|
||
| const params = new URLSearchParams(request.nextUrl.searchParams); | ||
| params.set('serviceKey', serviceKey); | ||
| params.set('_type', 'json'); | ||
|
|
||
| const upstreamUrl = `${UPSTREAM_BASE}/${upstreamPath}?${params.toString()}`; | ||
|
|
||
| try { | ||
| const upstream = await fetch(upstreamUrl, { | ||
| headers: { Accept: 'application/json' }, | ||
| }); | ||
| const body = await upstream.text(); | ||
| return new NextResponse(body, { | ||
| status: upstream.status, | ||
| headers: { | ||
| 'content-type': | ||
| upstream.headers.get('content-type') ?? 'application/json', | ||
| }, | ||
| }); | ||
| } catch (err) { | ||
| return NextResponse.json( | ||
| { error: 'Upstream fetch failed', message: String(err) }, | ||
| { status: 502 } | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: The
context.paramstype should not be aPromise; usingPromise<{ path: string[] }>is misleading and unnecessary.In Next.js route handlers,
context.paramsis a plain object, not a promise. Typing it asPromise<{ path: string[] }>diverges from the framework’s types, forces unnecessaryawaits, and may break when Next’s typings change.Use a synchronous params type instead: