2017-06-01 10:09:25 +00:00
|
|
|
const Router = require('koa-router');
|
|
|
|
const router = new Router();
|
2017-06-01 14:37:51 +00:00
|
|
|
const koaBody = require('koa-body');
|
2017-06-01 10:09:25 +00:00
|
|
|
|
2017-06-01 14:37:51 +00:00
|
|
|
// import tools
|
|
|
|
const mysql = require('../../libs/mysql_pool.js');
|
2017-06-02 10:07:25 +00:00
|
|
|
const so = require('../../libs/storeObject');
|
2017-06-01 14:37:51 +00:00
|
|
|
const config = require('../../config.json');
|
2017-06-04 03:54:25 +00:00
|
|
|
const errorMng = require('./errorManager');
|
2017-06-01 14:37:51 +00:00
|
|
|
|
|
|
|
// routes
|
2017-06-05 06:41:32 +00:00
|
|
|
const account_api = require('./account.js');
|
2017-06-01 14:37:51 +00:00
|
|
|
|
|
|
|
// api response handler
|
2017-06-05 06:41:32 +00:00
|
|
|
router.use(async(c, n) => {
|
|
|
|
// get MySQL connection
|
2017-06-01 14:37:51 +00:00
|
|
|
c.db = await mysql.getConn();
|
2017-06-05 06:41:32 +00:00
|
|
|
// set Async/Await Query
|
2017-06-01 14:37:51 +00:00
|
|
|
c.syncQuery = (query, param = null) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (param != null) {
|
|
|
|
c.db.query(query, param, (err, row) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
return resolve(row)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
c.db.query(query, (err, row) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
return resolve(row)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-06-04 13:38:14 +00:00
|
|
|
c.token = c.headers['x-auth-token'] || '';
|
2017-06-05 06:41:32 +00:00
|
|
|
|
2017-06-04 03:54:25 +00:00
|
|
|
// console.log(c.headers['accept-language'])
|
2017-06-03 13:20:34 +00:00
|
|
|
try {
|
|
|
|
await n();
|
2017-06-04 03:54:25 +00:00
|
|
|
c.body = {
|
|
|
|
data: c.body,
|
|
|
|
status: 1
|
|
|
|
}
|
2017-06-03 13:20:34 +00:00
|
|
|
} catch (e) {
|
2017-06-04 03:54:25 +00:00
|
|
|
let msg = errorMng.getMsg(e, c.headers['accept-language']);
|
|
|
|
c.body = {
|
|
|
|
errorCode: e,
|
|
|
|
message: msg,
|
|
|
|
servErr: c.serr ? c.serr.toString() : '',
|
|
|
|
status: 0
|
|
|
|
}
|
2017-06-05 06:41:32 +00:00
|
|
|
if (e[0] == 'C' || e[0] == 'c') c.status = 400;
|
|
|
|
if (e[0] == 'S' || e[0] == 's') c.status = 500;
|
2017-06-03 13:20:34 +00:00
|
|
|
}
|
2017-06-01 14:37:51 +00:00
|
|
|
if ('db' in c && typeof c.db == 'object' && 'release' in c.db && typeof c.db.release == 'function') c.db.release();
|
2017-06-01 10:09:25 +00:00
|
|
|
})
|
|
|
|
|
2017-06-01 14:37:51 +00:00
|
|
|
|
|
|
|
// enable bodyParser
|
|
|
|
router.all('*', koaBody({
|
|
|
|
multipart: true,
|
|
|
|
// upload file size 10mb
|
|
|
|
maxFieldSize: 10 * 1024 * 1024
|
2017-06-05 06:41:32 +00:00
|
|
|
}), async(c, n) => { await n(); })
|
2017-06-01 14:37:51 +00:00
|
|
|
|
|
|
|
router
|
2017-06-05 06:41:32 +00:00
|
|
|
.get('/', async(c, n) => {
|
2017-06-01 14:37:51 +00:00
|
|
|
c.body = {
|
|
|
|
msg: 'API Endpoint'
|
|
|
|
};
|
|
|
|
})
|
2017-06-05 06:41:32 +00:00
|
|
|
.use('/account', account_api.routes())
|
2017-06-01 14:37:51 +00:00
|
|
|
|
2017-06-01 10:09:25 +00:00
|
|
|
module.exports = router;
|