Skip to content

Instantly share code, notes, and snippets.

@nukosuke
Created May 30, 2016 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nukosuke/5ebbc527bd5795ee4e5d03de80ed2375 to your computer and use it in GitHub Desktop.
Save nukosuke/5ebbc527bd5795ee4e5d03de80ed2375 to your computer and use it in GitHub Desktop.
REPLで作ったハリボテAPIクライアント
'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}`;
}
}
#!/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> ' });
@nukosuke
Copy link
Author

ディレクトリは

/bin/
  |- lib/api-client.js
  |- console

という構成を想定

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment