-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (44 loc) · 1.32 KB
/
index.js
File metadata and controls
50 lines (44 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// index.js
const express = require('express');
const mysql = require('mysql2/promise');
const app = express();
// Create MySQL pool (Cloud SQL)
const pool = mysql.createPool({
host: process.env.DB_HOST,
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
waitForConnections: true,
connectionLimit: 10,
connectTimeout: 10000 // 10 seconds
});
// Root endpoint
app.get('/', async (req, res) => {
try {
const [rows] = await pool.query('SELECT NOW() as time');
res.json({
message: "deploy service with docker compose",
db_time: rows[0].time
});
} catch (error) {
console.error("DB ERROR:", error.message);
res.status(500).json({ error: "Database not connected" });
}
});
app.get('/will', (req, res) => {
res.json({ response: "Welcome" });
});
app.get('/ready', async (req, res) => {
try {
await pool.query('SELECT 1');
res.json({ status: "Database Ready" });
} catch (err) {
console.error("DB READY CHECK FAILED:", err.message);
res.status(500).json({ status: "Database Not Ready" });
}
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server running on port ${PORT}`);
});