feat: admin set env

This commit is contained in:
archer
2023-06-10 22:56:57 +08:00
parent 7dd8e7bea1
commit b8a75921ed
18 changed files with 243 additions and 58 deletions

View File

@@ -1,4 +1,5 @@
import jwt from 'jsonwebtoken';
import { System } from '../schema.js';
const adminAuth = {
username: process.env.ADMIN_USER,
@@ -6,6 +7,14 @@ const adminAuth = {
};
const authSecret = process.env.ADMIN_SECRET;
const postParent = () => {
fetch(`${process.env.PARENT_URL}/api/system/updateEnv`, {
headers: {
rootkey: process.env.PARENT_ROOT_KEY
}
});
};
export const useSystemRoute = (app) => {
app.post('/api/login', (req, res) => {
if (!adminAuth.username || !adminAuth.password) {
@@ -37,6 +46,63 @@ export const useSystemRoute = (app) => {
res.status(401).end('username or password incorrect');
}
});
app.get('/system', auth(), async (req, res) => {
try {
const data = await System.find();
const totalCount = await System.countDocuments();
res.header('Access-Control-Expose-Headers', 'X-Total-Count');
res.header('X-Total-Count', totalCount);
res.json(
data.map((item) => {
const obj = item.toObject();
return {
...obj,
id: obj._id
};
})
);
} catch (error) {
console.log(error);
res.status(500).json({ error: 'Error creating system env' });
}
});
app.post('/system', auth(), async (req, res) => {
try {
await System.create({
...req.body,
sensitiveCheck: req.body.sensitiveCheck === 'true'
});
postParent();
res.json({});
} catch (error) {
res.status(500).json({ error: 'Error creating system env' });
}
});
app.put('/system/:id', auth(), async (req, res) => {
try {
const _id = req.params.id;
await System.findByIdAndUpdate(_id, {
...req.body,
sensitiveCheck: req.body.sensitiveCheck === 'true'
});
postParent();
res.json({});
} catch (error) {
res.status(500).json({ error: 'Error updating system env' });
}
});
app.delete('/system/:id', auth(), async (req, res) => {
try {
const _id = req.params.id;
await System.findByIdAndDelete(_id);
res.json({});
} catch (error) {
res.status(500).json({ error: 'Error updating system env' });
}
});
};
export const auth = () => {