Skip to content

Commit e7d5d55

Browse files
committed
Squashed commit of the following:
commit 766e15e6f83800936f704d4c73d6c1db95f09835 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Thu Feb 15 14:22:42 2018 +0530 IC commit 6e3aaf4be19f241be645c1cc8f2161374ad94adf Author: Tanmai Gopal <tanmaig@34cross.in> Date: Thu Feb 15 14:21:27 2018 +0530 IC commit d273524 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Thu Feb 15 13:27:40 2018 +0530 Fixes filestore download link. commit b706bc1 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Thu Feb 15 11:45:55 2018 +0530 IC commit 0a30261 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Thu Feb 15 11:45:12 2018 +0530 IC commit ffcfa19 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Thu Feb 15 11:44:38 2018 +0530 IC commit 885f686 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Thu Feb 15 11:43:44 2018 +0530 IC commit 3fd46e3 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 21:38:56 2018 +0530 IC commit 4560ba0 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 21:33:03 2018 +0530 IC commit 4efa843 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 20:57:02 2018 +0530 IC commit 899a0cb Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 20:55:32 2018 +0530 IC commit b4bb636 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 20:52:27 2018 +0530 IC commit 4e06a84 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 20:52:06 2018 +0530 IC commit 1aae089 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 19:55:56 2018 +0530 IC commit 9c22dec Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 19:52:56 2018 +0530 IC commit def5bd9 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 19:52:07 2018 +0530 IC commit 1ac5749 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 19:47:07 2018 +0530 Add upload file.. commit 5836c75 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 18:51:56 2018 +0530 adds auth templates commit 5d2823c Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 18:51:45 2018 +0530 Adds auth page commit ca01e15 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 18:02:51 2018 +0530 'IC' commit 7109b22 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 15:52:32 2018 +0530 IC commit e9783a3 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 15:51:15 2018 +0530 IC commit b7bd984 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 15:41:23 2018 +0530 IC commit 7c90582 Author: Tanmai Gopal <tanmaig@34cross.in> Date: Wed Feb 14 15:34:49 2018 +0530 Updates many things.
1 parent 5c9b544 commit e7d5d55

File tree

13 files changed

+439
-296
lines changed

13 files changed

+439
-296
lines changed

README.md

Lines changed: 82 additions & 247 deletions
Large diffs are not rendered by default.

microservices/app/src/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
# This line adds the hasura example routes form the hasura.py file.
66
# Delete these two lines, and delete the file to remove them from your project
7-
from .hasura import hasura_examples
8-
app.register_blueprint(hasura_examples)
7+
# from .hasura import hasura_example
8+
# app.register_blueprint(hasura_examples)
99

1010
from .server import *
11+
from .data import *
12+
from .auth import *
13+
from .filestore import *

microservices/app/src/auth.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import requests, json
2+
from flask import jsonify, request, render_template
3+
from src import app
4+
5+
@app.route("/examples/auth")
6+
def user_info():
7+
"""
8+
An example of using Hasura's auth UI kit & API gateway
9+
to avoid writing auth and session handling code.
10+
11+
Hasura's auth microservice provides login/signup functionality
12+
and can redirect back to your own app.
13+
14+
Hasura's API gateway will automatically resolve session tokens into
15+
HTTP headers containing user-id and roles.
16+
"""
17+
# print (str(request.headers))
18+
19+
# Important for local development, ignore otherwise.
20+
# Local development will need you to add custom headers to simulate the API gateway.
21+
if 'x-hasura-allowed-roles' not in [x.lower() for x in request.headers.keys()]:
22+
return """This route can only be accessed
23+
via the Hasura API gateway.
24+
Add headers via your browser if you're testing locally.<br/>
25+
<a href="https://docs.hasura.io/0.15/manual/gateway/session-middleware.html"
26+
target="_blank">Read the docs.</a>"""
27+
28+
# If user is not logged in
29+
# render the HTML page with a login link.
30+
if ('anonymous' in request.headers['x-hasura-allowed-roles']):
31+
return render_template(
32+
'auth_anonymous.html',
33+
**{'base_domain': request.headers['X-Hasura-Base-Domain']}
34+
)
35+
36+
# If the user is logged in
37+
# render the HTML template showing the user's auth info
38+
else:
39+
return render_template(
40+
'auth_user.html',
41+
**{
42+
'base_domain': request.headers['X-Hasura-Base-Domain'],
43+
'user_id': request.headers['X-Hasura-User-Id'],
44+
'roles': request.headers['X-Hasura-Allowed-Roles']
45+
}
46+
)

microservices/app/src/data.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import requests, json
2+
from flask import request, render_template
3+
from src import app
4+
5+
# // For local development,
6+
# // First: connect to Hasura Data APIs directly on port 9000
7+
# // $ hasura ms port-forward data -n hasura --local-port=9000
8+
# // Second: Uncomment the line below
9+
# dataUrl = 'http://localhost:9000/v1/query'
10+
11+
# When deployed to your cluster, use this:
12+
dataUrl = 'http://data.hasura/v1/query'
13+
14+
@app.route("/examples/data")
15+
def get_articles():
16+
if ('hasura-app.io' in request.url_root) or \
17+
('data.hasura' not in dataUrl):
18+
19+
query = {
20+
"type": "select",
21+
"args": {
22+
"table": "article",
23+
"columns": [ "title", "id", "author_id", "rating", "title" ],
24+
"limit": 10
25+
}
26+
}
27+
28+
response = requests.post(dataUrl, data=json.dumps(query))
29+
if response.status_code == 200:
30+
return render_template('data.html', data=json.dumps(response.json(), indent=2, sort_keys=True))
31+
else:
32+
return 'Something went wrong: <br/>' + str(response.status_code) + '<br/>' + response.text
33+
34+
# Change the data URL during local development
35+
return ("""Edit the dataUrl variable in
36+
<code>microservices/app/src/hasura.py</code>
37+
to test locally.""")

