Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8d48bb4

Browse filesBrowse files
committed
Fully integrated markdown service
1 parent 8830713 commit 8d48bb4
Copy full SHA for 8d48bb4

File tree

Expand file treeCollapse file tree

7 files changed

+39
-119
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

7 files changed

+39
-119
lines changed
Open diff view settings
Collapse file

‎run/markdown-preview/editor/Dockerfile‎

Copy file name to clipboardExpand all lines: run/markdown-preview/editor/Dockerfile
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ COPY . .
3232
# Use gunicorn webserver with one worker process and 8 threads.
3333
# For environments with multiple CPU cores, increase the number of workers
3434
# to be equal to the cores available.
35-
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app
35+
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
Collapse file

‎run/markdown-preview/editor/main.py‎

Copy file name to clipboardExpand all lines: run/markdown-preview/editor/main.py
+13-10Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,33 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from flask import Flask, request, render_template
16-
import markdown
15+
from flask import Flask, render_template, request
1716
import os
18-
import sys
17+
18+
import render
1919

2020
app = Flask(__name__)
2121

22-
@app.route('/', methods=['GET','POST'])
22+
23+
@app.route('/', methods=['GET'])
2324
def index():
25+
# Render the default template
2426
f = open('templates/markdown.md')
25-
default =f.read()
27+
default = f.read()
2628
return render_template('index.html', default=default)
2729

2830

2931
@app.route('/render', methods=['POST'])
30-
def render():
31-
f = open('templates/markdown.md')
32-
default =f.read()
33-
return markdown.markdown(default)
32+
def render_handler():
33+
body = request.get_json()
34+
data = body["data"]
35+
parsed_markdown = render.new_request(data)
36+
return parsed_markdown
3437

3538

3639
if __name__ == '__main__':
3740
PORT = int(os.getenv('PORT')) if os.getenv('PORT') else 8080
3841

3942
# This is used when running locally. Gunicorn is used to run the
4043
# application on Cloud Run. See entrypoint in Dockerfile.
41-
app.run(host='127.0.0.1', port=PORT, debug=True)
44+
app.run(host='127.0.0.1', port=PORT, debug=True)
Collapse file

‎run/markdown-preview/editor/render.py‎

Copy file name to clipboardExpand all lines: run/markdown-preview/editor/render.py
+19-33Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,40 @@
1313
# limitations under the License.
1414

1515
import os
16-
from urllib import request
16+
import urllib
1717

1818

1919
METADATA_URL = 'http://metadata.google.internal/computeMetadata/v1/'
2020
METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
2121

2222

23-
class RenderService(object):
24-
"""
25-
RenderService represents our upstream render service.
26-
"""
27-
28-
def __init__(self, URL, Authenticated):
29-
# URL is the render service address.
30-
self.url = URL
31-
# Auth determines whether identity token authentication will be used.
32-
self.authenticated = Authenticated
33-
34-
35-
def new_request(RenderService):
23+
def new_request(data):
3624
"""
3725
new_request creates a new HTTP request with IAM ID Token credential.
3826
This token is automatically handled by private Cloud Run (fully managed)
3927
and Cloud Functions.
4028
"""
41-
req = request.Request(RenderService.url)
42-
43-
if not RenderService.authenticated:
44-
response = urllib.request.urlopen(req)
4529

46-
token_url = (
47-
f"{METADATA_URL}instance/service-accounts/"
48-
f"default/identity?audience={RenderService.url}")
49-
token_req = request.Request(token_url, headers=METADATA_HEADERS)
50-
token_response = request.urlopen(token_req)
51-
token = token_response.read()
30+
url = os.environ.get('EDITOR_UPSTREAM_RENDER_URL')
31+
unauthenticated = os.environ.get('EDITOR_UPSTREAM_UNAUTHENTICATED')
5232

53-
return token
33+
req = urllib.request.Request(url, data=data.encode())
5434

35+
if not unauthenticated:
36+
token = get_token(url)
37+
req.add_header("Authorization", f"Bearer {token}")
5538

56-
def render(RenderService):
57-
"""
58-
render converts the Markdown plaintext to HTML.
59-
"""
60-
req = new_request(RenderService)
6139
response = urllib.request.urlopen(req)
62-
return response
63-
64-
40+
return response.read()
6541

6642

43+
def get_token(url):
44+
"""
45+
Retrieves the IAM ID Token credential for the url.
46+
"""
47+
token_url = (f"{METADATA_URL}instance/service-accounts/"
48+
f"default/identity?audience={url}")
49+
token_req = urllib.request.Request(token_url, headers=METADATA_HEADERS)
50+
token_response = urllib.request.urlopen(token_req)
51+
token = token_response.read()
52+
return token.decode()
Collapse file
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
Flask==1.1.1
22
pytest==5.3.2; python_version > "3.0"
33
pytest==5.3.2; python_version < "3.0"
4-
gunicorn==20.0.4
5-
Markdown==3.2.1
4+
gunicorn==20.0.4
Collapse file

‎run/markdown-preview/editor/service.py‎

Copy file name to clipboardExpand all lines: run/markdown-preview/editor/service.py
-70Lines changed: 0 additions & 70 deletions
This file was deleted.
Collapse file

‎run/markdown-preview/renderer/Dockerfile‎

Copy file name to clipboardExpand all lines: run/markdown-preview/renderer/Dockerfile
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ COPY . .
3232
# Use gunicorn webserver with one worker process and 8 threads.
3333
# For environments with multiple CPU cores, increase the number of workers
3434
# to be equal to the cores available.
35-
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app
35+
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
Collapse file

‎run/markdown-preview/renderer/main.py‎

Copy file name to clipboardExpand all lines: run/markdown-preview/renderer/main.py
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@
1515
from flask import Flask, request
1616
import markdown
1717
import os
18-
import sys
18+
1919

2020
app = Flask(__name__)
2121

22+
2223
@app.route('/', methods=['POST'])
2324
def index():
2425
data = request.get_data(as_text=True)
2526
return markdown.markdown(data)
2627

28+
2729
if __name__ == '__main__':
2830
PORT = int(os.getenv('PORT')) if os.getenv('PORT') else 8080
2931

3032
# This is used when running locally. Gunicorn is used to run the
3133
# application on Cloud Run. See entrypoint in Dockerfile.
32-
app.run(host='127.0.0.1', port=PORT, debug=True)
34+
app.run(host='127.0.0.1', port=PORT, debug=True)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.