修复Prometheus连接错误的问题
This commit is contained in:
@@ -154,10 +154,11 @@ app.get('/api/sources', async (req, res) => {
|
|||||||
|
|
||||||
// Add a new Prometheus source
|
// Add a new Prometheus source
|
||||||
app.post('/api/sources', async (req, res) => {
|
app.post('/api/sources', async (req, res) => {
|
||||||
const { name, url, description } = req.body;
|
let { name, url, description } = req.body;
|
||||||
if (!name || !url) {
|
if (!name || !url) {
|
||||||
return res.status(400).json({ error: 'Name and URL are required' });
|
return res.status(400).json({ error: 'Name and URL are required' });
|
||||||
}
|
}
|
||||||
|
if (!/^https?:\/\//i.test(url)) url = 'http://' + url;
|
||||||
try {
|
try {
|
||||||
const [result] = await db.query(
|
const [result] = await db.query(
|
||||||
'INSERT INTO prometheus_sources (name, url, description) VALUES (?, ?, ?)',
|
'INSERT INTO prometheus_sources (name, url, description) VALUES (?, ?, ?)',
|
||||||
@@ -173,7 +174,8 @@ app.post('/api/sources', async (req, res) => {
|
|||||||
|
|
||||||
// Update a Prometheus source
|
// Update a Prometheus source
|
||||||
app.put('/api/sources/:id', async (req, res) => {
|
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 {
|
try {
|
||||||
await db.query(
|
await db.query(
|
||||||
'UPDATE prometheus_sources SET name = ?, url = ?, description = ? WHERE id = ?',
|
'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
|
// Test connection to a Prometheus source
|
||||||
app.post('/api/sources/test', async (req, res) => {
|
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 {
|
try {
|
||||||
const version = await prometheusService.testConnection(url);
|
const version = await prometheusService.testConnection(url);
|
||||||
res.json({ status: 'ok', version });
|
res.json({ status: 'ok', version });
|
||||||
|
|||||||
@@ -6,8 +6,12 @@ const QUERY_TIMEOUT = 10000;
|
|||||||
* Create an axios instance for a given Prometheus URL
|
* Create an axios instance for a given Prometheus URL
|
||||||
*/
|
*/
|
||||||
function createClient(baseUrl) {
|
function createClient(baseUrl) {
|
||||||
|
let url = baseUrl.replace(/\/+$/, '');
|
||||||
|
if (!/^https?:\/\//i.test(url)) {
|
||||||
|
url = 'http://' + url;
|
||||||
|
}
|
||||||
return axios.create({
|
return axios.create({
|
||||||
baseURL: baseUrl.replace(/\/+$/, ''),
|
baseURL: url,
|
||||||
timeout: QUERY_TIMEOUT
|
timeout: QUERY_TIMEOUT
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user