4.2 Case Studies of Server Deployment
Key Points
- Server deployment case (scheduled framework)
- Server deployment case (front-end event)
- Server deployment case (Webhook)
Preparation Before Class
None.
Course Content
Backend Application Case (Scheduled Framework)
import json
from apscheduler.schedulers.blocking import BlockingScheduler
import requests
def get_jodoo_data():
url = "https://www.jodoo.com/api/v1/app/5afe2461743ef87fde26a894/entry/5c12198c99b0d10dc49edfff/data"
payload = {}
headers = {
'Authorization': 'Bearer ????????????????????'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text)
if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(get_jodoo_data, 'cron', day_of_week='*' ,hour='14', minute='05', second='00')
scheduler.start()
Backend Application Case (Front-End Event)
Type | Parameter | Data Format |
Input parameter | x | JSON |
Return parameter | y | JSON |
from flask import Flask,request
import json
app = Flask(__name__)
def calc(x):
y = x + 10
return y
@app.route('/test/' ,methods=['POST'])
def hello_world():
x = json.loads(request.data)['x']
y = calc(int(x))
return json.dumps({'y':y})
if __name__ == '__main__':
app.run(host='0.0.0.0',port=3100)
Server Deployment Case (Webhook)
from flask import Flask,request
import json
import threading
app = Flask(__name__)
def calc(x):
y = x + 10
print(y)
@app.route('/test/' ,methods=['POST'])
def hello_world():
x = json.loads(request.data)['data']['_widget_1586252392790']
threading.Thread(target=calc,args=(int(x),)).start()
return 'success',200
if __name__ == '__main__':
app.run(host='0.0.0.0',port=3100)
Assignments
1. Implement volume calculation (v=xyz) using the front-end event.
Hint: add four controls (x, y, z, v) to the form. After you enter x, y, and z (z is the field that triggers the event), the front-end event will be triggered to call the API. It will then calculate and write back to the v control.
2. Implement volume calculation (v=xyz) using Webhook.
Hint: Add four controls (x, y, z, v) to the form. After you enter x, y, and z and submit data, the Webhook will be triggered to push the data to ECS for volume calculation. After calculating v, use the data API to write the result back to the corresponding data.
from flask import Flask,request
import json
import threading
import requests
app = Flask(__name__)
def post_to_jodoo(v,data_id):
pass
def calc(x,y,z,data_id):
pass
@app.route('/test/' ,methods=['POST'])
def hello_world():
x = json.loads(request.data)['data']['_widget_1586252719049']
y = json.loads(request.data)['data']['_widget_1586252719064']
z = json.loads(request.data)['data']['_widget_1586252719079']
data_id = json.loads(request.data)['data']['_id']
threading.Thread(target=calc,args=(float(x),float(y),float(z),data_id,)).start()
return 'success',200
if __name__ == '__main__':
app.run(host='0.0.0.0',port=3100)