forked from rmaphcr/vessel-game
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
63 lines (42 loc) · 1.84 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
from flask import Flask, request, json, jsonify, send_file, render_template
import pages
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
# Use a service account.
#remove this bit of the code when uploading to google cloud
try:
#this should run if I'm hosting the game locally on my laptop
cred = credentials.Certificate('vessel-game-987106f68973.json')
fb_app = firebase_admin.initialize_app(cred)
db = firestore.client()
except:
#this should run instead if it can't find the json file: a.k.a If I'm hosting it on google cloud
fb_app = firebase_admin.initialize_app()
db = firestore.client()
def create_app():
app = Flask(__name__)
app.register_blueprint(pages.bp)
return app
app = create_app()
@app.route("/dbwrite", methods = ['POST'])
def dbwrite():
#This function just takes whatever json string it's handed and writes it to the cloud database
#No error checking (yet) so just make sure you catch rubbish before it gets here
jsonString = json.dumps(request.json)
dbDict = json.loads(jsonString)
print(dbDict)
ref = db.collection("trueResults").add(dbDict)
respDict = {"wrote":True} #Just sends a token response back to the frontend to keep it happy
return jsonify(respDict)
@app.route("/dbfeedback", methods = ['POST'])
def dbfeedback():
#This function just takes whatever json string it's handed and writes it to the cloud database
#No error checking (yet) so just make sure you catch rubbish before it gets here
#This one is for the second collection, where feedback is stored
dbDict = request.form.to_dict()
ref = db.collection("trueFeedback").add(dbDict)
respDict = {"wrote":True} #Just sends a token response back to the frontend to keep it happy
return render_template("pages/read_form.html")
if __name__ == "__main__":
app.run()