diff --git a/packages/composer-rest-server/package.json b/packages/composer-rest-server/package.json index 2f6673d141..cf20213d6d 100644 --- a/packages/composer-rest-server/package.json +++ b/packages/composer-rest-server/package.json @@ -11,8 +11,12 @@ }, "main": "server/server.js", "scripts": { + "start": "node .", "lint": "eslint .", - "start": "node ." + "pretest": "npm run lint", + "postlint": "npm run licchk", + "licchk": "license-check", + "posttest": "istanbul check-coverage" }, "repository": { "type": "git", @@ -43,5 +47,29 @@ "strong-error-handler": "^1.0.1", "touch": "^1.0.0", "yargs": "^6.6.0" + }, + "devDependencies": { + "chai": "^3.5.0", + "eslint": "^3.8.1", + "ink-docstrap": "^1.3.0", + "jsdoc": "^3.4.2", + "license-check": "^1.1.5", + "loopback": "^2.35.0", + "mocha": "^3.1.2", + "sinon": "^1.17.6", + "sleep-promise": "^2.0.0" + }, + "license-check-config": { + "src": [ + "**/*.js", + "!./server/server.js", + "!./coverage/**/*", + "!./node_modules/**/*", + "!./out/**/*" + ], + "path": "header.txt", + "blocking": true, + "logInfo": false, + "logError": true } } diff --git a/packages/composer-rest-server/server/composer-rest-server.js b/packages/composer-rest-server/server/composer-rest-server.js deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/packages/composer-website/jekylldocs/start/getting-started-rest-api.md b/packages/composer-website/jekylldocs/start/getting-started-rest-api.md index 618825a8ff..9c94361afd 100644 --- a/packages/composer-website/jekylldocs/start/getting-started-rest-api.md +++ b/packages/composer-website/jekylldocs/start/getting-started-rest-api.md @@ -44,132 +44,41 @@ info: [Handel] Command completed successfully. We can see the two sample land titles listed. -## Install LoopBack +## Install the general purpose Composer REST Server -We need to install LoopBack; this is two npm modules `loopback` and `loopback-cli`. +We need to install the Fabric Composer REST server; this is an npm module named `composer-rest-server``. ->You might need to issue both of these using sudo. +>You might need to issue this command using sudo. ```bash -npm install -g loopback -npm install -g loopback-cli +npm install -g composer-rest-server ``` -## Creating the LoopBack server application - -We need to have a LoopBack server running to serve the REST api. As this is an application itself, it's worth creating a new directory for this. So assuming you're still in the Getting Started directory create a new directory as follows. - -```bash -cd .. -mkdir loopback-server -cd loopback-server -``` - -You can now create the loop back server. Issue the command `lb` in this new directory. This is the output you'll see together with the answers to the questions you'll be asked. - +## Running the REST server +You should now be able to run the Fabric Composer REST server. ```bash -$ lb - - _-----_ - | | ╭──────────────────────────╮ - |--(o)--| │ Let's create a LoopBack │ - `---------´ │ application! │ - ( _´U`_ ) ╰──────────────────────────╯ - /___A___\ / - | ~ | - __'.___.'__ - ´ ` |° ´ Y ` - -? What's the name of your application? rest -? Which version of LoopBack would you like to use? 2.x (long term support) -? What kind of application do you have in mind? api-server (A LoopBack API server with local User auth) -Generating .yo-rc.json - - -I'm all done. Running npm install for you to install the required dependencies. If this fails, try running the command yourself. - - - create .editorconfig - create .eslintignore - create .eslintrc - create server/boot/root.js - create server/middleware.development.json - create server/middleware.json - create server/server.js - create README.md - create server/boot/authentication.js - create .gitignore - create client/README.md - +composer-rest-server ``` -That is now all created - the next step is to add an adapter to link with the Fabric Composer runtime. +You will then be asked to enter a few simple details about your business network. ```bash -npm install --save loopback-connector-composer -``` - -Last step before trying things out is to add a function in the `server/boot` boot directory. This function is below - the key parts are in the `datasource` variable. These have been filled out with the same settings of the Getting Started guide. -Create a new file in this directory with the following contents, the name of the file is not important, so for example `loopback-composer.js`. - - -```javascript - -'use strict'; - -module.exports = function (server) { - - const dataSource = server.loopback.createDataSource('Concerto', { - "name": "Concerto", - "connector": "loopback-connector-composer", - "connectionProfileName" : 'defaultProfile', - "businessNetworkIdentifier" : 'digitalproperty-network', - "participantId" : 'WebAppAdmin', - "participantPwd" : 'DJY27pEnl16d' - }); - - dataSource.discoverModelDefinitions({}, (error, modelDefinitions) => { - console.log('Concerto Loopback Connector'); - - if (error) { - throw error; - } - modelDefinitions.forEach((modelDefinition) => { - console.log('found modelDefinition = ' + JSON.stringify(modelDefinition)) - dataSource.discoverSchemas(modelDefinition.name, {visited: {}, associations: true}, (error, modelSchema) => { - if (error) { - throw error; - } - // this is required because LoopBack doesn't like dots in model schema names - modelSchema.name = modelSchema.plural.replace(/\./g, '_'); - modelSchema.idInjection = false; - let model = server.loopback.createModel(modelSchema); - server.model(model, { - dataSource: dataSource, - public: true - }); - }); - }); - }); -}; -``` - -## Run LoopBack server - -Go back to the `loopback-server` directory and start the server as follows. - -```bash -$cd ../.. -$ node . -WARNING: No configurations found in configuration directory:/home/matthew/git17/rest/config + _____ _ _ ____ + | ___| __ _ | |__ _ __ (_) ___ / ___| ___ _ __ ___ _ __ ___ ___ ___ _ __ + | |_ / _` | | '_ \ | '__| | | / __| _____ | | / _ \ | '_ ` _ \ | '_ \ / _ \ / __| / _ \ | '__| + | _| | (_| | | |_) | | | | | | (__ |_____| | |___ | (_) | | | | | | | | |_) | | (_) | \__ \ | __/ | | + |_| \__,_| |_.__/ |_| |_| \___| \____| \___/ |_| |_| |_| | .__/ \___/ |___/ \___| |_| + |_| +? Enter your Fabric Connection Profile Name: defaultProfile +? Enter your Business Network Identifier : digitalproperty-network +? Enter your Fabric username : WebAppAdmin +? Enter your secret: DJY27pEnl16d +WARNING: No configurations found in configuration directory:/Users/samsmith/Projects/BlockChain/Composer/fabric-composer/packages/composer-rest-server/config WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment. -Web server listening at: http://0.0.0.0:3000 +CONNECTING...{"name":"Composer","connector":"loopback-connector-composer","connectionProfileName":"defaultProfile","businessNetworkIdentifier":"digitalproperty-network","participantId":"WebAppAdmin","participantPwd":"DJY27pEnl16d","debug":false} +Loopback Connector for Fabric Composer +Models Loaded Now Browse your REST API at http://0.0.0.0:3000/explorer -Concerto Loopback Connector -found modelDefinition = {"type":"table","name":"net.biz.digitalPropertyNetwork.LandTitle"} -found modelDefinition = {"type":"table","name":"net.biz.digitalPropertyNetwork.SalesAgreement"} -found modelDefinition = {"type":"table","name":"net.biz.digitalPropertyNetwork.Person"} - ``` ## Looking at the generated APIs diff --git a/packages/composer-website/jekylldocs/tasks/rest-api-server.md b/packages/composer-website/jekylldocs/tasks/rest-api-server.md new file mode 100644 index 0000000000..97f7db9998 --- /dev/null +++ b/packages/composer-website/jekylldocs/tasks/rest-api-server.md @@ -0,0 +1,97 @@ +--- +layout: default +title: Task - Generating a REST API Server +category: tasks +sidebar: sidebars/tasks.md +excerpt: How to generate a REST API Server for a Business Network +--- + +# How Generate a REST API Server for a Business Network + +--- +The easiest way to setup a REST API Server that works with a published Business Network is to use the composer-rest-server package. +Once installed this can be run from the command line in an interactive mode or a non-interactive mode. + +--- + +# Prerequesites + +--- +Along with the details of the business network and the connection profile that you will use to connect to it, you will also need to have the following +prerequesite software installed on your system. + +*node.js*: v6.9.5 + +Node comes with the *npm* package manager preinstalled and so once you have node installed you can install the composer-rest-server package using + +```bash +npm install -g composer-rest-server +``` + +You may need to use *sudo* with the command above to gain permission to install. + + +--- + +# Running the Server + +--- + +## Interactive Mode + +Once installed, you can start the composer-rest-server using: + +```bash +composer-rest-server +``` + +You will then be asked to enter a few simple details about your business network. + +```bash + _____ _ _ ____ + | ___| __ _ | |__ _ __ (_) ___ / ___| ___ _ __ ___ _ __ ___ ___ ___ _ __ + | |_ / _` | | '_ \ | '__| | | / __| _____ | | / _ \ | '_ ` _ \ | '_ \ / _ \ / __| / _ \ | '__| + | _| | (_| | | |_) | | | | | | (__ |_____| | |___ | (_) | | | | | | | | |_) | | (_) | \__ \ | __/ | | + |_| \__,_| |_.__/ |_| |_| \___| \____| \___/ |_| |_| |_| | .__/ \___/ |___/ \___| |_| + |_| +? Enter your Fabric Connection Profile Name: defaultProfile +? Enter your Business Network Identifier : digitalproperty-network +? Enter your Fabric username : WebAppAdmin +? Enter your secret: DJY27pEnl16d +WARNING: No configurations found in configuration directory:/Users/samsmith/Projects/BlockChain/Composer/fabric-composer/packages/composer-rest-server/config +WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment. +Loopback Connector for Fabric Composer +Models Loaded Now +Browse your REST API at http://0.0.0.0:3000/explorer +``` + +## Non-Interactive Mode + +Alternatively you can run the server specifying all the parameters on the command line +```bash +$ composer-rest-server -c car-sample -b org.acme.biznet -i WebAppAdmin -p DJY27pEnl16d +Loopback Connector for Fabric Composer +Models Loaded Now +Browse your REST API at http://0.0.0.0:3000/explorer +``` + +## Looking at the generated APIs + +Launch your browser and go to the URL given (http://0.0.0.0:3000/explorer) + +You will be able to inspect and execute the APIs that are generated from the Business Network. + + +## Debug + +If you should encounter any issues when using the composer-rest-server then you can use the following environment variable to enable extra trace information. + +```bash +export DEBUG=loopback:connector:businessnetworkconnector* +``` + +## Changing the port of the server +You can change the port of the server hosting the API by editing the file: +```bash +