microservices/app/src/filestore.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import requests, json
2+
from flask import jsonify, request, render_template
3+
from src import app
4+
5+
# // For local development,
6+
# // First: connect to Hasura Data APIs directly on port 9000
7+
# // $ hasura ms port-forward data -n hasura --local-port=9000
8+
# // Second: Uncomment the line below
9+
# dataUrl = 'http://localhost:9000/v1/query'
10+
11+
# When deployed to your cluster, use this:
12+
dataUrl = 'http://data.hasura/v1/query'
13+
14+
@app.route("/examples/filestore")
15+
def user_files():
16+
"""
17+
This route renders a file-upload page for users.
18+
19+
Not logged-in users (anonymous):
20+
> request that they login
21+
22+
logged-in users (anonymous):
23+
> list files they own
24+
> show file-upload box
25+
26+
The file-upload and download API uses hasura's filestore APIs
27+
so you will notice that this code has no file-handling code!
28+
29+
"""
30+
print (str(request.headers))
31+
32+
# Important only for local development. Ignore otherwise.
33+
# Local development will need you to add custom headers
34+
if 'x-hasura-allowed-roles' not in [x.lower() for x in request.headers.keys()]:
35+
return """This route can only be accessed
36+
via the Hasura API gateway.
37+
Deploy with <code>git push</code> and then test this route."""
38+
39+
# If user is not logged in
40+
if ('anonymous' in request.headers['x-hasura-allowed-roles']):
41+
return render_template(
42+
'filestore_anonymous.html',
43+
**{'base_domain': request.headers['X-Hasura-Base-Domain']}
44+
)
45+
46+
# If user is logged in, show the user files they have uploaded
47+
else:
48+
# Query from the file-upload table to fetch files this user owns.
49+
# We're using the Hasura data APIs to query
50+
requestPayload = {
51+
"type": "select",
52+
"args": {
53+
"table": {
54+
"name": "hf_file",
55+
"schema": "hf_catalog"
56+
},
57+
"columns": [ "*" ],
58+
"where": {"user_id": request.headers['x-hasura-user-id']}
59+
}
60+
}
61+
62+
resp = requests.post(dataUrl, data=json.dumps(requestPayload))
63+
64+
# resp.content contains the json response.
65+
if not(resp.status_code == 200):
66+
print (resp.text)
67+
return "Something went wrong while trying to fetch files: " + resp.text
68+
69+
files = resp.json()
70+
return render_template('filestore_user.html',
71+
**{
72+
'base_domain': request.headers['X-Hasura-Base-Domain'],
73+
'files': files
74+
})

microservices/app/src/hasura.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

microservices/app/src/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from src import app
2+
from flask import render_template
23
# from flask import jsonify
34

4-
55
@app.route("/")
66
def home():
7-
return "Hasura Hello World"
7+
return render_template('index.html')
88

99
# Uncomment to add a new URL at /new
1010

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Hello world - Python Flask</title>
5+
<style>
6+
body {font-family: sans-serif; line-height: 23px;}
7+
code {color: #222; background: #f9f9f9;}
8+
</style>
9+
</head>
10+
<body style="color: #333">
11+
<div style="max-width: 800px; margin: 0 auto;">
12+
<a href="/">Back to /</a>
13+
<div style="float: right;">[Source: <code>microservices/app/src/auth.py</code>]</div>
14+
<br/>
15+
<h1>You are not logged in.</h1>
16+
<hr/>
17+
<a href="http://auth.{{base_domain}}/ui/login?redirect_url=https%3A%2F%2Fapp.abaft85.hasura-app.io%2Fexamples%2Fauth">Login</a>
18+
</div>
19+
</body>
20+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Hello world - Python Flask</title>
5+
<style>
6+
body {font-family: sans-serif; line-height: 23px;}
7+
code {color: #222; background: #f9f9f9;}
8+
</style>
9+
</head>
10+
<body style="color: #333">
11+
<div style="max-width: 800px; margin: 0 auto;">
12+
<a href="/">/</a>
13+
<div style="float: right;">[Source: <code>microservices/app/src/auth.py</code>]</div>
14+
<br/>
15+
<h1>You are logged in!</h1>
16+
<hr/>
17+
<label>User Id:</label> {{user_id}}<br/>
18+
<label>Roles:</label> {{roles}}<br/>
19+
<hr/>
20+
<a href="http://auth.{{base_domain}}/ui/logout?redirect_url=https%3A%2F%2Fapp.abaft85.hasura-app.io%2Fexamples%2Fauth">Logout</a>
21+
</div>
22+
</body>
23+
</html>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Hello world - Python Flask</title>
5+
<style>
6+
body {font-family: sans-serif; line-height: 23px;}
7+
code {color: #222; background: #f9f9f9;}
8+
</style>
9+
</head>
10+
<body style="color: #333">
11+
<div style="max-width: 800px; margin: 0 auto;">
12+
<a href="/">Back to /</a>
13+
<div style="float: right;">[Source: <code>microservices/app/src/data.py</code>]</div>
14+
<br/>
15+
<h1>Articles fetched from article table</h1>
16+
<hr/>
17+
Run:<br/>
18+
<code> $ hasura api-console </code><br/>
19+
to explore Hasura Data APIs.<br/><br/>
20+
21+
This data is fetched from postgres via JSON data APIs.<br/>
22+
Code written in python on flask makes the data API call and renders this template.
23+
<hr/>
24+
<pre style=" white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">
25+
{{data}}
26+
</pre>
27+
</div>
28+
</body>
29+
</html>

0 commit comments

Comments
 (0)