-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
70 lines (62 loc) · 1.69 KB
/
index.py
File metadata and controls
70 lines (62 loc) · 1.69 KB
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
66
67
68
69
70
import flask
import json
import os
from flask_pymongo import PyMongo
from flask import request, jsonify
from flask_cors import CORS, cross_origin
from flask import render_template
import creds
from stack import Stack
def get_stack_from_db():
stackdb=mongo.db.stack.find({"name" :"stack0"})[0]
stack._stack['data'] = stackdb['data']
stack._data_validate()
return "OK",200
def dbupdate():
temp={
"$set":{
"data":stack.stack(),
"size":stack.size()
}
}
try :
mongo.db.stack.update_one({"name":"stack0"},temp)
except:
return "Internal Server Error With DB", 500
return "DB UPDATE OK",200
# APP CONFIG
app = flask.Flask(__name__)
CORS(app, support_credentials=True)
app.config["DEBUG"] = False
MONGO_DB_URI = os.environ['MONGO_DB_URI']
app.config['MONGO_URI'] = MONGO_DB_URI
mongo = PyMongo(app)
stackdb=mongo.db.stack.find({"name" :"stack0"})[0]
stack = Stack(elements=stackdb['data'])
@app.route("/")
def index():
return render_template("index.html")
@app.route('/stack',methods=['GET'])
def view_stack():
get_stack_from_db()
return json.dumps(stack.raw_stack()),200
@app.route('/pop',methods=['GET'])
def pop():
stack.pop()
try :
dbupdate()
except:
return "Internal Server Error With DB", 500
return {"POP":"OK"},200
@app.route('/push',methods=['GET'])
def push():
if 'data' not in request.args:
return "Bad Request Please Pass the data in the Quary Parameters",400
stack.push(request.args['data'])
try :
dbupdate()
except:
return "Internal Server Error With DB", 500
return {"PUSH":" OK"},200
if __name__ == "__main__":
app.run(host='0.0.0.0')