REPLで作ったハリボテAPIクライアント
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
'use strict'; | |
var http = require('http'); | |
module.exports = class ApiClient { | |
constructor(host, port) { | |
this.host = host; | |
this.port = port; | |
} | |
setHost(host) { | |
this.host = host; | |
return `host = [${this.host}]`; | |
} | |
setPort(port) { | |
this.port = port; | |
return `port = [${this.port}]`; | |
} | |
getHost() { | |
return `host = [${this.host}]`; | |
} | |
getPort() { | |
return `port = [${this.port}]`; | |
} | |
authenticate(identifier, password) { | |
var req = http.request({ | |
hostname: this.host, | |
port: this.port, | |
path: '/api/authenticate/token', | |
method: 'POST', | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
}, res => { | |
res.on('data', chunk => { | |
var response = JSON.parse(chunk.toString()); | |
this.JWT = response.JWT; | |
}); | |
res.on('end', () => console.log(`get JWT ${this.JWT}`)); | |
}); | |
req.on('error', err => console.log(err)); | |
req.write(JSON.stringify({ identifier, password })); | |
req.end(); | |
} | |
getJWT() { | |
return `JWT = ${this.JWT}`; | |
} | |
} |
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
#!/usr/bin/env node | |
var repl = require('repl'); | |
var env = process.env.NODE_ENV || 'development'; | |
var dbconf = require('../config/database')[env]; | |
var Sequelize = require('sequelize'); | |
var sequelize = new Sequelize(dbconf); | |
var _ = Sequelize.Utils._; | |
/** | |
* insert models to global | |
*/ | |
var models = require('../server/models'); | |
_(models).each((model, key) => { | |
global[key] = new model(sequelize, Sequelize); | |
}); | |
/** | |
* create API client | |
*/ | |
global.client = new (require('./lib/api-client'))('localhost', 3000); | |
/** | |
* show info | |
*/ | |
console.log(`ENV : ${env}`); | |
console.log(`DB : ${JSON.stringify(dbconf)}`); | |
var modelName = []; | |
_(models).each((models, key) => modelName.push(key)); | |
console.log(`MODEL : [${modelName.join(', ')}]`); | |
console.log(`CLIENT : ${global.client.getHost()}, ${global.client.getPort()}`); | |
console.log('-----'); | |
var replServer = repl.start({ prompt: 'loq> ' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ディレクトリは
という構成を想定