Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions gateway-flask/README.md
Original file line number Diff line number Diff line change
@@ -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:

```
<html><body>I RESTed for 39 time units.</body></html>
```

---
## 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:
```
<html><body>I made 3 requests and it took 72 milliseconds.</body></html>
```
22 changes: 22 additions & 0 deletions gateway-flask/gateway.py
Original file line number Diff line number Diff line change
@@ -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)
44 changes: 44 additions & 0 deletions gateway-flask/restful.py
Original file line number Diff line number Diff line change
@@ -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 <head></head> etc
return '<html><body>' + content + '</body></html>'

@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)