Skip to content

Commit

Permalink
refactor: ApiAdmin 基本完成
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Nov 1, 2024
1 parent 4222d11 commit 5ccb5ba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
7 changes: 2 additions & 5 deletions src/routes/ApiAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ export class ApiAdmin {
});

inst.app.post("/api/admin/update", async (req, res) => {
const user = inst.db.getEntity<UserEntity>(UserEntity, (JwtHelper.instance.verifyToken(req.cookies.adminToken, 'admin') as { userId: number }).userId);
if (!user) {
res.status(401).send();
return;
}
if (inst.server.isUpdating) {
res.status(409).send({
success: false,
Expand All @@ -77,5 +72,7 @@ export class ApiAdmin {
success: true
});
});

inst.app.get("/api/admin/all_users", async (req, res) => { res.json(inst.db.getEntities<UserEntity>(UserEntity)) });
}
}
30 changes: 30 additions & 0 deletions src/routes/ApiClusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,35 @@ export class ApiClusters {
inst.db.update(cluster);
res.json({ success: true });
});

inst.app.get("/api/clusters/:id/shards", async (req, res) => {
if (!Utilities.verifyAdmin(req, res, inst.db)) return;
const cluster = inst.clusters.find(c => c.clusterId === req.params.id);
if (!cluster) {
res.status(404).send(); // 集群不存在
return;
}
res.status(200).json({ shards: cluster.shards });
});
inst.app.put("/api/clusters/:id/shards", async (req, res) => {
if (!Utilities.verifyAdmin(req, res, inst.db)) return;
const cluster = inst.clusters.find(c => c.clusterId === req.query.clusterId);
if (!cluster) {
res.status(404).send(); // 集群不存在
return;
}
// 判断 shards 是不是在 int 范围内
const shards = Number(req.body.shards);
if (shards < 0 || shards > 1000) {
res.status(400).send({
message: "Shards must be between 0 and 1000"
});
return;
}
cluster.shards = shards;
inst.db.update(cluster);
res.setHeader('Content-Type', 'application/json');
res.status(200).json(cluster.getJson(true, false));
});
}
}
29 changes: 0 additions & 29 deletions src/routes/ApiV1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -561,33 +561,4 @@
});
this.app.get('/93AtHome/syncSources', (req: Request, res: Response) => {
res.status(200).json(this.sources);
});
this.app.get('/93AtHome/super/list_users', (req: Request, res: Response) => {
if (!Utilities.verifyAdmin(req, res, this.db)) return;
const users = this.db.getEntities<UserEntity>(UserEntity);
res.status(200).json(users);
})
this.app.get('/93AtHome/shards', (req: Request, res: Response) => {
if (!Utilities.verifyAdmin(req, res, this.db)) return;
res.json(this.fileList.shards);
});
this.app.post('/93AtHome/super/modify_shards', (req: Request, res: Response) => {
if (!Utilities.verifyAdmin(req, res, this.db)) return;
const cluster = this.clusters.find(c => c.clusterId === req.query.clusterId);
if (!cluster) {
res.status(404).send(); // 集群不存在
return;
}
// 判断 shards 是不是在 int 范围内
const shards = Number(req.body.shards);
if (shards < 0 || shards > 1000) {
res.status(400).send({
message: "Shards must be between 0 and 1000"
});
return;
}
cluster.shards = shards;
this.db.update(cluster);
res.setHeader('Content-Type', 'application/json');
res.status(200).json(cluster.getJson(true, false));
});

0 comments on commit 5ccb5ba

Please sign in to comment.