Sharkey/src/api/server.ts

68 lines
1.4 KiB
TypeScript
Raw Normal View History

2016-12-29 00:49:51 +02:00
/**
* API Server
*/
import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as cors from 'cors';
import * as multer from 'multer';
2016-12-29 02:57:07 +02:00
// import authenticate from './authenticate';
2016-12-29 00:49:51 +02:00
import endpoints from './endpoints';
/**
* Init app
*/
const app = express();
app.disable('x-powered-by');
app.set('etag', false);
2017-02-11 22:09:33 +02:00
require('./service/github')(app);
2016-12-29 00:49:51 +02:00
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({
type: ['application/json', 'text/plain']
}));
2016-12-29 00:49:51 +02:00
app.use(cors({
origin: true
}));
2016-12-29 19:17:30 +02:00
app.get('/', (req, res) => {
res.send('YEE HAW');
});
2016-12-29 00:49:51 +02:00
/**
* 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);
2017-01-21 07:39:39 +02:00
app.use((req, res, next) => {
res.locals.user = ((req.headers['cookie'] || '').match(/i=(!\w+)/) || [null, null])[1];
next();
});
require('./service/twitter')(app);
2016-12-29 00:49:51 +02:00
module.exports = app;