Skip to content

Commit

Permalink
feat: proxy nextjs with fastify
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Jul 5, 2024
1 parent 5d481d9 commit ea38eb6
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ RUN npm ci --prefix server
COPY . .

ARG NEXT_PUBLIC_API_BASE_PATH=/api
ARG SERVER_PORT=31577
ENV NEXT_PUBLIC_API_BASE_PATH=$NEXT_PUBLIC_API_BASE_PATH
ENV SERVER_PORT=$SERVER_PORT
ENV SERVER_PORT=$PORT

ARG NEXT_PUBLIC_COGNITO_POOL_ENDPOINT
ARG COGNITO_USER_POOL_ID
Expand Down
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dev:aspida": "aspida --watch",
"dev:path": "pathpida --ignorePath .gitignore -s -w",
"build": "npm run generate && next build",
"start": "next start",
"start": "sh -c \"next start -p $(($PORT + 1))\"",
"lint": "run-p lint:js lint:prettier lint:style",
"lint:js": "eslint --ext .ts,.tsx,.js --ignore-path .gitignore .",
"lint:prettier": "prettier --check \"./**/*.{ts,tsx,js}\" --ignore-path .gitignore",
Expand Down
123 changes: 115 additions & 8 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@fastify/cookie": "^9.3.1",
"@fastify/etag": "^5.2.0",
"@fastify/helmet": "^11.1.1",
"@fastify/http-proxy": "^9.5.0",
"@fastify/jwt": "^8.0.1",
"@prisma/client": "^5.13.0",
"aspida": "^1.14.0",
Expand Down
10 changes: 10 additions & 0 deletions server/service/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cookie from '@fastify/cookie';
import fastifyEtag from '@fastify/etag';
import helmet from '@fastify/helmet';
import fastifyHttpProxy from '@fastify/http-proxy';
import type { TokenOrHeader } from '@fastify/jwt';
import fastifyJwt from '@fastify/jwt';
import assert from 'assert';
Expand All @@ -14,6 +15,7 @@ import {
COGNITO_POOL_ENDPOINT,
COGNITO_USER_POOL_CLIENT_ID,
COGNITO_USER_POOL_ID,
SERVER_PORT,
} from './envValues';

export const init = (): FastifyInstance => {
Expand All @@ -23,6 +25,7 @@ export const init = (): FastifyInstance => {
fastify.register(helmet);
fastify.register(fastifyEtag, { weak: true });
fastify.register(cookie);

fastify.register(fastifyJwt, {
decoratorName: JWT_PROP_NAME,
cookie: { cookieName: COOKIE_NAME, signed: false },
Expand All @@ -37,6 +40,13 @@ export const init = (): FastifyInstance => {
},
});

fastify.register(fastifyHttpProxy, {
upstream: `http://localhost:${SERVER_PORT + 1}`,
replyOptions: {
rewriteHeaders: (headers) => ({ ...headers, 'content-security-policy': undefined }),
},
});

server(fastify, { basePath: API_BASE_PATH });

return fastify;
Expand Down

0 comments on commit ea38eb6

Please sign in to comment.