添加初始化页面
This commit is contained in:
116
server/index.js
116
server/index.js
@@ -10,6 +10,122 @@ const PORT = process.env.PORT || 3000;
|
||||
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
const fs = require('fs');
|
||||
|
||||
let isDbInitialized = false;
|
||||
|
||||
async function checkDb() {
|
||||
try {
|
||||
const [rows] = await db.query("SHOW TABLES LIKE 'prometheus_sources'");
|
||||
if (rows.length > 0) {
|
||||
isDbInitialized = true;
|
||||
} else {
|
||||
isDbInitialized = false;
|
||||
}
|
||||
} catch (err) {
|
||||
isDbInitialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
checkDb();
|
||||
|
||||
// Setup API Routes
|
||||
app.post('/api/setup/test', async (req, res) => {
|
||||
const { host, port, user, password } = req.body;
|
||||
try {
|
||||
const mysql = require('mysql2/promise');
|
||||
const connection = await mysql.createConnection({
|
||||
host: host || 'localhost',
|
||||
port: parseInt(port) || 3306,
|
||||
user: user || 'root',
|
||||
password: password || ''
|
||||
});
|
||||
await connection.ping();
|
||||
await connection.end();
|
||||
res.json({ success: true, message: 'Connection successful' });
|
||||
} catch (err) {
|
||||
res.status(400).json({ success: false, error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/setup/init', async (req, res) => {
|
||||
const { host, port, user, password, database } = req.body;
|
||||
try {
|
||||
const mysql = require('mysql2/promise');
|
||||
const connection = await mysql.createConnection({
|
||||
host: host || 'localhost',
|
||||
port: parseInt(port) || 3306,
|
||||
user: user || 'root',
|
||||
password: password || ''
|
||||
});
|
||||
|
||||
const dbName = database || 'display_wall';
|
||||
|
||||
// Create database
|
||||
await connection.query(`CREATE DATABASE IF NOT EXISTS \`${dbName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`);
|
||||
await connection.query(`USE \`${dbName}\``);
|
||||
|
||||
// Create tables
|
||||
await connection.query(`
|
||||
CREATE TABLE IF NOT EXISTS prometheus_sources (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
url VARCHAR(500) NOT NULL,
|
||||
description TEXT DEFAULT '',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||
`);
|
||||
|
||||
await connection.end();
|
||||
|
||||
// Save to .env
|
||||
const envContent = `MYSQL_HOST=${host || 'localhost'}
|
||||
MYSQL_PORT=${port || '3306'}
|
||||
MYSQL_USER=${user || 'root'}
|
||||
MYSQL_PASSWORD=${password || ''}
|
||||
MYSQL_DATABASE=${dbName}
|
||||
PORT=${process.env.PORT || 3000}
|
||||
REFRESH_INTERVAL=${process.env.REFRESH_INTERVAL || 5000}
|
||||
`;
|
||||
fs.writeFileSync(path.join(__dirname, '..', '.env'), envContent);
|
||||
|
||||
// Update process.env
|
||||
process.env.MYSQL_HOST = host;
|
||||
process.env.MYSQL_PORT = port;
|
||||
process.env.MYSQL_USER = user;
|
||||
process.env.MYSQL_PASSWORD = password;
|
||||
process.env.MYSQL_DATABASE = dbName;
|
||||
|
||||
// Re-initialize pool
|
||||
db.initPool();
|
||||
|
||||
isDbInitialized = true;
|
||||
res.json({ success: true, message: 'Initialization complete' });
|
||||
} catch (err) {
|
||||
console.error('Initialization error:', err);
|
||||
res.status(500).json({ success: false, error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Middleware to protect routes
|
||||
app.use((req, res, next) => {
|
||||
if (!isDbInitialized) {
|
||||
if (req.path.startsWith('/api/setup') || req.path === '/init.html' || req.path.startsWith('/css/') || req.path.startsWith('/js/') || req.path.startsWith('/fonts/')) {
|
||||
return next();
|
||||
}
|
||||
if (req.path.startsWith('/api/')) {
|
||||
return res.status(503).json({ error: 'Database not initialized', needSetup: true });
|
||||
}
|
||||
return res.redirect('/init.html');
|
||||
}
|
||||
|
||||
if (req.path === '/init.html') {
|
||||
return res.redirect('/');
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
app.use(express.static(path.join(__dirname, '..', 'public')));
|
||||
|
||||
// ==================== Prometheus Source CRUD ====================
|
||||
|
||||
Reference in New Issue
Block a user