diff --git a/README.md b/README.md index 6635438..d27b951 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ # test-cicd-1 +Repo demo CI có hai Dockerfile để tạo hai service riêng: + +| Service | Build context | Dockerfile | Port | +| --- | --- | --- | --- | +| `fe` | `fe` | `Dockerfile` | `8080` | +| `be` | `be` | `Dockerfile` | `8080` | + +Trong Console, kết nối repo này, scan branch `main`, chọn cả hai Dockerfile, rồi đặt tên service lần lượt là `fe` và `be`. Cả hai dùng port container `8080`. + +FE có trang đơn giản và gọi `GET /api/hello` qua server FE. Để thử kết nối nội bộ, đặt biến môi trường cho service `fe`: + +```text +BE_URL=http://be:8080/api/hello +``` + +Tên host `be` cần khớp tên service và môi trường phải cho phép FE kết nối tới BE. Mỗi service có endpoint kiểm tra sức khỏe tại `/health`. diff --git a/be/Dockerfile b/be/Dockerfile new file mode 100644 index 0000000..73bb906 --- /dev/null +++ b/be/Dockerfile @@ -0,0 +1,9 @@ +FROM node:22-alpine + +WORKDIR /app +COPY server.js ./ + +ENV PORT=8080 +EXPOSE 8080 +USER node +CMD ["node", "server.js"] diff --git a/be/server.js b/be/server.js new file mode 100644 index 0000000..c767070 --- /dev/null +++ b/be/server.js @@ -0,0 +1,26 @@ +const { createServer } = require("node:http"); + +const port = Number(process.env.PORT || 8080); + +function sendJson(response, status, body) { + response.writeHead(status, { "content-type": "application/json; charset=utf-8" }); + response.end(JSON.stringify(body)); +} + +const server = createServer((request, response) => { + const pathname = new URL(request.url || "/", "http://localhost").pathname; + + if (request.method === "GET" && (pathname === "/" || pathname === "/health")) { + return sendJson(response, 200, { status: "ok", service: "be" }); + } + + if (request.method === "GET" && pathname === "/api/hello") { + return sendJson(response, 200, { service: "be", message: "Hello from the backend" }); + } + + return sendJson(response, 404, { error: "Not found" }); +}); + +server.listen(port, "0.0.0.0", () => { + console.log(`BE listening on port ${port}`); +}); diff --git a/fe/Dockerfile b/fe/Dockerfile new file mode 100644 index 0000000..76d64cb --- /dev/null +++ b/fe/Dockerfile @@ -0,0 +1,9 @@ +FROM node:22-alpine + +WORKDIR /app +COPY server.js index.html ./ + +ENV PORT=8080 +EXPOSE 8080 +USER node +CMD ["node", "server.js"] diff --git a/fe/index.html b/fe/index.html new file mode 100644 index 0000000..29ce179 --- /dev/null +++ b/fe/index.html @@ -0,0 +1,35 @@ + + + + + + FE / BE CI demo + + + +
+

Frontend is running

+

This page is served by the fe service.

+ +
Click the button to call the backend.
+
+ + + diff --git a/fe/server.js b/fe/server.js new file mode 100644 index 0000000..5f4afec --- /dev/null +++ b/fe/server.js @@ -0,0 +1,48 @@ +const { createServer } = require("node:http"); +const { readFile } = require("node:fs/promises"); +const { join } = require("node:path"); + +const port = Number(process.env.PORT || 8080); +const backendUrl = process.env.BE_URL || "http://be:8080/api/hello"; + +function sendJson(response, status, body) { + response.writeHead(status, { "content-type": "application/json; charset=utf-8" }); + response.end(JSON.stringify(body)); +} + +const server = createServer(async (request, response) => { + const pathname = new URL(request.url || "/", "http://localhost").pathname; + + if (request.method === "GET" && pathname === "/health") { + return sendJson(response, 200, { status: "ok", service: "fe" }); + } + + if (request.method === "GET" && pathname === "/api/hello") { + try { + const backendResponse = await fetch(backendUrl, { signal: AbortSignal.timeout(3000) }); + const body = await backendResponse.text(); + response.writeHead(backendResponse.status, { + "content-type": backendResponse.headers.get("content-type") || "application/json; charset=utf-8", + }); + return response.end(body); + } catch { + return sendJson(response, 502, { error: "Backend is unavailable", backendUrl }); + } + } + + if (request.method === "GET" && pathname === "/") { + try { + const html = await readFile(join(__dirname, "index.html")); + response.writeHead(200, { "content-type": "text/html; charset=utf-8" }); + return response.end(html); + } catch { + return sendJson(response, 500, { error: "Frontend page is unavailable" }); + } + } + + return sendJson(response, 404, { error: "Not found" }); +}); + +server.listen(port, "0.0.0.0", () => { + console.log(`FE listening on port ${port}`); +});