1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-25 12:08:07 +09:00
hotomoe/src/api/server.ts

58 lines
1.4 KiB
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09: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 09:57:07 +09:00
// import authenticate from './authenticate';
2016-12-29 07:49:51 +09: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 }));
app.use(bodyParser.json({
type: ['application/json', 'text/plain']
}));
2016-12-29 07:49:51 +09:00
app.use(cors({
origin: true
}));
2016-12-30 02:17:30 +09:00
app.get('/', (req, res) => {
res.send('YEE HAW');
});
2016-12-29 07:49:51 +09:00
/**
* Register endpoint handlers
*/
endpoints.forEach(endpoint =>
endpoint.withFile ?
2017-04-14 20:45:37 +09:00
app.post(`/${endpoint.name}`,
2016-12-29 07:49:51 +09:00
endpoint.withFile ? multer({ dest: 'uploads/' }).single('file') : null,
require('./api-handler').default.bind(null, endpoint)) :
2017-04-14 20:45:37 +09:00
app.post(`/${endpoint.name}`,
2016-12-29 07:49:51 +09:00
require('./api-handler').default.bind(null, endpoint))
);
app.post('/signup', require('./private/signup').default);
app.post('/signin', require('./private/signin').default);
2017-01-21 14:39:39 +09:00
app.use((req, res, next) => {
2017-08-10 22:06:47 +09:00
// req.headers['cookie'] は常に string ですが、型定義の都合上
// string | string[] になっているので string を明示しています
res.locals.user = ((req.headers['cookie'] as string || '').match(/i=(!\w+)/) || [null, null])[1];
2017-01-21 14:39:39 +09:00
next();
});
require('./service/github')(app);
2017-01-21 14:39:39 +09:00
require('./service/twitter')(app);
2016-12-29 07:49:51 +09:00
module.exports = app;