summaryrefslogtreecommitdiff
path: root/src/api/server.ts
blob: 78b0d0aea84754f0e2f0df5cb22b4b10c4090f71 (plain)
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
/**
 * API Server
 */

import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as cors from 'cors';
import * as multer from 'multer';

import authenticate from './authenticate';
import endpoints from './endpoints';

/**
 * Init app
 */
const app = express();

app.disable('x-powered-by');
app.set('etag', false);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors({
	origin: true
}));

/**
 * Authetication
 */
/*app.post('*', async (req, res, next) => {
	try {
		ctx = await authenticate(req);
		next();
	} catch (e) {
		res.status(403).send('AUTHENTICATION_FAILED');
	}
});
*/
/**
 * Register endpoint handlers
 */
endpoints.forEach(endpoint =>
	endpoint.withFile ?
		app.post('/' + endpoint.name,
			endpoint.withFile ? multer({ dest: 'uploads/' }).single('file') : null,
			require('./api-handler').default.bind(null, endpoint)) :
		app.post('/' + endpoint.name,
			require('./api-handler').default.bind(null, endpoint))
);

app.post('/signup', require('./private/signup').default);
app.post('/signin', require('./private/signin').default);

module.exports = app;