add example of asset api

parent 58cdca16
var config = require('../config.json');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : config.db.hostname,
user : config.db.username,
password : config.db.password,
database : config.db.name
});
function get_asset_id(req, res){
// Function will perform a DB query using the asset id provided
connection.query('SELECT id, name, amount, units, has_ipfs, ipfs_hash, reissuable FROM assets WHERE id=' + req.params.id, function (error, results, fields) {
if (error) throw error;
response = results[0];
res.send(response);
});
}
function get_all_assets(req, res){
if (typeof req.params.paged === 'undefined' || req.params.paged === null) {
req.params.paged = 0;
}
connection.query('SELECT id, name, amount, units, has_ipfs, ipfs_hash, reissuable FROM assets limit ' + req.params.paged + ',1000', function (error, results, fields) {
if (error) throw error;
response = {}
response['assets'] = results;
response['paging'] = (Number(req.params.paged) + Number(1000));
res.send(response);
});
}
function get_assetsbyaddress(req, res){
// Function will perform a DB query using the asset id provided
connection.query('SELECT id, name, amount, units, has_ipfs, ipfs_hash, reissuable FROM assetaddresses WHERE id=' + req.params.id, function (error, results, fields) {
if (error) throw error;
response = results[0];
res.send(response);
});
}
module.exports = function(server) {
// server.get('/appstats', tester); NOT REQUIRED
server.get('/asset/:id', get_asset_id)
server.get('/assets', get_all_assets)
server.get('/assets/:paged', get_all_assets)
server.get('/assetsbyaddress/:id', get_assetsbyaddress)
};
......@@ -4,7 +4,7 @@
* @requires './config.json'
*/
'use strict';
var restify, bunyan, Elasticsearch, esStream, routing, log, server, config;
var restify, bunyan, routing, log, server, config;
restify = require('restify');
routing = require('./routing/');
......@@ -33,12 +33,6 @@ log = bunyan.createLogger({
serializers: bunyan.stdSerializers
});
/**
* Create restify server
* pulls service name from config
* forces headers such as caching and content type
*/
server = restify.createServer({
name: config.name,
log: log,
......@@ -46,27 +40,12 @@ server = restify.createServer({
'application/json': function(req, res, body) {
//res.setHeader('Cache-Control', 'must-revalidate');
// ONLY send JSON
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('Content-Type', 'application/json');
return JSON.stringify(body);
}
}
});
/*
// Enforce Rate Limiting
server.use(restify.throttle({
burst: 200,
rate: 100,
ip: true,
overrides: {
'10.0.0.1': {
rate: 0, // unlimited
burst: 0
}
}
}));
*/
server.pre(restify.pre.sanitizePath());
/*jslint unparam:true*/
......
......@@ -26,6 +26,11 @@ bcrypt-pbkdf@^1.0.0:
dependencies:
tweetnacl "^0.14.3"
bignumber.js@4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.1.0.tgz#db6f14067c140bd46624815a7916c92d9b6c24b1"
integrity sha512-eJzYkFYy9L4JzXsbymsFn3p54D+llV27oTQ+ziJG7WFRheJcNZilgVXMG0LoZtlQSKBsJdWtLFqOD0u+U0jZKA==
brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
......@@ -127,6 +132,11 @@ ewma@^2.0.1:
dependencies:
assert-plus "^1.0.0"
extend@^3.0.0:
version "3.0.2"
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa"
integrity sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==
extsprintf@1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.2.0.tgz#5ad946c22f5b32ba7f8cd7426711c6e8a3fc2529"
......@@ -312,6 +322,16 @@ mv@~2:
ncp "~2.0.0"
rimraf "~2.4.0"
mysql@^2.16.0:
version "2.16.0"
resolved "https://registry.yarnpkg.com/mysql/-/mysql-2.16.0.tgz#b23b22ab5de44fc2d5d32bd4f5af6653fc45e2ba"
integrity sha512-dPbN2LHonQp7D5ja5DJXNbCLe/HRdu+f3v61aguzNRQIrmZLOeRoymBYyeThrR6ug+FqzDL95Gc9maqZUJS+Gw==
dependencies:
bignumber.js "4.1.0"
readable-stream "2.3.6"
safe-buffer "5.1.2"
sqlstring "2.3.1"
nan@^2.10.0:
version "2.12.1"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.12.1.tgz#7b1aa193e9aa86057e3c7bbd0ac448e770925552"
......@@ -364,7 +384,7 @@ qs@^6.5.2:
resolved "https://registry.yarnpkg.com/qs/-/qs-6.6.0.tgz#a99c0f69a8d26bf7ef012f871cdabb0aee4424c2"
integrity sha512-KIJqT9jQJDQx5h5uAVPimw6yVg2SekOKu959OCtktD3FjzbpvaPr8i4zzg07DOMz+igA4W/aNM7OV8H37pFYfA==
readable-stream@^2.0.1, readable-stream@^2.2.9:
readable-stream@2.3.6, readable-stream@^2.0.1, readable-stream@^2.2.9:
version "2.3.6"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf"
integrity sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==
......@@ -388,6 +408,13 @@ restify-errors@^5.0.0:
optionalDependencies:
safe-json-stringify "^1.0.3"
restify-paginate@^0.0.5:
version "0.0.5"
resolved "https://registry.yarnpkg.com/restify-paginate/-/restify-paginate-0.0.5.tgz#961a17b09acf58e4907875177d9a02d98571b370"
integrity sha512-NMvSd/A+tOss5eOvunwuKrMAkN3Et38lAFWREadF/bGmAnRGLeHt+d12oOhXbWj4gStuLskAncwj7HA9HuFgVQ==
dependencies:
extend "^3.0.0"
restify@^7.5.0:
version "7.5.0"
resolved "https://registry.yarnpkg.com/restify/-/restify-7.5.0.tgz#5c7d434f96b23f535649bfcd7b4ad3d5b921853f"
......@@ -429,7 +456,7 @@ rimraf@~2.4.0:
dependencies:
glob "^6.0.1"
safe-buffer@^5.0.1, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
safe-buffer@5.1.2, safe-buffer@^5.0.1, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
......@@ -491,6 +518,11 @@ spdy@^3.4.7:
select-hose "^2.0.0"
spdy-transport "^2.0.18"
sqlstring@2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/sqlstring/-/sqlstring-2.3.1.tgz#475393ff9e91479aea62dcaf0ca3d14983a7fb40"
integrity sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A=
sshpk@^1.7.0:
version "1.16.0"
resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.16.0.tgz#1d4963a2fbffe58050aa9084ca20be81741c07de"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment