feat: unified portal server, single database migration & railway configuration
This commit is contained in:
@@ -11,22 +11,38 @@ app.use(bodyParser.json());
|
||||
app.use(express.static(__dirname));
|
||||
|
||||
// Create MySQL connection
|
||||
const db = mysql.createConnection({
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
user: process.env.DB_USER || 'root',
|
||||
password: process.env.DB_PASSWORD || '',
|
||||
database: process.env.DB_NAME || 'spbu_gis',
|
||||
port: process.env.DB_PORT || 3306
|
||||
});
|
||||
let db;
|
||||
function handleDisconnect() {
|
||||
const connectionString = process.env.DATABASE_URL || process.env.MYSQL_URL || process.env.MYSQL_PRIVATE_URL;
|
||||
db = connectionString
|
||||
? mysql.createConnection(connectionString)
|
||||
: mysql.createConnection({
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
user: process.env.DB_USER || 'root',
|
||||
password: process.env.DB_PASSWORD || '',
|
||||
database: process.env.DB_NAME || 'spbu_gis',
|
||||
port: process.env.DB_PORT || 3306
|
||||
});
|
||||
|
||||
db.connect(err => {
|
||||
if (err) {
|
||||
console.error('Database connection failed: ' + err.stack);
|
||||
setTimeout(handleDisconnect, 2000);
|
||||
} else {
|
||||
console.log('Connected to MySQL database.');
|
||||
}
|
||||
});
|
||||
|
||||
db.connect(err => {
|
||||
if (err) {
|
||||
console.error('Database connection failed: ' + err.stack);
|
||||
return;
|
||||
}
|
||||
console.log('Connected to MySQL database.');
|
||||
});
|
||||
db.on('error', err => {
|
||||
console.error('Database error:', err);
|
||||
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
|
||||
handleDisconnect();
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
}
|
||||
handleDisconnect();
|
||||
|
||||
// GET route to fetch all SPBU locations
|
||||
app.get('/api/spbus', (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user