diff --git a/gateway-flask/README.md b/gateway-flask/README.md new file mode 100644 index 0000000..4c08370 --- /dev/null +++ b/gateway-flask/README.md @@ -0,0 +1,50 @@ +## Stage 1 + +### Step 1: + +Start the server + +``` +./server_osx +``` + +### Step 2: +Start the python app. + +``` +python3 ./restful.py +``` +The Gatewway then will run on http://127.0.0.1:5000/ + + +### Step 3: + +Hit + +``` +curl localhost:5000 +``` +Result: + +``` +I RESTed for 39 time units. +``` + +--- +## Stage 2 +### Step 1 & 2: +Do Step 1 and Step 2 like the steps in stage 1 mentioned above. + +### Step 3: +At the command line, hit + +``` +curl -X POST localhost:5000/someEndPoint -d '{"numRequests": 3 }' +``` + +You can change the numRequests value. + +Result: +``` +I made 3 requests and it took 72 milliseconds. +``` \ No newline at end of file diff --git a/gateway-flask/gateway.py b/gateway-flask/gateway.py new file mode 100644 index 0000000..b6f1eb8 --- /dev/null +++ b/gateway-flask/gateway.py @@ -0,0 +1,22 @@ +## TO INSTALL FLASK + ## Optional: setup a virtual environment + # python3 -m venv venv + # . venv/bin/activate + ## Install Flask and the requests package + # pip install Flask + # pip install requests +### TO RUN FLASK + # export FLASK_APP=gateway.py + # flask run + + +from flask import Flask +app = Flask(__name__) + +import requests + +@app.route('/') +def hello_world(): + req = requests.get("http://localhost:8080/request") + return 'Hello, World! Response was: ' \ + + str(req.text) diff --git a/gateway-flask/restful.py b/gateway-flask/restful.py new file mode 100755 index 0000000..1678b89 --- /dev/null +++ b/gateway-flask/restful.py @@ -0,0 +1,44 @@ +#!flask/bin/python + + +from flask import Flask +import requests +from flask import request +import json + +app = Flask(__name__) + +def html(content): # Also allows you to set your own etc + return '' + content + '' + +@app.route('/') +def singleReq(): + req = requests.get("http://localhost:8080/request") + + resTime = req.json() + + return html('I RESTed for ' + str(resTime["data"]) + ' time units.' + '\n') + +@app.route('/someEndPoint', methods = ['POST']) +def nRequest(): + reqData = request.get_json(force=True) + nValue = reqData["numRequests"] + + totalTime = 0 + + for i in range(nValue): + req = requests.get("http://localhost:8080/request") + resData = req.json() + + totalTime += resData["data"] + + + return html('I made ' + str(nValue) + ' requests and it took ' + str(totalTime) + ' milliseconds.' + '\n') + + + + + + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file