Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ node_modules
!.env.example
!.env.*.example
**.pem
.vscode
.vscode
.terraform
*.tfvars
28 changes: 8 additions & 20 deletions auth-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
FROM node:22-alpine AS base
FROM node:22-alpine

WORKDIR /app

COPY auth-service/package*.json ./
# Cài curl do alpine không có sẵn
RUN apk add --no-cache curl

FROM base AS development
# Cần chạy build từ /root
COPY auth-service/package*.json ./

RUN npm install

COPY auth-service/ .
COPY shared-libs ./shared-libs

EXPOSE 3001

CMD ["npm", "run", "dev"]

FROM base AS production

RUN npm ci --omit=dev && npm cache clean --force

RUN addgroup --system --gid 1001 nodejs \
&& adduser --system --uid 1001 nodeusr
USER nodeusr

COPY auth-service/ .
COPY shared-libs ./shared-libs

ENV NODE_ENV=production
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:3001/health || exit 1

EXPOSE 3001

CMD ["npm", "start"]
CMD ["npm", "run", "dev"]
25 changes: 25 additions & 0 deletions auth-service/Dockerfile.lambda
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM public.ecr.aws/lambda/nodejs@sha256:ba6eb28e407ede3f5d8ab441aadd4ad7558cfdb099f324f195d1f6ddc885d4bf AS builder

# Lambda workdir chuẩn
WORKDIR /var/task

COPY auth-service/package*.json .

# Không cài devDependencies và xóa cache npm
RUN npm ci --omit=dev && npm cache clean --force

COPY auth-service/ .
COPY shared-libs/ ./shared-libs

FROM public.ecr.aws/lambda/nodejs@sha256:ba6eb28e407ede3f5d8ab441aadd4ad7558cfdb099f324f195d1f6ddc885d4bf AS runner

WORKDIR /var/task

COPY --from=builder /var/task .

# APP_ENV test AWS infra local
ENV NODE_ENV=production
ENV APP_ENV=production

# Lambda handler
CMD ["server.handler"]
4 changes: 0 additions & 4 deletions auth-service/controllers/authController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import dotenv from 'dotenv';
import bcrypt from 'bcrypt';

import TokenRepository from "../repositories/tokenRepository.js";
import UserRepository from '../repositories/userRepository.js';
import { asyncWrapper } from "#shared/middlewares/index.js"
Expand All @@ -14,8 +12,6 @@ import {
} from "#shared/errors/errors.js";
import { createHash, hashToken, generateUuidv4, JwtFacade } from '../utils/index.js';

dotenv.config();

// POST /refresh
// Cấp lại token khi accessToken hết hạn
export const token = asyncWrapper(async (req, res) => {
Expand Down
4 changes: 4 additions & 0 deletions auth-service/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ app.use(express.json());
app.use(cookieParser());
app.use(pinoLogger);

app.get('/health', (req, res) => {
res.status(200).json({ status: 'ok' });
});

app.use('/users', router);

app.use(routeNotFound);
Expand Down
32 changes: 8 additions & 24 deletions class-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,40 +1,24 @@
FROM node:22-alpine AS base
FROM node:22-alpine

WORKDIR /app

# Cài curl do alpine không có sẵn
RUN apk add --no-cache curl

# Cần chạy build từ /root
COPY class-service/package*.json ./

FROM base AS development

RUN npm install

COPY class-service/ .
COPY shared-libs ./shared-libs

# Generate Prisma client cho môi trường Alpine (linux-musl)
# Generate Prisma client
RUN npx prisma generate

EXPOSE 3003

CMD ["npm", "run", "dev"]

FROM base AS production

RUN addgroup --system --gid 1001 nodejs \
&& adduser --system --uid 1001 nodeusr

RUN npm ci --omit=dev && npm cache clean --force

COPY class-service/ .
COPY shared-libs ./shared-libs

RUN npx prisma generate --schema=prisma/schema.prisma

USER nodeusr

ENV NODE_ENV=production
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:3003/health || exit 1

EXPOSE 3003

CMD ["npm", "start"]
CMD ["npm", "run", "dev"]
37 changes: 37 additions & 0 deletions class-service/Dockerfile.lambda
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM public.ecr.aws/lambda/nodejs@sha256:ba6eb28e407ede3f5d8ab441aadd4ad7558cfdb099f324f195d1f6ddc885d4bf AS deps

# Lambda workdir chuẩn
WORKDIR /var/task

# NOTE: Cần chạy build từ root
COPY class-service/package*.json .

RUN npm ci

FROM public.ecr.aws/lambda/nodejs@sha256:ba6eb28e407ede3f5d8ab441aadd4ad7558cfdb099f324f195d1f6ddc885d4bf AS builder

WORKDIR /var/task

COPY --from=deps /var/task/node_modules ./node_modules

COPY class-service/ .
COPY shared-libs ./shared-libs

# Generate Prisma client
RUN npx prisma generate

# Bỏ devDependencies và xóa cache npm
RUN npm prune --omit=dev && npm cache clean --force

FROM public.ecr.aws/lambda/nodejs@sha256:ba6eb28e407ede3f5d8ab441aadd4ad7558cfdb099f324f195d1f6ddc885d4bf AS runner

WORKDIR /var/task

COPY --from=builder /var/task .

# APP_ENV test AWS infra local
ENV NODE_ENV=production
ENV APP_ENV=production

# Lambda handler
CMD ["server.handler"]
8 changes: 3 additions & 5 deletions class-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@
"axios": "^1.11.0",
"bcrypt": "^6.0.0",
"cookie-parser": "^1.4.7",
"dotenv": "^17.2.1",
"express": "^5.1.0",
"fs": "^0.0.1-security",
"ioredis": "^5.7.0",
"joi": "^18.0.1",
"jsonwebtoken": "^9.0.2",
"pino": "^9.9.0",
"pino-http": "^10.5.0",
"prisma": "^6.14.0",
"serverless-http": "^4.0.0",
"uuid": "^11.1.0"
"serverless-http": "^4.0.0"
},
"devDependencies": {
"artillery": "^2.0.25",
"nodemon": "^3.1.10",
"pino-pretty": "^13.1.1"
"pino-pretty": "^13.1.1",
"prisma": "^6.14.0"
}
}
4 changes: 4 additions & 0 deletions class-service/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ app.use(express.json());
app.use(cookieParser());
app.use(pinoLogger);

app.get('/health', (req, res) => {
res.status(200).json({ status: 'ok' });
});

app.use(authentication);

app.use('/classes', classRouter);
Expand Down
Loading