Skip to content

Commit 5e5677a

Browse files
Update index.js
1 parent 2891e35 commit 5e5677a

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

index.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
11
// index.js
22
const express = require('express');
3+
const mysql = require('mysql2/promise');
34
const app = express();
45

6+
// Create MySQL pool
7+
const pool = mysql.createPool({
8+
host: process.env.DB_HOST || 'localhost',
9+
user: process.env.DB_USER || 'root',
10+
password: process.env.DB_PASSWORD || 'password',
11+
database: process.env.DB_NAME || 'testdb',
12+
waitForConnections: true,
13+
connectionLimit: 10
14+
});
15+
516
// Root endpoint
6-
app.get('/', (req, res) => {
7-
res.json({ response: "deploy service with docker compose" });
17+
app.get('/', async (req, res) => {
18+
try {
19+
const [rows] = await pool.query('SELECT NOW() as time');
20+
res.json({
21+
message: "deploy service with docker compose",
22+
db_time: rows[0].time
23+
});
24+
} catch (error) {
25+
res.status(500).json({ error: "Database not connected" });
26+
}
827
});
928

10-
// /will endpoint
1129
app.get('/will', (req, res) => {
1230
res.json({ response: "Welcome" });
1331
});
1432

15-
// /ready endpoint
16-
app.get('/ready', (req, res) => {
17-
res.json({ response: "Great!, It works!" });
33+
app.get('/ready', async (req, res) => {
34+
try {
35+
await pool.query('SELECT 1');
36+
res.json({ status: "Database Ready" });
37+
} catch (err) {
38+
res.status(500).json({ status: "Database Not Ready" });
39+
}
1840
});
1941

20-
// Use PORT from environment or default 8080
2142
const PORT = process.env.PORT || 8080;
2243

23-
// Bind to 0.0.0.0 to expose outside container
2444
app.listen(PORT, '0.0.0.0', () => {
2545
console.log(`Server running on port ${PORT}`);
2646
});

0 commit comments

Comments
 (0)