Add fallback memory store handling for local development server testing
This commit is contained in:
+40
-12
@@ -85,11 +85,23 @@ const DEFAULT_REGIONS = [
|
||||
},
|
||||
];
|
||||
|
||||
// In-memory fallback store for local development when DB is unreachable
|
||||
const memoryStore = {
|
||||
users: {
|
||||
user: { password: "user", role: "user" },
|
||||
admin: { password: "admin", role: "admin" }
|
||||
},
|
||||
regions: DEFAULT_REGIONS,
|
||||
stats: {},
|
||||
settings: {}
|
||||
};
|
||||
let dbConnected = false;
|
||||
|
||||
// Initialize Database Tables
|
||||
async function initDb() {
|
||||
let client;
|
||||
try {
|
||||
console.log("Connecting to Neon PostgreSQL database...");
|
||||
console.log("Connecting to PostgreSQL database...");
|
||||
client = await pool.connect();
|
||||
console.log("Initializing database tables...");
|
||||
await client.query("BEGIN");
|
||||
@@ -153,6 +165,7 @@ async function initDb() {
|
||||
}
|
||||
|
||||
await client.query("COMMIT");
|
||||
dbConnected = true;
|
||||
console.log("Database initialized successfully.");
|
||||
} catch (err) {
|
||||
if (client) {
|
||||
@@ -162,7 +175,8 @@ async function initDb() {
|
||||
console.error("Rollback failed:", rollbackErr);
|
||||
}
|
||||
}
|
||||
console.error("Error initializing database:", err);
|
||||
dbConnected = false;
|
||||
console.warn("⚠️ Could not connect to PostgreSQL DB (running in local fallback mode):", err.message);
|
||||
} finally {
|
||||
if (client) client.release();
|
||||
}
|
||||
@@ -177,6 +191,7 @@ app.post('/api/auth/login', async (req, res) => {
|
||||
return res.status(400).json({ error: "Username and password are required" });
|
||||
}
|
||||
|
||||
if (dbConnected) {
|
||||
try {
|
||||
const result = await pool.query(
|
||||
"SELECT role FROM app_users WHERE username = $1 AND password = $2",
|
||||
@@ -184,13 +199,21 @@ app.post('/api/auth/login', async (req, res) => {
|
||||
);
|
||||
|
||||
if (result.rows.length > 0) {
|
||||
res.json({ success: true, role: result.rows[0].role });
|
||||
return res.json({ success: true, role: result.rows[0].role });
|
||||
} else {
|
||||
res.status(401).json({ error: "Invalid username or password" });
|
||||
return res.status(401).json({ error: "Invalid username or password" });
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: "Database error during authentication" });
|
||||
console.error("DB Login error, using fallback:", err);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback mode login check
|
||||
const fallbackUser = memoryStore.users[username];
|
||||
if (fallbackUser && fallbackUser.password === password) {
|
||||
return res.json({ success: true, role: fallbackUser.role });
|
||||
} else {
|
||||
return res.status(401).json({ error: "Invalid username or password" });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -218,17 +241,19 @@ app.post('/api/auth/reset', async (req, res) => {
|
||||
|
||||
// Users Management APIs
|
||||
app.get('/api/users', async (req, res) => {
|
||||
if (dbConnected) {
|
||||
try {
|
||||
const result = await pool.query("SELECT username, role, password FROM app_users");
|
||||
const usersMap = {};
|
||||
result.rows.forEach(row => {
|
||||
usersMap[row.username] = { password: row.password, role: row.role };
|
||||
});
|
||||
res.json(usersMap);
|
||||
return res.json(usersMap);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: "Failed to fetch users" });
|
||||
}
|
||||
}
|
||||
return res.json(memoryStore.users);
|
||||
});
|
||||
|
||||
app.post('/api/users', async (req, res) => {
|
||||
@@ -237,20 +262,23 @@ app.post('/api/users', async (req, res) => {
|
||||
return res.status(400).json({ error: "Missing fields" });
|
||||
}
|
||||
|
||||
if (dbConnected) {
|
||||
try {
|
||||
await pool.query(
|
||||
"INSERT INTO app_users (username, password, role) VALUES ($1, $2, $3)",
|
||||
[username, password, role]
|
||||
);
|
||||
res.json({ success: true });
|
||||
return res.json({ success: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
if (err.code === '23505') {
|
||||
res.status(400).json({ error: "Username already exists" });
|
||||
} else {
|
||||
res.status(500).json({ error: "Failed to create user" });
|
||||
return res.status(400).json({ error: "Username already exists" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memoryStore.users[username] = { password, role };
|
||||
return res.json({ success: true });
|
||||
});
|
||||
|
||||
app.put('/api/users/:username', async (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user