Skip to content

Instantly share code, notes, and snippets.

@LuoZijun
Created July 20, 2015 03:18
Show Gist options
  • Save LuoZijun/c2c068451784cb510a89 to your computer and use it in GitHub Desktop.
Save LuoZijun/c2c068451784cb510a89 to your computer and use it in GitHub Desktop.
fibjs.js
#!/usr/bin/env fibjs
// -*- mode: js -*-
/*
Fibjs:
web: http://fibjs.org/
Package: http://fpmjs.org/
Install:
1. $ npm install fibjs
2. Build from source code.
For Linux:
$ apt-get install g++ make cmake
$ git clone https://github.com/xicilion/fibjs.git
$ cd fibjs
$ git submodule init
$ git submodule update
$ sh build Release -j4
For Mac OSX:
$ brew install cmake
$ git clone https://github.com/xicilion/fibjs.git
$ cd fibjs
$ git submodule init
$ git submodule update
$ sh build Release -j4
http lib in fibjs
document: http://fibjs.org/d3/d69/namespacehttp.html
test: https://github.com/xicilion/fibjs/blob/master/test/http_test.js
qn-fibjs: https://github.com/Hi-Rube/qn-fibjs/blob/master/lib/up.js
var res = http.get("http://127.0.0.1/");
res.readAll() // Base64 string
res.readAll().toString() // ASCII String.
*/
var UNAUTHORIZED = "Unauthorized\n";
var METHOD_NOT_ALLOWED = "Method Not Allowed\n";
var INVALID_REQUEST = "Invalid Request\n";
var INVALID_JSON = "Invalid Json\n";
var http = require('http');
var rpc = {};
rpc.call = function (method, params, opts ){
if ( !opts ) var opts = {};
if ( !opts.callback ) opts.callback = function (){};
if ( !opts.url ) opts.url = "http://115.29.211.139:3000/";
var requestID = 1
var requestJSON = {
"jsonrpc": "2.0",
"id": requestID,
"method": method,
"params": params // ["select * from ucentermain.common_user_bd where id=875596"]
};
var error = null;
var result = null;
var data = new Buffer();
data.write( JSON.stringify(requestJSON) );
var res = http.post(opts.url, data, {'Content-Type': 'application/json' } ); // .body.read().toString()
// console.log( res );
var status_code = res.status;
if ( status_code != 200 ) {
return {"error": {'message': 'Internal error', 'code': -32603}, "result": null };
}
try{
var body = JSON.parse(res.read().toString());
if ( body.error || !body.result ) {
// error.
return {"error": body.error, "result": null };
} else {
// success.
return {"error": null, "result": body.result };
}
}catch ( e ) {
// exception.
return { "error": {"message": INVALID_JSON, "code": -32603}, "result": null };
}
};
function test(){
var url = "http://115.29.211.139:3000/";
var params = ["select * from ucentermain.common_user_bd where id=875596"];
var res = rpc.call( "index", params, { "url": url } );
if ( res.error || !res.result ) {
// sql execute fail.
var response_body = {
"jsonrpc": "2.0",
"id": null,
"error": res.error,
"result": null
};
console.log( response_body );
} else {
// success
var response_body = {
"jsonrpc": "2.0",
"id": 1,
"error": null,
"result": res.result
};
console.log( response_body );
}
console.info("\n");
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment