-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
360 lines (286 loc) · 11.4 KB
/
server.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import bottle, json, uuid, os, time, base64
import hypercorn, aioquic
import hypercorn.asyncio
import hypercorn.logging
import hypercorn.middleware
import utils as su, securelogin as sl
SERVER_SECRET = os.environ.get("SERVER_SECRET") or base64.b64decode("cmFuY2h5IHNtYXNoeQ==")
app = bottle.Bottle()
bottle.TEMPLATE_PATH = ["./templates"]
database = json.load(open("./database.json"))
@app.route("/")
def index():
if not bottle.request.get_cookie("token"):
return bottle.redirect("/login")
n = 5
items = [list(database.keys())[i : i + n] for i in range(0, len(database), n)]
return bottle.template("index.stpl", data=database, items=items)
@app.route("/login")
def loginView():
return bottle.template("login.stpl", data=database)
@app.route("/register")
def registerView():
return bottle.template("register.stpl")
@app.route("/devpage")
def devpage():
return bottle.static_file("devpage.html", root="./static")
@app.route("/d1362b46-48db-407d-a9bb-3a87c8634aa6")
def maincss():
return bottle.static_file("main.min.css", root="./")
@app.route("/static/<filename>")
def static(filename):
return bottle.static_file(filename, root="./static")
@app.route("/uuid/<uuid>")
def serve_by_uuid(uuid):
item = su.find_item_by_uuid(database, uuid)
if item is None:
return bottle.abort(404, "Item not found")
return bottle.template("itemCardView.stpl", name=item, data=database[item])
@app.route("/tpl/itemview/<item>", method="GET")
def itemview(item):
if item not in database:
return bottle.abort(404, "Item not found")
return bottle.template("itemCardView.stpl", name=item, data=database[item])
@app.route("/tpl/itemviewuuid/<uuid>", method="GET")
def itemviewuuid(uuid):
item = su.find_item_by_uuid(database, uuid)
if item is None:
return bottle.abort(404, "Item not found")
return bottle.template("itemCardView.stpl", name=item, data=database[item])
@app.route("/tpl/itemUpdate/<uuid>", method="GET")
def itemUpdate(uuid):
item = su.find_item_by_uuid(database, uuid)
if item is None:
return bottle.abort(404, "Item not found")
return bottle.template("itemUpdate.stpl", name=item, data=database[item])
@app.route("/tpl/imageUpload/<uuid>", method="GET")
def imageUpload(uuid):
item = su.find_item_by_uuid(database, uuid)
if item is None:
return bottle.abort(404, "Item not found")
return bottle.template("imageUpload.stpl", name=item)
@app.route("/tpl/create")
def create():
return bottle.template("create.stpl")
@app.route("/tpl/checkout/<uuid>", method="GET")
def checkout_page(uuid):
user = sl.decode_token(bottle.request.get_cookie("token"), SERVER_SECRET)
if not user:
return bottle.abort(401, "Unauthorized")
item = su.find_item_by_uuid(database, uuid)
if item is None:
return bottle.abort(404, "Item not found")
return bottle.template("checkout.stpl", item=item, user=user, data=database[item])
@app.route("/tpl/item/<item>/<checkoutid>/returnpage", method="GET")
def return_item(item, checkoutid):
user = sl.decode_token(bottle.request.get_cookie("token"), SERVER_SECRET)
if not user:
return bottle.abort(401, "Unauthorized")
global database
if item not in database:
return bottle.abort(404, "Item not found")
return bottle.template("return.stpl", item=item, checkoutid=checkoutid, data=database[item]["checkouts"][checkoutid])
@app.route("/api/item/<item>/get", method="GET")
def item(item):
if item not in database:
return bottle.abort(404, "Item not found")
return database[item]
@app.put("/api/item/<item>/create")
def create(item):
if not sl.decode_token(bottle.request.get_cookie("token"), SERVER_SECRET):
return bottle.abort(401, "Unauthorized")
global database
if item in database:
return bottle.abort(409, "Item already exists")
database[item] = {
"name": item,
"uuid": su.generate_id(),
"description": None,
"images": {},
"amount": 0,
"checkouts": {}
}
json.dump(database, open("./database.json", "w"))
return bottle.HTTPResponse("Item created", status=201)
@app.post("/api/item/<item>/update")
def update(item):
if not sl.decode_token(bottle.request.get_cookie("token"), SERVER_SECRET):
return bottle.abort(401, "Unauthorized")
global database
if item not in database:
return bottle.abort(404, "Item not found")
try:
arc = bottle.request.json
except ValueError:
return bottle.HTTPResponse("Invalid JSON", status=400)
if not arc:
return bottle.HTTPResponse("No JSON data", status=400)
if "description" in arc:
database[item]["description"] = arc["description"]
if "amount" in arc and arc["amount"]:
database[item]["amount"] = int(arc["amount"])
if "name" in arc and arc["name"]:
database[item]["name"] = arc["name"]
# rename the whole item dictionary
temp = database[item]
del database[item]
database[arc["name"]] = temp
json.dump(database, open("./database.json", "w"))
return bottle.HTTPResponse(status=204)
@app.route("/api/item/<item>/checkout", method="PUT")
def checkout(item):
user = sl.decode_token(bottle.request.get_cookie("token"), SERVER_SECRET)
if not user:
return bottle.abort(401, "Unauthorized")
try:
arc = bottle.request.json
except ValueError:
return bottle.HTTPResponse("Invalid JSON", status=400)
if not arc:
return bottle.HTTPResponse("No JSON data", status=400)
global database
if item not in database:
return bottle.abort(404, "Item not found")
if int(arc["amount"]) > database[item]["amount"]:
return bottle.abort(400, "Not enough stock")
id = su.generate_id()
database[item]["checkouts"][id] = {
"user": user,
"timestamp": time.time(),
"amount": int(arc["amount"]),
}
database[item]["amount"] -= int(arc["amount"])
json.dump(database, open("./database.json", "w"))
return bottle.HTTPResponse(status=201)
@app.route("/api/item/<item>/getcheckouts", method="GET")
def get_checkouts(item):
user = sl.decode_token(bottle.request.get_cookie("token"), SERVER_SECRET)
if not user:
return bottle.abort(401, "Unauthorized")
global database
if item not in database:
return bottle.abort(404, "Item not found")
return database[item]["checkouts"]
@app.route("/api/item/<item>/return", method="PUT")
def return_item(item):
user = sl.decode_token(bottle.request.get_cookie("token"), SERVER_SECRET)
if not user:
return bottle.abort(401, "Unauthorized")
global database
if item not in database:
return bottle.abort(404, "Item not found")
try:
arc = bottle.request.json
except ValueError:
return bottle.HTTPResponse("Invalid JSON", status=400)
if not arc:
return bottle.HTTPResponse("No JSON data", status=400)
if not arc["id"] in database[item]["checkouts"]:
return bottle.abort(404, "Checkout not found")
amt = database[item]["checkouts"][arc["id"]]["amount"]
del database[item]["checkouts"][arc["id"]]
database[item]["amount"] += amt
json.dump(database, open("./database.json", "w"))
return bottle.HTTPResponse(status=204)
@app.route("/api/item/<item>/delete", method="DELETE")
def delete(item):
if not sl.decode_token(bottle.request.get_cookie("token"), SERVER_SECRET):
return bottle.abort(401, "Unauthorized")
global database
if item in database:
images = database[item]["images"]
for image in images:
for file in os.listdir("./images/"):
if file.startswith(image):
os.remove("./images/" + file)
break
del database[item]
json.dump(database, open("./database.json", "w"))
return bottle.HTTPResponse(status=204)
else:
return bottle.abort(404, "Item not found")
@app.route("/api/item/<item>/image", method="PUT")
def upload_image(item):
upload = bottle.request.files.get("upload")
if not upload:
return bottle.abort(400, "No image to upload")
_, ext = os.path.splitext(upload.filename)
if ext not in [".png", ".jpg", ".jpeg", ".webp"]:
return bottle.abort(400, "Only .png, .jpg, .jpeg, .webp images are allowed")
id = su.generate_id()
dest = os.path.join("./images/", id + ext)
upload.save(dest, overwrite=True)
database[item]["images"][id] = {"timestamp": int(time.time())}
json.dump(database, open("./database.json", "w"))
return bottle.HTTPResponse(id, status=201)
@app.route("/api/item/<item>/deleteimage/<id>", method="DELETE")
def delete_image(item, id):
if not sl.decode_token(bottle.request.get_cookie("token"), SERVER_SECRET):
return bottle.abort(401, "Unauthorized")
if id in database[item]["images"]:
for file in os.listdir("./images/"):
if file.startswith(id):
os.remove("./images/" + file)
break
del database[item]["images"][id]
json.dump(database, open("./database.json", "w"))
return bottle.HTTPResponse(status=204)
else:
return bottle.abort(404, "Image not found")
@app.route("/api/images/<uuid>", method="GET")
def get_image(uuid):
for file in os.listdir("./images/"):
if file.startswith(uuid):
return bottle.static_file(file, root="./images/")
return bottle.abort(404, "Image not found")
@app.post("/api/login")
def login():
try:
arc = bottle.request.json
except ValueError:
return bottle.HTTPResponse("Invalid JSON", status=400)
if not arc:
return bottle.HTTPResponse("No JSON data", status=400)
if not "username" in arc or not "password" in arc:
return bottle.HTTPResponse("No username or password provided", status=400)
if not arc["username"] or not arc["password"]:
return bottle.HTTPResponse("No username or password provided", status=400)
if not sl.check_password_against_db(arc["username"], arc["password"]):
return bottle.HTTPResponse("Invalid username or password", status=401)
token = sl.generate_token(arc["username"], SERVER_SECRET)
bottle.response.set_cookie("token", token, path="/")
@app.get("/api/checktoken")
def checktoken():
user = sl.decode_token(bottle.request.get_cookie("token"), SERVER_SECRET)
if not user:
return bottle.abort(401, "Unauthorized")
return bottle.HTTPResponse(user, status=200)
@app.put("/api/users/<user>")
def register(user):
try:
arc = bottle.request.json
except ValueError:
return bottle.HTTPResponse("Invalid JSON", status=400)
if not arc:
return bottle.HTTPResponse("No JSON data", status=400)
if "password" in arc and arc["password"]:
sl.register(user, arc["password"])
return bottle.HTTPResponse(status=201)
else:
return bottle.HTTPResponse("No password provided", status=400)
@app.route("/api/item/all", method="GET")
def all():
return json.dumps({"items": list(database.keys())})
config = hypercorn.Config()
config.bind = ["0.0.0.0:8080"]
config.accesslog = "-"
config.errorlog = "-"
config.quic_bind = ["0.0.0.0:4433"]
config.certfile = "./cert.pem"
config.keyfile = "./key.pem"
config.alpn_protocols = ["h3","h2"]
config.loglevel = "DEBUG"
import asyncio, ssl
async def main():
await hypercorn.asyncio.serve(app, config)
asyncio.run(main())