Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | /** * Routes Status Page — Dashboard de statut public. * * Routes protegees (CRUD) : * GET /status-pages — Lister mes pages de statut * POST /status-pages — Creer une page * PATCH /status-pages/:id — Modifier une page * DELETE /status-pages/:id — Supprimer une page * POST /status-pages/:id/monitors — Ajouter un monitor * DELETE /status-pages/:id/monitors/:mid — Retirer un monitor * * Route publique (pas d'auth) : * GET /status-pages/public/:slug — Voir la page publique * * @module routes/statusPage */ const express = require('express'); const { pool: db } = require('../db'); const { requireAuth } = require('../middleware/auth'); const { checkQuota } = require('../middleware/planLimits'); const { makeDeleteHandler } = require('../helpers/routeHelpers'); const { requireGroupContext, requireGroupPermission } = require('../middleware/groupContext'); const router = express.Router(); // Regex pour les slugs (alphanumérique + tirets, 3-50 caractères) const SLUG_REGEX = /^[a-z0-9][a-z0-9-]{1,48}[a-z0-9]$/; // Regex pour les couleurs hex const COLOR_REGEX = /^#[0-9a-fA-F]{6}$/; // ─── Route PUBLIQUE (sans auth) — DOIT etre avant requireAuth ─────────────── router.get('/public/:slug', async (req, res, next) => { try { const { slug } = req.params; // Valider le slug if (!slug || !SLUG_REGEX.test(slug)) { return res.status(400).json({ error: 'Slug invalide.' }); } // Recuperer la page const pageResult = await db.query( `SELECT id, title, description, logo_url, banner_url, theme, custom_css, accent_color, bg_color, text_color, show_uptime, show_latency FROM status_pages WHERE slug = $1 AND enabled = TRUE`, [slug], ); if (pageResult.rows.length === 0) { return res.status(404).json({ error: 'Page de statut introuvable.' }); } const page = pageResult.rows[0]; // Recuperer les monitors avec leur statut actuel const monitorsResult = await db.query( `SELECT spm.label, spm.sort_order, um.url, um.label AS monitor_label, (SELECT status FROM uptime_history WHERE monitor_id = um.id ORDER BY created_at DESC LIMIT 1) AS last_status, (SELECT latency_ms FROM uptime_history WHERE monitor_id = um.id ORDER BY created_at DESC LIMIT 1) AS last_latency, (SELECT ROUND( COUNT(*) FILTER (WHERE status BETWEEN 200 AND 399) * 100.0 / GREATEST(COUNT(*), 1), 1 ) FROM uptime_history WHERE monitor_id = um.id AND created_at > NOW() - INTERVAL '24 hours') AS uptime_24h, (SELECT ROUND( COUNT(*) FILTER (WHERE status BETWEEN 200 AND 399) * 100.0 / GREATEST(COUNT(*), 1), 1 ) FROM uptime_history WHERE monitor_id = um.id AND created_at > NOW() - INTERVAL '30 days') AS uptime_30d, (SELECT ROUND(AVG(latency_ms)) FROM uptime_history WHERE monitor_id = um.id AND created_at > NOW() - INTERVAL '1 hour' AND status BETWEEN 200 AND 399) AS avg_latency_1h FROM status_page_monitors spm JOIN uptime_monitors um ON um.id = spm.monitor_id WHERE spm.status_page_id = $1 AND um.enabled = TRUE ORDER BY spm.sort_order, spm.created_at`, [page.id], ); // Calculer le statut global const monitors = monitorsResult.rows.map((m) => { const httpStatus = (m.last_status >= 200 && m.last_status < 400) ? 'up' : 'down'; return { label: m.label || m.monitor_label, status: m.last_status ? httpStatus : 'unknown', uptime_24h: m.uptime_24h, uptime_30d: m.uptime_30d, latency: m.last_latency, avg_latency: m.avg_latency_1h, }; }); const allUp = monitors.every((m) => m.status === 'up'); const someDown = monitors.some((m) => m.status === 'down'); let globalStatus; if (monitors.length === 0) { globalStatus = 'unknown'; } else if (allUp) { globalStatus = 'operational'; } else if (someDown) { globalStatus = 'degraded'; } else { globalStatus = 'unknown'; } res.json({ page: { title: page.title, description: page.description, logo_url: page.logo_url, banner_url: page.banner_url, theme: page.theme, custom_css: page.custom_css, accent_color: page.accent_color, bg_color: page.bg_color, text_color: page.text_color, show_uptime: page.show_uptime, show_latency: page.show_latency, }, global_status: globalStatus, monitors, }); } catch (err) { next(err); } }); // ─── Routes protegees ─────────────────────────────────────────────────────── router.use(requireAuth); router.use(requireGroupContext()); // ─── GET /status-pages ────────────────────────────────────────────────────── router.get('/', requireGroupPermission('status_pages', 'read'), async (req, res, next) => { try { const { rows } = await db.query( `SELECT sp.*, (SELECT COUNT(*) FROM status_page_monitors WHERE status_page_id = sp.id) AS monitor_count FROM status_pages sp WHERE sp.user_id = $1 ORDER BY sp.created_at`, [req.user.id], ); res.json(rows); } catch (err) { next(err); } }); // ─── POST /status-pages ───────────────────────────────────────────────────── router.post('/', requireGroupPermission('status_pages', 'read'), checkQuota('status_pages', 'status_pages'), async (req, res, next) => { try { const { slug, title, description, logo_url, banner_url, theme, custom_css, accent_color, bg_color, text_color, show_uptime, show_latency } = req.body; if (!slug || !SLUG_REGEX.test(slug)) { return res.status(400).json({ error: 'Slug invalide (3-50 caracteres, minuscules, chiffres et tirets).' }); } if (!title || typeof title !== 'string' || title.length > 200) { return res.status(400).json({ error: 'Titre requis (max 200 caracteres).' }); } if (accent_color && !COLOR_REGEX.test(accent_color)) { return res.status(400).json({ error: 'Couleur d\'accentuation invalide (format #RRGGBB).' }); } // Verifier l'unicite du slug const existing = await db.query('SELECT id FROM status_pages WHERE slug = $1', [slug]); if (existing.rows.length > 0) { return res.status(409).json({ error: 'Ce slug est deja utilise.' }); } const { rows } = await db.query( `INSERT INTO status_pages (user_id, slug, title, description, logo_url, banner_url, theme, custom_css, accent_color, bg_color, text_color, show_uptime, show_latency) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING *`, [ req.user.id, slug, title, description || null, logo_url || null, banner_url || null, theme || 'default', custom_css || null, accent_color || '#6366f1', bg_color || '#0f172a', text_color || '#f3f4f6', show_uptime !== false, show_latency !== false, ], ); res.status(201).json(rows[0]); } catch (err) { next(err); } }); // ─── PATCH /status-pages/:id ──────────────────────────────────────────────── router.patch('/:id', requireGroupPermission('status_pages', 'read'), async (req, res, next) => { try { const existing = await db.query( 'SELECT * FROM status_pages WHERE id = $1 AND user_id = $2', [req.params.id, req.user.id], ); if (existing.rows.length === 0) { return res.status(404).json({ error: 'Page introuvable.' }); } const page = existing.rows[0]; const { title, description, logo_url, banner_url, theme, custom_css, accent_color, bg_color, text_color, show_uptime, show_latency, enabled } = req.body; if (accent_color && !COLOR_REGEX.test(accent_color)) { return res.status(400).json({ error: 'Couleur invalide.' }); } const { rows } = await db.query( `UPDATE status_pages SET title = $1, description = $2, logo_url = $3, banner_url = $4, theme = $5, custom_css = $6, accent_color = $7, bg_color = $8, text_color = $9, show_uptime = $10, show_latency = $11, enabled = $12 WHERE id = $13 AND user_id = $14 RETURNING *`, [ title ?? page.title, description === undefined ? page.description : description, logo_url === undefined ? page.logo_url : logo_url, banner_url === undefined ? page.banner_url : banner_url, theme || page.theme || 'default', custom_css === undefined ? page.custom_css : custom_css, accent_color || page.accent_color, bg_color || page.bg_color, text_color || page.text_color, show_uptime == null ? page.show_uptime : Boolean(show_uptime), show_latency == null ? page.show_latency : Boolean(show_latency), enabled == null ? page.enabled : Boolean(enabled), req.params.id, req.user.id, ], ); res.json(rows[0]); } catch (err) { next(err); } }); // ─── DELETE /status-pages/:id ─────────────────────────────────────────────── router.delete('/:id', requireGroupPermission('status_pages', 'read'), makeDeleteHandler('status_pages', 'Page introuvable.', { message: 'Page de statut supprimee.' })); // ─── POST /status-pages/:id/monitors — ajouter un monitor ────────────────── router.post('/:id/monitors', requireGroupPermission('status_pages', 'read'), async (req, res, next) => { try { // Verifier la propriete de la page const pageCheck = await db.query( 'SELECT id FROM status_pages WHERE id = $1 AND user_id = $2', [req.params.id, req.user.id], ); if (pageCheck.rows.length === 0) { return res.status(404).json({ error: 'Page introuvable.' }); } const { monitor_id, label, sort_order = 0 } = req.body; if (!monitor_id) { return res.status(400).json({ error: 'monitor_id requis.' }); } // Verifier que le monitor appartient a l'utilisateur const monitorCheck = await db.query( 'SELECT id FROM uptime_monitors WHERE id = $1 AND user_id = $2', [monitor_id, req.user.id], ); if (monitorCheck.rows.length === 0) { return res.status(404).json({ error: 'Monitor introuvable.' }); } // Eviter les doublons const dupCheck = await db.query( 'SELECT id FROM status_page_monitors WHERE status_page_id = $1 AND monitor_id = $2', [req.params.id, monitor_id], ); if (dupCheck.rows.length > 0) { return res.status(409).json({ error: 'Ce monitor est deja sur cette page.' }); } const { rows } = await db.query( `INSERT INTO status_page_monitors (status_page_id, monitor_id, label, sort_order) VALUES ($1, $2, $3, $4) RETURNING *`, [req.params.id, monitor_id, label || null, Number.parseInt(sort_order) || 0], ); res.status(201).json(rows[0]); } catch (err) { next(err); } }); // ─── DELETE /status-pages/:id/monitors/:mid ───────────────────────────────── router.delete('/:id/monitors/:mid', requireGroupPermission('status_pages', 'read'), async (req, res, next) => { try { const pageCheck = await db.query( 'SELECT id FROM status_pages WHERE id = $1 AND user_id = $2', [req.params.id, req.user.id], ); if (pageCheck.rows.length === 0) { return res.status(404).json({ error: 'Page introuvable.' }); } const result = await db.query( 'DELETE FROM status_page_monitors WHERE id = $1 AND status_page_id = $2 RETURNING id', [req.params.mid, req.params.id], ); if (result.rows.length === 0) { return res.status(404).json({ error: 'Monitor introuvable sur cette page.' }); } res.json({ message: 'Monitor retire de la page.' }); } catch (err) { next(err); } }); // ─── GET /status-pages/:id/monitors — lister les monitors d'une page ─────── router.get('/:id/monitors', requireGroupPermission('status_pages', 'read'), async (req, res, next) => { try { const pageCheck = await db.query( 'SELECT id FROM status_pages WHERE id = $1 AND user_id = $2', [req.params.id, req.user.id], ); if (pageCheck.rows.length === 0) { return res.status(404).json({ error: 'Page introuvable.' }); } const { rows } = await db.query( `SELECT spm.id, spm.monitor_id, spm.label, spm.sort_order, um.url, um.label AS monitor_label, um.enabled FROM status_page_monitors spm JOIN uptime_monitors um ON um.id = spm.monitor_id WHERE spm.status_page_id = $1 ORDER BY spm.sort_order, spm.created_at`, [req.params.id], ); res.json(rows); } catch (err) { next(err); } }); module.exports = router; |