u
This commit is contained in:
43
uiserver/api/index.js
Normal file
43
uiserver/api/index.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* API Routes Index - Sets up all API endpoints
|
||||
*/
|
||||
|
||||
const setupAuthApi = require('./auth');
|
||||
const setupViewsApi = require('./views');
|
||||
const setupRulesApi = require('./rules');
|
||||
const setupOutputsApi = require('./outputs');
|
||||
const setupDevicesApi = require('./devices');
|
||||
const setupReadingsApi = require('./readings');
|
||||
|
||||
module.exports = function setupAllApis(app, context) {
|
||||
const { db, bcrypt, jwt, JWT_SECRET, OUTPUT_CHANNELS, OUTPUT_BINDINGS, runRules, activeRuleIds } = context;
|
||||
|
||||
// Auth middleware helpers
|
||||
const checkAuth = (req, res, next) => {
|
||||
const authHeader = req.headers.authorization;
|
||||
if (authHeader) {
|
||||
const token = authHeader.split(' ')[1];
|
||||
jwt.verify(token, JWT_SECRET, (err, user) => {
|
||||
if (user) req.user = user;
|
||||
next();
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
};
|
||||
|
||||
const requireAdmin = (req, res, next) => {
|
||||
if (!req.user || req.user.role !== 'admin') {
|
||||
return res.status(403).json({ error: 'Admin access required' });
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
// Setup all API routes
|
||||
setupAuthApi(app, { db, bcrypt, jwt, JWT_SECRET });
|
||||
setupViewsApi(app, { db, checkAuth, requireAdmin });
|
||||
setupRulesApi(app, { db, checkAuth, requireAdmin, runRules, activeRuleIds });
|
||||
setupOutputsApi(app, { db, OUTPUT_CHANNELS, OUTPUT_BINDINGS });
|
||||
setupDevicesApi(app, { db, OUTPUT_CHANNELS });
|
||||
setupReadingsApi(app, { db });
|
||||
};
|
||||
Reference in New Issue
Block a user