修复Prometheus连接错误的问题

This commit is contained in:
CN-JS-HuiBai
2026-04-04 17:17:06 +08:00
parent 7baf628e0e
commit a46285c592
2 changed files with 11 additions and 4 deletions

View File

@@ -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 });

View File

@@ -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
});
}