-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Refactor Webserver structure (breaks compatibility)
- Loading branch information
1 parent
78c8155
commit 8cb948a
Showing
13 changed files
with
215 additions
and
73 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 was deleted.
Oops, something went wrong.
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
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
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,3 @@ | ||
module.exports = { | ||
get webserverRouteLoader() { return require(__dirname + '/webserver-route-loader'); }, | ||
}; |
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,35 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const express = require('express'); | ||
const fsUtils = require('../utils/fs'); | ||
|
||
module.exports = function (webserver, baseDir = __dirname, routePath = null) { | ||
const logger = require('../logging')('abbott-framework:webserver-route-loader'); | ||
|
||
var parentRoute = webserver; | ||
|
||
if (routePath) { | ||
parentRoute = express.Router(); | ||
webserver.use(routePath, parentRoute); | ||
} | ||
|
||
var routeDirectories = fsUtils.getDirectories(baseDir); | ||
|
||
routeDirectories.forEach((routeApiItem) => { | ||
logger.info(`directories: (${baseDir} / ${routeApiItem})`); | ||
|
||
var apiSubRoute = require(path.join(baseDir, routeApiItem)); | ||
if (apiSubRoute) { | ||
let subPath = routePath || '/'; | ||
let apiRoutePath = path.join(subPath, routeApiItem); | ||
let router = express.Router(); | ||
|
||
apiSubRoute(webserver, router); | ||
|
||
parentRoute.use(apiRoutePath, router); | ||
logger.info(`subroute loaded (${apiRoutePath})!`); | ||
} | ||
}); | ||
|
||
return parentRoute; | ||
}; |
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,13 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
module.exports = class { | ||
static getDirectories(srcpath) { | ||
if (fs.existsSync(srcpath)) { | ||
return fs.readdirSync(srcpath) | ||
.filter((file) => fs.lstatSync(path.join(srcpath, file)).isDirectory()); | ||
} | ||
|
||
return []; | ||
} | ||
}; |
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,96 @@ | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var express = require('express'); | ||
var bodyParser = require('body-parser'); | ||
var querystring = require('querystring'); | ||
const logging = require('../logging'); | ||
const { webserverRouteLoader } = require('../loaders'); | ||
|
||
module.exports = class { | ||
get logger() { | ||
if (!this.__logger) { | ||
this.__logger = logging(`abbott-framework:webserver`); | ||
} | ||
return this.__logger; | ||
} | ||
|
||
constructor(options) { | ||
this.options = options || {}; | ||
|
||
this.server = express(); | ||
this.server.use(bodyParser.json()); | ||
this.server.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
this.serverRoutes = webserverRouteLoader(this.server, path.join(__dirname, 'routes')); | ||
} | ||
|
||
start() { | ||
return new Promise((resolve, reject) => { | ||
this.serverListener = this.server.listen(process.env.express_port || 3000, () => { | ||
this.logger.debug('Webserver configured and listening at http://localhost:' + process.env.express_port || 3000); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
stop() { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
this.logger.debug('Closing webserver...'); | ||
this.serverListener.close(() => { | ||
this.logger.debug('Webserver closed!'); | ||
resolve(); | ||
}); | ||
} catch(ex) { | ||
reject(ex); | ||
} | ||
}); | ||
} | ||
|
||
}; | ||
/* | ||
module.exports = function (options) { | ||
var logger = logging(`abbott-framework:webserver`); | ||
options = options || {}; | ||
var webserver = express(); | ||
webserver.use(bodyParser.json()); | ||
webserver.use(bodyParser.urlencoded({ extended: true })); | ||
var webserverRoutes = webserverRouteLoader(webserver, path.join(__dirname, 'routes')); | ||
// var bot_assets_dir = path.join(process.cwd(), 'bot'); | ||
// var mimeImage = { | ||
// gif: 'image/gif', | ||
// jpg: 'image/jpeg', | ||
// png: 'image/png', | ||
// svg: 'image/svg+xml' | ||
// }; | ||
// webserver.get('/bot/avatar', (req, res) => { | ||
// var file = path.join(bot_assets_dir, 'avatar.png'); | ||
// if (file.indexOf(bot_assets_dir + path.sep) !== 0) { | ||
// return res.status(403).end('Forbidden'); | ||
// } | ||
// var type = mimeImage[path.extname(file).slice(1)] || 'text/plain'; | ||
// var s = fs.createReadStream(file); | ||
// s.on('open', function () { | ||
// res.set('Content-Type', type); | ||
// s.pipe(res); | ||
// }); | ||
// s.on('error', function () { | ||
// res.set('Content-Type', 'text/plain'); | ||
// res.status(404).end('Not found'); | ||
// }); | ||
// }); | ||
webserver.listen(process.env.express_port || 3000, null, function () { | ||
logger.debug('Express webserver configured and listening at http://localhost:' + process.env.express_port || 3000); | ||
}); | ||
return webserver; | ||
}; | ||
*/ |
File renamed without changes.
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,33 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
module.exports = function (webserver, router) { | ||
const logger = require('../../../logging')('abbott-framework:webserver(/bot)'); | ||
logger.debug('route loaded!'); | ||
|
||
var mimeImage = { | ||
gif: 'image/gif', | ||
jpg: 'image/jpeg', | ||
png: 'image/png', | ||
svg: 'image/svg+xml' | ||
}; | ||
|
||
var bot_assets_dir = path.join(process.cwd(), 'bot'); | ||
|
||
router.get('/avatar', (req, res) => { | ||
var file = path.join(bot_assets_dir, 'avatar.png'); | ||
if (file.indexOf(bot_assets_dir + path.sep) !== 0) { | ||
return res.status(403).end('Forbidden'); | ||
} | ||
var type = mimeImage[path.extname(file).slice(1)] || 'text/plain'; | ||
var s = fs.createReadStream(file); | ||
s.on('open', function () { | ||
res.set('Content-Type', type); | ||
s.pipe(res); | ||
}); | ||
s.on('error', function () { | ||
res.set('Content-Type', 'text/plain'); | ||
res.status(404).end('Not found'); | ||
}); | ||
}); | ||
}; |