-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
65 lines (50 loc) · 1.55 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask
from flask import Response
from flask import json
from flask import redirect
from flask import render_template
from flask import request
from flask import send_from_directory
from flask import url_for
from werkzeug.utils import escape
import cloudstorage as gcs
from google.appengine.api import app_identity
app = Flask(__name__)
app.config['DEBUG'] = True
# Note: We don't need to call run() since our application is embedded within
# the App Engine WSGI application server.
def get_filename():
bucket_name = app_identity.get_default_gcs_bucket_name()
return '/' + bucket_name + '/post-data'
@app.route('/')
def index():
return redirect(url_for('oneshot'))
@app.route('/oneshot/')
def oneshot():
string_json = do_get()
obj = json.loads(string_json)
return render_template('index.html', text=obj['text'])
@app.route('/oneshot/oneshot_sw.js')
def oneshot_sw():
# This needs to be routed through /oneshot/ for service worker scoping
return send_from_directory('static', 'oneshot_sw.js')
@app.route('/oneshot/get')
def do_get():
filename = get_filename()
gcs_file = gcs.open(filename)
txt = gcs_file.read()
gcs_file.close()
return json.dumps({"text": txt})
@app.route('/oneshot/post', methods=['POST'])
def do_post():
obj = json.loads(request.data)
txt = escape(obj['text']).encode('utf-8')
filename = get_filename()
gcs_file = gcs.open(filename, 'w' )
gcs_file.write(txt)
gcs_file.close()
return do_get()
@app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
return 'Sorry, nothing at this URL.', 404