summaryrefslogtreecommitdiff
path: root/src/api/server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/server.ts')
-rw-r--r--src/api/server.ts52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/api/server.ts b/src/api/server.ts
new file mode 100644
index 0000000000..78b0d0aea8
--- /dev/null
+++ b/src/api/server.ts
@@ -0,0 +1,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;