forked from redpencilio/fragmentation-producer-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
43 lines (38 loc) · 1.09 KB
/
app.ts
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
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { app, errorHandler } from 'mu';
import bodyParser from 'body-parser';
import cors from 'cors';
import { addData, getNode } from './lib/controller';
import type { Request, Response, NextFunction } from 'express';
import {
AUTH_PASSWORD,
AUTH_USERNAME,
ENABLE_AUTH,
} from './lib/utils/constants';
app.use(cors());
app.use(
bodyParser.text({
type: function () {
return true;
},
})
);
const validateUser = (req: Request, res: Response, next: NextFunction) => {
if (ENABLE_AUTH) {
if (req.headers.authorization?.startsWith('Basic ')) {
const b64value = req.headers.authorization.split(' ')[1];
const [username, password] = Buffer.from(b64value, 'base64')
.toString()
.split(':');
if (username === AUTH_USERNAME && password === AUTH_PASSWORD) {
return next();
}
}
return res.status(401).send('Authentication failed.');
}
return next();
};
app.post('/:folder', validateUser, addData);
app.get('/:folder*/:nodeId', getNode);
app.use(errorHandler);