Frontend is running
+This page is served by the fe service.
Click the button to call the backend.+
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 @@ + + +
+ + +This page is served by the fe service.
Click the button to call the backend.+