5. 체인코드 연동 Flask 웹어플리케이션 만들기
페이지 정보
작성자 관리자 댓글 0건 조회 3,005회 작성일 19-08-26 13:06본문
Hyperledger Fabric과 Flask 웹어플리케이션 서버 구성
쉘프로그램 작성
웹어플리케이션에서 호출할 쉘프로그램을 만든다.
# wget https://www.leelab.co.kr/down/fabric-samples/query-users.sh -O scripts/query-users.sh
# cat scripts/query-users.sh
root@client:~/hyperledger/fabric-samples/first-network# wget https://www.leelab.co.kr/down/fabric-samples/query-users.sh -O scripts/query-users.sh
--2019-11-30 03:49:19-- https://www.leelab.co.kr/down/fabric-samples/query-users.sh
Resolving www.leelab.co.kr (www.leelab.co.kr)... 202.31.147.218
접속 www.leelab.co.kr (www.leelab.co.kr)|202.31.147.218|:443... 접속됨.
HTTP request sent, awaiting response... 200 OK
Length: 109 [application/x-sh]
Saving to: ‘scripts/query-users.sh’
scripts/query-users.sh 100%[=========================================>] 109 --.-KB/s in 0s
2019-11-30 03:49:19 (2.80 MB/s) - ‘scripts/query-users.sh’ saved [109/109]
root@client:~/hyperledger/fabric-samples/first-network# cat scripts/query-users.sh
#!/bin/bash
USERID="$1"
peer chaincode query -C mychannel -n mycc -c '{"Args":["user_read", "'$USERID'" ]}'
root@client:~/hyperledger/fabric-samples/first-network#
root@client:~/hyperledger/fabric-samples/first-network# chmod 755 scripts/query-users.sh
Flask 웹어플리케이션 만들기
터미널2에서 Flask 웹어플리케이션을 작성한다.
root@client:~# cd pyApp/
root@client:~/pyApp# mkdir -p myPS/templates
root@client:~/pyApp# cd myPS/
root@client:~/pyApp/myPS# vi app.py
from flask import Flask, render_template
import json
import os
import sys
import subprocess
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('hello.html' )
@app.route('/user/<userid>')
def user(userid):
result = subprocess.check_output ('FABRIC_CFG_PATH=/root/hyperledger/fabric-samples/first-network/ docker exec cli scripts/query-users.sh '+userid , shell=True)
print(result)
userinfo = json.loads(result)
return render_template('user_read.html', userid=userid, userinfo=userinfo)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000,debug=True)
root@client:~/pyApp/myPS# vi templates/hello.html
<h1>Hello World!</h1>
root@client:~/pyApp/myPS# vi templates/user_read.html
<H1>User Info</H1>
<p>Name : {{userinfo.NM}}</p>
웹어플리케이션을 구동한다.
root@client:~/pyApp/myPS# python app.py
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 301-791-187
웹브라우저로 http://192.168.0.105:8000/user/admin 을 입력하면 터미널2의 웹어플리케이션 로그에서 블록체인에서 받아온값을 확인할 수 있다.
{"NM":"LeeJinKwan","PW":"1234","PN":"0","CC":"0","AP":"0"}
192.168.0.101 - - [30/Nov/2019 04:17:26] "GET /user/admin HTTP/1.1" 200 -
소스 다운로드
# wget https://www.leelab.co.kr/down/fabric-samples/myPS.tar.gz
첨부파일
- myPS.tar.gz (642byte) 30회 다운로드 | DATE : 2019-11-30 04:41:08
댓글목록
등록된 댓글이 없습니다.