From da0b3eb8f454de697b88a59935d96103bc8ff3d4 Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 12 Feb 2026 13:19:04 +0700 Subject: [PATCH 1/2] add problem 4 for challenge --- .DS_Store | Bin 0 -> 6148 bytes src/problem4/.env | 0 src/problem4/README.md | 36 +++++++++++++++++++++++++ src/problem4/api/Dockerfile | 9 +++++++ src/problem4/api/package.json | 9 +++++++ src/problem4/api/src/index.js | 35 ++++++++++++++++++++++++ src/problem4/docker-compose.yml | 28 +++++++++++++++++++ src/problem4/nginx/conf.d/default.conf | 12 +++++++++ src/problem4/nginx/nginx.conf | 0 src/problem4/postgres/init.sql | 1 + 10 files changed, 130 insertions(+) create mode 100644 .DS_Store create mode 100644 src/problem4/.env create mode 100644 src/problem4/README.md create mode 100644 src/problem4/api/Dockerfile create mode 100644 src/problem4/api/package.json create mode 100644 src/problem4/api/src/index.js create mode 100644 src/problem4/docker-compose.yml create mode 100644 src/problem4/nginx/conf.d/default.conf create mode 100644 src/problem4/nginx/nginx.conf create mode 100644 src/problem4/postgres/init.sql diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6e2fe72c73b42d813d493f6dfd04dc10f9d29c10 GIT binary patch literal 6148 zcmeHK%SyvQ6rE|SO({Ya3SADkEf`xLh?`LB4;ayfN=;1BV9b;xHH%WnT7Sqd@q4^? zW&#$o7P0rj%(>5*%z?~pL z2mJOX%UH@Dg5vx4$5EC$&L?j)TN|5gt8I0yJMT#rUhe1fY~-gmXkAJf2bJyzSJAYX z*xP3^$^9sqrm7$cr;u`c9VMYGMsl8nnX2`4!0K3?iQQW)hJ(Yt7#=&zzF3?LoW3|1 z9xaz0YiD=w^kVWHza;Wa)5(EtC0hn7cn8I-=G9A+SSF9)sj{mqLSldzAO?tm^=81F z3s!f%X`s~;1H?cL1Gqm3Xo#-CQlr{Bpu_7k##@LepyOKtQ5bX$mKq@f!gVR2F6HKl z!F4(Kg~@XbmKt?A<7#FY$IM(kUbvba{6eKO?rNl-7$62#8E9(L#`FIiewo@w{%Q$X z!~iky&lupXu{R#UqRiR)V|jSi3TXGxP%y7V1qAe!O8^+SkMvYf#|7$;=Nc?E;wb1> Q<$!b%P=ruN4EzEEU(@JGfdBvi literal 0 HcmV?d00001 diff --git a/src/problem4/.env b/src/problem4/.env new file mode 100644 index 0000000..e69de29 diff --git a/src/problem4/README.md b/src/problem4/README.md new file mode 100644 index 0000000..e82293d --- /dev/null +++ b/src/problem4/README.md @@ -0,0 +1,36 @@ +# Problem 4 + +A small platform has been deployed using Docker Compose. +Users are reporting that the API is unreliable and sometimes inaccessible. + +Your task is to investigate and stabilize the system. + +## Setup + +Requirements: + +* Docker +* Docker Compose + +Run: + +docker compose up --build + +The application will be available at: +[http://localhost:8080](http://localhost:8080) + +## Your Tasks + +1. Identify the issues affecting the system +2. Fix them +3. Explain the root causes + +## Submission + +Submit a short report including: + +* What problems you found +* How you diagnosed them +* The fixes you applied +* What monitoring/alerts you would add +* How you would prevent this in production diff --git a/src/problem4/api/Dockerfile b/src/problem4/api/Dockerfile new file mode 100644 index 0000000..6f65f06 --- /dev/null +++ b/src/problem4/api/Dockerfile @@ -0,0 +1,9 @@ +FROM node:20-alpine + +WORKDIR /app +COPY package.json . +RUN npm install + +COPY src ./src + +CMD ["node", "src/index.js"] \ No newline at end of file diff --git a/src/problem4/api/package.json b/src/problem4/api/package.json new file mode 100644 index 0000000..a954315 --- /dev/null +++ b/src/problem4/api/package.json @@ -0,0 +1,9 @@ +{ + "name": "api", + "version": "1.0.0", + "dependencies": { + "express": "^4.18.2", + "pg": "^8.11.3", + "ioredis": "^5.3.2" + } +} diff --git a/src/problem4/api/src/index.js b/src/problem4/api/src/index.js new file mode 100644 index 0000000..a6f466d --- /dev/null +++ b/src/problem4/api/src/index.js @@ -0,0 +1,35 @@ +const express = require("express"); +const { Pool } = require("pg"); +const Redis = require("ioredis"); + +const app = express(); + +const pool = new Pool({ + host: process.env.DB_HOST, + user: "postgres", + password: "postgres", + database: "postgres", + port: 5432, +}); + +const redis = new Redis({ host: process.env.REDIS_HOST, port: 6379 }); + +app.get("/api/users", async (req, res) => { + try { + const db = await pool.connect(); + const result = await db.query("SELECT NOW()"); + db.release(); + + await redis.set("last_call", Date.now()); + res.json({ ok: true, time: result.rows[0] }); + } catch (err) { + console.error(err); + res.status(500).json({ ok: false, error: err.message }); + } +}); + +app.get("/status", (req, res) => { + res.json({ status: "ok" }); +}); + +app.listen(3000, () => console.log("API running on 3000")); diff --git a/src/problem4/docker-compose.yml b/src/problem4/docker-compose.yml new file mode 100644 index 0000000..11bd063 --- /dev/null +++ b/src/problem4/docker-compose.yml @@ -0,0 +1,28 @@ +version: "3.9" + +services: + nginx: + image: nginx:1.25 + ports: + - "8080:80" + volumes: + - ./nginx/conf.d:/etc/nginx/conf.d + depends_on: + - api + + api: + build: ./api + environment: + - DB_HOST=postgres + - REDIS_HOST=redis + depends_on: + - postgres + - redis + + postgres: + image: postgres:15 + environment: + POSTGRES_PASSWORD: postgres + + redis: + image: redis:7 diff --git a/src/problem4/nginx/conf.d/default.conf b/src/problem4/nginx/conf.d/default.conf new file mode 100644 index 0000000..578be2f --- /dev/null +++ b/src/problem4/nginx/conf.d/default.conf @@ -0,0 +1,12 @@ +server { + listen 80; + + location = / { + return 200 "Welcome to the platform\n"; + } + + location /api/ { + proxy_pass http://api:3001; + } + +} diff --git a/src/problem4/nginx/nginx.conf b/src/problem4/nginx/nginx.conf new file mode 100644 index 0000000..e69de29 diff --git a/src/problem4/postgres/init.sql b/src/problem4/postgres/init.sql new file mode 100644 index 0000000..2b36506 --- /dev/null +++ b/src/problem4/postgres/init.sql @@ -0,0 +1 @@ +ALTER SYSTEM SET max_connections = 20; From 0da3d9c9436ae1b31090230793f760c742a745bb Mon Sep 17 00:00:00 2001 From: Dan-S5Tech Date: Thu, 12 Feb 2026 13:27:05 +0700 Subject: [PATCH 2/2] Delete .DS_Store --- .DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 6e2fe72c73b42d813d493f6dfd04dc10f9d29c10..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%SyvQ6rE|SO({Ya3SADkEf`xLh?`LB4;ayfN=;1BV9b;xHH%WnT7Sqd@q4^? zW&#$o7P0rj%(>5*%z?~pL z2mJOX%UH@Dg5vx4$5EC$&L?j)TN|5gt8I0yJMT#rUhe1fY~-gmXkAJf2bJyzSJAYX z*xP3^$^9sqrm7$cr;u`c9VMYGMsl8nnX2`4!0K3?iQQW)hJ(Yt7#=&zzF3?LoW3|1 z9xaz0YiD=w^kVWHza;Wa)5(EtC0hn7cn8I-=G9A+SSF9)sj{mqLSldzAO?tm^=81F z3s!f%X`s~;1H?cL1Gqm3Xo#-CQlr{Bpu_7k##@LepyOKtQ5bX$mKq@f!gVR2F6HKl z!F4(Kg~@XbmKt?A<7#FY$IM(kUbvba{6eKO?rNl-7$62#8E9(L#`FIiewo@w{%Q$X z!~iky&lupXu{R#UqRiR)V|jSi3TXGxP%y7V1qAe!O8^+SkMvYf#|7$;=Nc?E;wb1> Q<$!b%P=ruN4EzEEU(@JGfdBvi