Add error page and index
This commit is contained in:
parent
7cf3bc2b54
commit
ba93fa69f3
13
index.js
13
index.js
|
@ -5,6 +5,8 @@ const SQLiteStore = require('connect-sqlite3')(session);
|
|||
const cors = require("cors");
|
||||
const bcrypt = require('bcrypt');
|
||||
|
||||
var dbReady = false;
|
||||
|
||||
const DSN = `mysql://${process.env.DB_USER || "root"}:${process.env.DB_PASSWORD || ""}@tcp(${process.env.DB_HOST || "127.0.0.1"}:${process.env.DB_PORT || "3306"})/${process.env.DB_NAME}`;
|
||||
const mariadb = require("mariadb");
|
||||
const fs = require('fs');
|
||||
|
@ -49,6 +51,12 @@ app.use(express.static("public"));
|
|||
app.set("view engine", "ejs");
|
||||
app.set("views", "./views");
|
||||
|
||||
app.use((req, res, next) => {
|
||||
if (!dbReady) {
|
||||
return res.render('error', { error: 'Database is not ready. Please try again later.' });
|
||||
}
|
||||
});
|
||||
|
||||
const routersDir = path.join(__dirname, 'routes');
|
||||
fs.readdirSync(routersDir).forEach(file => {
|
||||
const router = require(path.join(routersDir, file));
|
||||
|
@ -58,6 +66,10 @@ fs.readdirSync(routersDir).forEach(file => {
|
|||
}
|
||||
});
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
|
||||
})
|
||||
|
||||
app.listen(port, (err) => {
|
||||
if (err) {
|
||||
global.log.error(`Cannot start server: ${err}`);
|
||||
|
@ -70,6 +82,7 @@ app.listen(port, (err) => {
|
|||
return conn.query('SELECT 1 FROM ACL LIMIT 1')
|
||||
.then(() => {
|
||||
global.log.info('Database connection validated with ACL table.');
|
||||
dbReady = true;
|
||||
conn.release();
|
||||
})
|
||||
.catch(err => {
|
||||
|
|
10
routes/index.js
Normal file
10
routes/index.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
const express = require('express');
|
||||
const db = global.db;
|
||||
const router = express.Router();
|
||||
|
||||
// GET /login
|
||||
router.get('/', (req, res) => {
|
||||
res.send("Test")
|
||||
});
|
||||
|
||||
module.exports = router;
|
18
views/error.ejs
Normal file
18
views/error.ejs
Normal file
|
@ -0,0 +1,18 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Error</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; background: #f8d7da; color: #721c24; padding: 2em; }
|
||||
.error-container { background: #fff; border: 1px solid #f5c6cb; padding: 2em; border-radius: 8px; max-width: 500px; margin: auto; }
|
||||
h1 { margin-top: 0; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="error-container">
|
||||
<h1>Error</h1>
|
||||
<p><%= error %></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue