-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c36d01c
commit 828db17
Showing
6 changed files
with
777 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
const ROOT = process.cwd(); | ||
const config = require(ROOT + '/config.json'); | ||
|
||
const {getHTTPUtils, getWSUtils} = require('./utils'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
let loadedControllers = {}; | ||
|
||
async function getController(controller) { | ||
controller = controller.charAt(0).toUpperCase() + controller.slice(1); | ||
return new Promise((resolve, reject) => { | ||
if (controller in loadedControllers) { | ||
resolve(loadedControllers[controller]); | ||
} else { | ||
const controllerPath = path.normalize(`${ROOT}/controllers/${controller}.js`); | ||
fs.access(controllerPath, fs.constants.F_OK | fs.constants.W_OK, (err) => { | ||
if(err && err.code == 'ENOENT') reject([`API ${controller} controller not found`, 404]); | ||
else if(err) reject([`Can't access ${controller} controller`, 403]); | ||
else { | ||
let controller = require(controllerPath); | ||
controller = new controller(); | ||
// Clear cache and don't save controller cache in devMode | ||
config.devMode | ||
? delete require.cache[controllerPath] | ||
: (loadedControllers[controller.constructor.name] = controller); | ||
resolve(controller); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
async function load(controller, action, utils) { | ||
try { controller = await getController(controller); } | ||
catch(err) { throw err; } | ||
|
||
controller.onLoad && controller.onLoad(); | ||
if (action in controller) return controller[action].apply(utils); | ||
else throw [`API ${controller.constructor.name}.${action} action not found`, 404]; | ||
} | ||
|
||
const dispatch = { | ||
async http(req, res) { | ||
// Getting controller and action from path | ||
let [controller, action] = req.path.split('/').slice(1); | ||
const utils = getHTTPUtils(req, res, this.db); | ||
try { | ||
await load(controller, action, utils); | ||
} catch(err) { | ||
utils.send(...err); | ||
return; | ||
} | ||
}, | ||
|
||
async ws(req, ws) { | ||
const utils = getWSUtils(ws, req, this.db); | ||
// Waiting first message with dispatch information | ||
const timeout = setTimeout(() => { | ||
utils.send('connection_timeout', false); | ||
utils.close(); | ||
ws.off('message', onData); | ||
}, (config.ws_timeout || 60) * 1000); | ||
|
||
const onData = async req => { | ||
clearTimeout(timeout); | ||
try { | ||
req = JSON.parse(req); | ||
} catch { | ||
utils.send('wrong_request', 400); | ||
return; | ||
} | ||
|
||
try { | ||
utils.data = req.data; | ||
await load(req.controller, req.action, utils); | ||
} catch(err) { | ||
if(err instanceof Array) { utils.end(...err); } | ||
else { utils.end(err, 500); } | ||
return; | ||
} | ||
}; | ||
ws.once('message', onData); | ||
} | ||
}; | ||
|
||
module.exports = dispatch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
{ | ||
"name": "dc-api-core", | ||
"version": "0.1.5-1", | ||
"version": "0.1.6", | ||
"author": "DimaCrafter", | ||
"homepage": "https://github.com/DimaCrafter/dc-api-core", | ||
"bugs": "https://github.com/DimaCrafter/dc-api-core/issues", | ||
"repository": "github.com:DimaCrafter/dc-api-core", | ||
"dependencies": { | ||
"connect-mongo": "^2.0.1", | ||
"express": "^4.16.3", | ||
"express": "^4.16.4", | ||
"express-session": "^1.15.6", | ||
"mongoose": "^5.3.1", | ||
"express-ws": "^4.0.0", | ||
"mongoose": "^5.3.4", | ||
"mongoose-auto-increment": "^5.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,40 @@ | ||
module.exports = (req, res, db) => { | ||
return { | ||
send(msg, code = 200) { | ||
res.set('Access-Control-Allow-Origin', req.get('origin')); | ||
res.set('Access-Control-Allow-Credentials', 'true'); | ||
res.set('Access-Control-Allow-Headers', '*'); | ||
res.status(code).json({ | ||
success: code == 200, | ||
code, | ||
msg | ||
}); | ||
}, | ||
const base = (utils, db, req) => { | ||
return Object.assign(utils, { | ||
db, | ||
data: req.body, | ||
session: req.session | ||
}; | ||
session: req.session, | ||
ROOT: process.cwd() | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
getHTTPUtils(req, res, db) { | ||
return base({ | ||
send(msg, code = 200) { | ||
res.set('Access-Control-Allow-Origin', req.get('origin')); | ||
res.set('Access-Control-Allow-Credentials', 'true'); | ||
res.set('Access-Control-Allow-Headers', '*'); | ||
res.status(code).json({ | ||
success: code === 200, | ||
code, | ||
msg | ||
}); | ||
}, | ||
data: req.body | ||
}, db, req); | ||
}, | ||
|
||
getWSUtils(ws, req, db) { | ||
return base({ | ||
send(msg, code = 0) { | ||
ws.send(JSON.stringify({ | ||
success: code === 0, | ||
code, | ||
msg | ||
})); | ||
}, | ||
end(...args) { this.send(...args); this.close(); }, | ||
close() { ws.close(); }, | ||
data: {} | ||
}, db, req); | ||
} | ||
}; |
Oops, something went wrong.