-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
190 lines (165 loc) · 5.75 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require('dotenv').config();
const { ObjectID } = require('mongodb');
const express = require('express');
const path = require('path');
const {
connect,
find,
aggregate,
insertNewDocument,
changeDocument,
deleteDocument,
} = require('./api/database');
const { encrypt, decryptPwd } = require('./lib/crypto');
const { createPasswordList } = require('./lib/createlists');
const app = express();
const port = process.env.PORT || 3001;
app.use(express.json());
app.get('/password/:userquery', async (request, response) => {
const { userquery } = request.params;
const regex = new RegExp(`.*${userquery}.*`, 'ig');
const query = { $or: [{ category: { $in: [regex] } }, { name: { $in: [regex] } }] };
try {
const documents = await find(process.env.DB_COLLECTION, query);
if (documents.length === 0) {
response.status(404).send('Could not find passwords.');
return;
}
response.json(documents);
} catch (err) {
console.log(err);
response.status(500).send('An internal server error occured.');
}
});
app.get('/password/id/:id', async (request, response) => {
const { id } = request.params;
const passwordID = new ObjectID.createFromHexString(id);
const queryPwd = [
{ $match: { _id: passwordID } },
{ $project: { category: false, _id: false } },
];
try {
const passwordDocument = await aggregate(process.env.DB_COLLECTION, queryPwd);
if (!passwordDocument) {
response.status(404).send('Could not specific password.');
return;
}
const name = passwordDocument[0].name;
const value = decryptPwd(passwordDocument[0].value, process.env.MASTER_PWD);
const encryptedDocument = {
name,
value,
};
response.send(encryptedDocument);
} catch (err) {
console.log(err);
response.status(500).send('An internal server error occured.');
}
});
app.get('/categories', async (request, response) => {
const query = [{ $group: { _id: '$category' } }];
try {
const objectCategories = await aggregate(process.env.DB_COLLECTION, query);
if (!objectCategories) {
response.status(404).send('Could not find categories.');
return;
}
const categories = objectCategories.map((document) => {
return document._id;
});
response.send(categories);
} catch (err) {
console.log(err);
response.status(500).send('An internal server error occured.');
}
});
app.get('/categories/:category', async (request, response) => {
const { category } = request.params;
const query = [
{ $match: { category } },
{
$project: {
value: false,
category: false,
},
},
];
try {
const documents = await aggregate(process.env.DB_COLLECTION, query);
if (!documents) {
response.status(404).send(`Could not find passwords in category ${category}`);
return;
}
const choices = await createPasswordList(documents);
response.send(choices);
} catch (err) {
console.log(err);
response.status(500).send('An internal server error occured.');
}
});
app.post('/password/', async function (request, response) {
const password = request.body;
const newDocument = {};
newDocument.category = password.category;
newDocument.name = password.name;
const rawValue = password.value;
newDocument.value = encrypt(rawValue, process.env.MASTER_PWD);
try {
await insertNewDocument(process.env.DB_COLLECTION, newDocument);
response.send('Got a POST request');
} catch (err) {
console.log(err);
response.status(500).send('An internal server error occured.');
}
});
app.put('/password/', async function (request, response) {
const passwordID = request.query.id;
const encryptedPassword = encrypt(request.query.password, process.env.MASTER_PWD);
try {
const result = await changeDocument(
process.env.DB_COLLECTION,
passwordID,
encryptedPassword
);
if (result.modifiedCount === 0) {
return response.status(404).send('Couldn´t modify password.');
}
response.send('Password changed');
} catch (err) {
console.log(err);
response.status(500).send('An internal server error occured.');
}
});
app.delete('/password/:id', async (request, response) => {
const { id } = request.params;
const objectID = new ObjectID.createFromHexString(id);
try {
const result = await deleteDocument(process.env.DB_COLLECTION, objectID);
if (result.deletedCount === 0) {
return response.status(404).send('Password to delete not found');
}
response.send('Password deleted');
} catch (err) {
console.log(err);
response.status(500).send('An internal server error occured.');
}
});
// preparation build scripts
// heroku necessary scripts in package.json
// "build": "cd client && npm run build && npm run build-storybook",
// "start": "node server.js",
// "postinstall": "cd client && npm install"
// + ....
app.use(express.static(path.join(__dirname, 'client/build')));
app.use('/storybook', express.static(path.join(__dirname, 'client/storybook-static')));
app.get('*', (request, response) => {
response.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
async function run() {
console.log('Connecting to database ...');
await connect(process.env.DB_URL, process.env.DB_NAME);
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
}
run();