UNPKG

2.22 kBJavaScriptView Raw
1const express = require("express");
2const bodyParser = require("body-parser");
3const CredentialDB = require("./CredentialDB");
4
5/**
6 * @type {CredentialServer}
7 */
8class CredentialServer {
9
10 constructor() {
11 this.server = express();
12 this.server.use(bodyParser.json());
13 this.db = new CredentialDB();
14
15 this.server.post("/credentials", (req, res) => {
16 try {
17 this.db.createPool(req.body, req.query.pool);
18 res.sendStatus(201);
19 } catch (e) {
20 res.status(500).send(null);
21 }
22 });
23
24 this.server.get("/credentials", (req, res) => {
25 try {
26 const user = this.db.getCredentials(req.query.pool);
27 res.status(200).send(user);
28 } catch (e) {
29 res.status(500).send(null);
30 }
31 });
32
33 this.server.get("/credentials/:username", (req, res) => {
34 try {
35 const user = this.db.getCredentialsByUsername(req.params.username, req.query.pool);
36 res.status(200).send(user);
37 } catch (e) {
38 res.status(500).send(null);
39 }
40 });
41
42 this.server.put("/credentials", (req, res) => {
43 try {
44 this.db.freeCredentials(req.body.username, req.query.pool);
45 res.sendStatus(200);
46 } catch (e) {
47 res.status(500).send(null);
48 }
49 });
50
51 this.server.put("/credentials/update", (req, res) => {
52 try {
53 this.db.updateProperty(req.body.username, req.body.property, req.body.value, req.query.pool);
54 res.sendStatus(200);
55 } catch (e) {
56 res.status(500).send(null);
57 }
58 });
59
60 this.server.get("/state", (req, res) => {
61 try {
62 res.status(200).send(this.db);
63 } catch (e) {
64 res.status(500).send(e);
65 }
66 });
67
68 }
69
70 /**
71 * Start server
72 * @param {number} port - port to start the server
73 */
74 start(port) {
75 this.server.listen(port);
76 }
77
78}
79
80module.exports = CredentialServer;
\No newline at end of file