Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. Changing the way of handling configuration 3. Adding tests for configuration 4. Adding features to auto scan for configurations and directory structure 5. many design related changes
- Loading branch information
1 parent
d75de17
commit dd8fcc9
Showing
18 changed files
with
484 additions
and
98 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,40 @@ | ||
Usage: stubby-db [OPTION]... | ||
Runs stubbyDB server | ||
|
||
Arguments | ||
-p, --port port number | ||
-m, --mapping file path of yaml mapping file (response.yaml) | ||
-s, --stub path for stubs only if mapping file has relative path and they are not in current folder | ||
-c, --config name of config file. It must be json file. | ||
-C json config as command line argument | ||
-h, --help display on screen help | ||
-d location of project directory, if it is not a current directory. | ||
-q, --quite Under development | ||
all - turn off on screen logging | ||
info - turn off on screen logging for info only | ||
error - turn off on screen logging for error only | ||
|
||
Considerations: | ||
if -p, -m, -s options are given then relevant properties from config file would be ignored; | ||
Precedence of loding config is -p,-m-s > -c > -C > -d > default | ||
|
||
Default config: | ||
if no option is given then | ||
1. check for config.json in current folder | ||
2. otherwise check for 'dbsets', 'mappings', 'stubs' folder in currrent directory. and set port to 7777. | ||
|
||
Note: | ||
-c, --config The purpose of this option to choose different config file instead of config.json from current directory. But if you specify different location then run stubby-db from that location only. Or maintain absolute paths in config.json for dbsets, mappings, and stubs. | ||
|
||
Project directory structure: | ||
|
||
Project | ||
|_ dbsets | ||
|_ dumps | ||
|_ mappings | ||
|_ stubs | ||
|_ logs | ||
|
||
Read the wiki : https://github.com/NaturalIntelligence/StubbyDB | ||
Report a bug : https://github.com/NaturalIntelligence/StubbyDB/issues | ||
|
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,4 +1,42 @@ | ||
var stubbyDB = require('./js/stubbyDB').stubbyDB; | ||
#!/usr/bin/env node | ||
|
||
var stubby_db = new stubbyDB(7788); | ||
stubby_db.start(); | ||
if(process.argv[2] == "--help" || process.argv[2] == "-h"){ | ||
console.log(); | ||
var fs = require('fs'); | ||
console.log(fs.readFileSync("help", 'utf-8')); | ||
console.log(); | ||
process.exit(1); | ||
} | ||
|
||
var options = {} | ||
for(var i=2; i<process.argv.length;i++){ | ||
if(process.argv[i].startsWith("-")){ | ||
var key = process.argv[i]; | ||
if(key == '--port' || key == '-p'){ | ||
key = '-p'; | ||
}else if(key == '--config' || key == '-c'){ | ||
key = '-c'; | ||
}else if(key == '--mapping' || key == '-m'){ | ||
key = '-m'; | ||
}else if(key == '--stub' || key == '-s'){ | ||
key = '-s'; | ||
}else if(key == '-C' || key == '-d'){ | ||
//key = '-p'; | ||
}else{ | ||
console.log("Invalid options"); | ||
console.log("Try 'stubbydb --help' for more information.*/") | ||
process.exit(1); | ||
} | ||
options[key] = process.argv[++i]; | ||
} | ||
} | ||
|
||
var configBuilder = require("./js/configbuilder"); | ||
configBuilder.buildConfig(options,process.argv.length); | ||
var config = configBuilder.getConfig(); | ||
console.log("Configuration: " + JSON.stringify(config)); | ||
|
||
|
||
var stubbyDB = require('./js/stubbyDB'); | ||
var server = new stubbyDB(); | ||
server.start(); |
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,154 @@ | ||
var fs = require('fs'); | ||
var util = require('./preutil'); | ||
|
||
var defaultConfig = { | ||
mappings: { | ||
default: { | ||
request:{ | ||
method: 'GET' | ||
}, | ||
response: { | ||
strategy: 'first-found', | ||
latency: 0, | ||
status: 200 | ||
} | ||
}, | ||
requests: ["response.yaml"] | ||
}, | ||
server: { | ||
port: 7777 | ||
}, | ||
logs:{ | ||
path: 'logs' | ||
} | ||
}; | ||
|
||
/*exports.reset = function(){ | ||
var someconfig = { | ||
mappings: { | ||
default: { | ||
request:{ | ||
method: 'GET' | ||
}, | ||
response: { | ||
strategy: 'first-found', | ||
latency: 0, | ||
status: 200 | ||
} | ||
}, | ||
request: "response.yaml" | ||
}, | ||
server: { | ||
port: 7777 | ||
} | ||
}; | ||
defaultConfig = someconfig; | ||
}*/ | ||
|
||
var setConfig = function(path, value) { | ||
var s = defaultConfig; | ||
var pList = path.split('.'); | ||
var len = pList.length; | ||
for(var i = 0; i < len-1; i++) { | ||
var elem = pList[i]; | ||
if( !s[elem] ) s[elem] = {} | ||
s = s[elem]; | ||
} | ||
|
||
s[pList[len-1]] = value; | ||
} | ||
|
||
exports.buildConfig = function(options,count){ | ||
|
||
if(count == 2){//No option is provided. Run with default options | ||
useDefaultConfig(); | ||
}else if(options['-c']){ | ||
var jsonconfig = require(options['-c']); | ||
buildFromJsonConfig(jsonconfig); | ||
}else if(options['-C']){ | ||
var jsonConfig; | ||
try{ | ||
jsonConfig = JSON.parse(options['-C']); | ||
}catch(e){ | ||
console.log("Invalid json"); | ||
process.exit(1); | ||
} | ||
buildFromJsonConfig(jsonConfig); | ||
}else if(options['-d']){ | ||
buildFromDirectory(options['-d']); | ||
} | ||
|
||
if(options['-p']){ | ||
setConfig('server.port',options['-p']); | ||
} | ||
|
||
if(options['-m']){ | ||
defaultConfig.mappings.requests = []; | ||
defaultConfig.mappings.requests.push(options['-m']); | ||
} | ||
|
||
if(options['-s']){ | ||
setConfig('stubs',options['-s']); | ||
} | ||
|
||
} | ||
|
||
/* | ||
load config from config.json, if presents. | ||
Otherwise follow current directory structure. | ||
Use default port only. | ||
*/ | ||
function useDefaultConfig(){ | ||
console.log(process.cwd()); | ||
if(util.isExist(process.cwd() + "/config.json")){ | ||
var jsonconfig = require(process.cwd() +'/config.json'); | ||
buildFromJsonConfig(jsonconfig); | ||
}else{ | ||
buildFromDirectory(process.cwd()); | ||
} | ||
} | ||
|
||
var merge = require('deepmerge'); | ||
|
||
function buildFromJsonConfig(jsonconfig){ | ||
defaultConfig = merge(defaultConfig,jsonconfig); | ||
} | ||
|
||
/* | ||
build configurtaion on the basis of directory structure | ||
It'll ignore if there is any config file in specified directory. | ||
It update the path of : mappings, dbsets, and stubs, whichever is presnt | ||
*/ | ||
function buildFromDirectory(dirPath){ | ||
if(util.isExist(dirPath+"/dbsets")){ | ||
defaultConfig['dbsets'] = dirPath + '/dbsets/'; | ||
} | ||
|
||
if(util.isExist(dirPath+"/stubs")){ | ||
defaultConfig['stubs'] = dirPath + '/stubs/'; | ||
} | ||
|
||
if(util.isExist(dirPath+"/logs")){ | ||
defaultConfig['logs']['path'] = dirPath + '/logs/'; | ||
}else{ | ||
defaultConfig['logs']['path'] = dirPath + '/'; | ||
} | ||
|
||
if(util.isExist(dirPath+"/dumps")){ | ||
defaultConfig['dumps'] = dirPath + '/dumps/'; | ||
} | ||
|
||
var files = util.ls(dirPath+"/mappings"); | ||
if(files.length > 0){ | ||
defaultConfig['mappings']['requests'] = []; | ||
files.forEach(function(filename){ | ||
defaultConfig['mappings']['requests'].push(dirPath + '/mappings/' + filename); | ||
}); | ||
} | ||
} | ||
|
||
exports.getConfig= function(){ | ||
return defaultConfig; | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -1,17 +1,18 @@ | ||
var YAML = require('yamljs'); | ||
var fs = require('fs'); | ||
|
||
var config; | ||
|
||
try{ | ||
config = YAML.parseFile("./config.yaml"); | ||
}catch(e){ | ||
console.log("Config file is not found on root location of the project."); | ||
exports.isExist = function(path){ | ||
try{ | ||
fs.accessSync(path, fs.F_OK); | ||
return true; | ||
}catch(e){ | ||
return false; | ||
} | ||
} | ||
|
||
exports.getConfigFor = function(name){ | ||
for(var i in config){ | ||
if(config[i][name]){ | ||
return config[i][name]; | ||
} | ||
/* Empty list if no file or directory not exist or access issues*/ | ||
exports.ls = function(dirpath){ | ||
if(exports.isExist(dirpath)){ | ||
return fs.readdirSync(dirpath); | ||
} | ||
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
Oops, something went wrong.