修复Prometheus连接错误的问题
This commit is contained in:
@@ -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 });
|
||||
|
||||
@@ -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
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user