From a46285c592b5df9e4d90149d8c2c3848f9590b31 Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Sat, 4 Apr 2026 17:17:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DPrometheus=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/index.js | 9 ++++++--- server/prometheus-service.js | 6 +++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/server/index.js b/server/index.js index 9f961db..3945886 100644 --- a/server/index.js +++ b/server/index.js @@ -154,10 +154,11 @@ app.get('/api/sources', async (req, res) => { // Add a new Prometheus source app.post('/api/sources', async (req, res) => { - const { name, url, description } = req.body; + let { name, url, description } = req.body; if (!name || !url) { return res.status(400).json({ error: 'Name and URL are required' }); } + if (!/^https?:\/\//i.test(url)) url = 'http://' + url; try { const [result] = await db.query( 'INSERT INTO prometheus_sources (name, url, description) VALUES (?, ?, ?)', @@ -173,7 +174,8 @@ app.post('/api/sources', async (req, res) => { // Update a Prometheus source app.put('/api/sources/:id', async (req, res) => { - const { name, url, description } = req.body; + let { name, url, description } = req.body; + if (url && !/^https?:\/\//i.test(url)) url = 'http://' + url; try { await db.query( 'UPDATE prometheus_sources SET name = ?, url = ?, description = ? WHERE id = ?', @@ -200,7 +202,8 @@ app.delete('/api/sources/:id', async (req, res) => { // Test connection to a Prometheus source app.post('/api/sources/test', async (req, res) => { - const { url } = req.body; + let { url } = req.body; + if (url && !/^https?:\/\//i.test(url)) url = 'http://' + url; try { const version = await prometheusService.testConnection(url); res.json({ status: 'ok', version }); diff --git a/server/prometheus-service.js b/server/prometheus-service.js index 6b10280..0f3dd07 100644 --- a/server/prometheus-service.js +++ b/server/prometheus-service.js @@ -6,8 +6,12 @@ const QUERY_TIMEOUT = 10000; * Create an axios instance for a given Prometheus URL */ function createClient(baseUrl) { + let url = baseUrl.replace(/\/+$/, ''); + if (!/^https?:\/\//i.test(url)) { + url = 'http://' + url; + } return axios.create({ - baseURL: baseUrl.replace(/\/+$/, ''), + baseURL: url, timeout: QUERY_TIMEOUT }); }