2016-12-28 15:49:51 -07: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-28 17:57:07 -07:00
|
|
|
// import authenticate from './authenticate';
|
2016-12-28 15:49:51 -07:00
|
|
|
import endpoints from './endpoints';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init app
|
|
|
|
*/
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
app.set('etag', false);
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
2017-02-11 10:18:58 -07:00
|
|
|
app.use(bodyParser.json({
|
2017-10-06 13:30:57 -06:00
|
|
|
type: ['application/json', 'text/plain'],
|
|
|
|
verify: (req, res, buf, encoding) => {
|
|
|
|
if (buf && buf.length) {
|
|
|
|
(req as any).rawBody = buf.toString(encoding || 'utf8');
|
|
|
|
}
|
|
|
|
}
|
2017-02-11 10:18:58 -07:00
|
|
|
}));
|
2016-12-28 15:49:51 -07:00
|
|
|
app.use(cors({
|
|
|
|
origin: true
|
|
|
|
}));
|
|
|
|
|
2016-12-29 10:17:30 -07:00
|
|
|
app.get('/', (req, res) => {
|
|
|
|
res.send('YEE HAW');
|
|
|
|
});
|
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
/**
|
|
|
|
* Register endpoint handlers
|
|
|
|
*/
|
|
|
|
endpoints.forEach(endpoint =>
|
|
|
|
endpoint.withFile ?
|
2017-04-14 05:45:37 -06:00
|
|
|
app.post(`/${endpoint.name}`,
|
2017-11-13 13:26:27 -07:00
|
|
|
endpoint.withFile ? multer({ storage: multer.diskStorage({}) }).single('file') : null,
|
2016-12-28 15:49:51 -07:00
|
|
|
require('./api-handler').default.bind(null, endpoint)) :
|
2017-04-14 05:45:37 -06:00
|
|
|
app.post(`/${endpoint.name}`,
|
2016-12-28 15:49:51 -07:00
|
|
|
require('./api-handler').default.bind(null, endpoint))
|
|
|
|
);
|
|
|
|
|
|
|
|
app.post('/signup', require('./private/signup').default);
|
|
|
|
app.post('/signin', require('./private/signin').default);
|
|
|
|
|
2017-01-20 22:39:39 -07:00
|
|
|
app.use((req, res, next) => {
|
2017-08-10 07:06:47 -06:00
|
|
|
// req.headers['cookie'] は常に string ですが、型定義の都合上
|
|
|
|
// string | string[] になっているので string を明示しています
|
|
|
|
res.locals.user = ((req.headers['cookie'] as string || '').match(/i=(!\w+)/) || [null, null])[1];
|
2017-01-20 22:39:39 -07:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2017-02-11 14:03:04 -07:00
|
|
|
require('./service/github')(app);
|
2017-01-20 22:39:39 -07:00
|
|
|
require('./service/twitter')(app);
|
|
|
|
|
2017-10-06 12:36:46 -06:00
|
|
|
require('./bot/interfaces/line')(app);
|
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
module.exports = app;
|