-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(RServer): create the server module, currently contains the abili…
…ty to resolve the user defined
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import Util from './Util.js'; | ||
import http from 'http'; | ||
import StaticFileServer from './StaticFileServer.js'; | ||
import BodyParser from './BodyParser.js'; | ||
import _config from '../.rsvrc.json'; | ||
import RServerResponse from './RServerResponse.js'; | ||
|
||
export default class { | ||
|
||
/** | ||
*@param {string} configPath - user specified configuration path | ||
*/ | ||
constructor(configPath) { | ||
this.routes = []; | ||
this.middlewares = []; | ||
|
||
/* istanbul ignore else */ | ||
if (require.main) | ||
this.entryPath = this.getEntryPath(require.main.filename); | ||
else | ||
this.entryPath = this.getEntryPath(__dirname); | ||
|
||
this.config = this.resolveConfig(this.entryPath, configPath || '.rsvrc.json'); | ||
|
||
this.staticfileServer = new StaticFileServer( | ||
this.entryPath, | ||
this.config.staticFileDir, | ||
this.config.mimeTypes, | ||
this.config.defaultDocuments, | ||
this.config.cacheControl | ||
); | ||
|
||
this.bodyParser = new BodyParser( | ||
path.join(this.entryPath, this.config.tempDir, '/'), | ||
this.config.encoding | ||
); | ||
|
||
this.server = http.createServer({ | ||
ServerResponse: RServerResponse.getClass(this.staticfileServer) | ||
}); | ||
} | ||
|
||
/** | ||
* returns the server identity | ||
*@returns {string} | ||
*/ | ||
get [Symbol.toStringTag]() { | ||
return 'RServer'; | ||
} | ||
|
||
/** | ||
* returns boolean indicating if the server is listening | ||
*/ | ||
get listening() { | ||
return this.server.listening; | ||
} | ||
|
||
/** | ||
* returns the project's entry path | ||
*@param {string} knownPath - the known path for the mean time | ||
*/ | ||
getEntryPath(knownPath) { | ||
|
||
if (knownPath.indexOf('node_modules') > 0) | ||
return knownPath.split('node_modules/')[0]; | ||
|
||
let currentPath = path.join(knownPath, '../'); | ||
while (currentPath !== '/') { | ||
if (fs.existsSync(currentPath + '/package.json')) | ||
return currentPath; | ||
|
||
currentPath = path.join(currentPath, '../'); | ||
} | ||
|
||
/* istanbul ignore next */ | ||
return ''; | ||
} | ||
|
||
/** | ||
* resolves the configuration object | ||
*@param {string} entryPath - the project root path | ||
*@param {string} configPath - the project config file relative path | ||
*/ | ||
resolveConfig(entryPath, configPath) { | ||
let config = null, | ||
absConfigPath = path.join(entryPath, configPath); | ||
|
||
if (fs.existsSync(absConfigPath)) | ||
config = Util.mergeObjects(_config, require(absConfigPath)); | ||
else | ||
config = _config; | ||
|
||
return config; | ||
} | ||
} |