生成一个REST API

安装REST服务器

Hyperledger Composer REST服务器可以使用npm或Docker进行安装。

要使用npm进行安装,请运行以下命令:

  1. npm install -g composer-rest-server

要使用Docker安装REST服务器,请参阅部署REST服务器

运行REST服务器

Hyperledger Composer包含独立的Node.js进程,将业务网络公开为REST API。LoopBack框架用于生成由Swagger文档描述的Open API。

要启动REST服务器,只需输入:

  1. composer-rest-server

然后您将被要求输入关于你的业务网络的一些简单的细节。下面显示了使用已部署的业务网络的一个示例。

  1. ? Enter the name of the business network card to use: admin@basic-sample-network
  2. ? Specify if you want namespaces in the generated REST API: always use namespaces
  3. ? Specify if you want to enable authentication for the REST API using Passport: No
  4. ? Specify if you want to enable event publication over WebSockets: Yes
  5. ? Specify if you want to enable TLS security for the REST API: No
  6. To restart the REST server using the same options, issue the following command:
  7. composer-rest-server -c admin@basic-sample-network -n always -w true
  8. Discovering types from business network definition ...
  9. Discovered types from business network definition
  10. Generating schemas for all types in business network definition ...
  11. Generated schemas for all types in business network definition
  12. Adding schemas for all types to Loopback ...
  13. Added schemas for all types to Loopback
  14. Web server listening at: http://localhost:3000
  15. Browse your REST API at http://localhost:3000/explorer

composer-rest-server命令

composer-rest-server命令有多个选项用于定义安全性和身份认证:

  1. Options:
  2. -c, --card The name of the business network card to use [string]
  3. -n, --namespaces Use namespaces if conflicting types exist [string] [choices: "always", "required", "never"] [default: "always"]
  4. -p, --port The port to serve the REST API on [number]
  5. -a, --authentication Enable authentication for the REST API using Passport [boolean] [default: false]
  6. -m, --multiuser Enable multiple user and identity management using wallets (implies -a) [boolean] [default: false]
  7. -w, --websockets Enable event publication over WebSockets [boolean] [default: true]
  8. -t, --tls Enable TLS security for the REST API [boolean] [default: false]
  9. -e, --tlscert File containing the TLS certificate [string] [default: "/usr/local/lib/node_modules/composer-rest-server/cert.pem"]
  10. -k, --tlskey File containing the TLS private key [string] [default: "/usr/local/lib/node_modules/composer-rest-server/key.pem"]
  11. -h, --help Show help [boolean]
  12. -v, --version Show version number [boolean]

看看生成的API

启动浏览器并转到给定的URL(http://0.0.0.0:3000/explorer)。你会看到类似这样的屏幕。

回送1

更新REST服务器

在更新业务网络定义之后,可以更新REST服务器以生成反映业务网络定义更新的新API。

要更新REST服务器,首先必须使用ctrl-C停止REST服务器。然后可以使用composer-rest-server重新启动REST服务器。

总结

在Hyperledger Composer运行时之上使用Loopback框架使我们能够基于已部署的业务网络模型生成特定于业务领域的REST API!