-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresting.py
99 lines (80 loc) · 2.75 KB
/
resting.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
import cherrypy
import json
import pickledb
import os
DELETE_PASSWORD = "dcl1234!"
class Footy(object):
def __init__(self):
# initialisation
self.db = pickledb.load("journal.db", True)
# read the games into memory
# self.db.lgetall("games")
@cherrypy.expose
def journal(self):
# we want to read out the journal as json
games = self.db.lgetall('games')
return '{"data": ' + json.dumps(games) + '}'
@cherrypy.expose
def addgame(self, winner1="null", winner2="null", loser1="null",
loser2="null"):
# add a game.
if ((winner1 != "null") & (winner2 != "null") & (loser1 != "null") &
(loser2 != "null") & (winner1 != "") & (winner2 != "") &
(loser1 != "") & (loser2 != "")):
# game of doubles
gamedef = {}
victorlist = []
loserlist = []
victorlist.append(winner1)
victorlist.append(winner2)
loserlist.append(loser1)
loserlist.append(loser2)
gamedef['v'] = victorlist
gamedef['l'] = loserlist
print(gamedef)
self.db.ladd('games', gamedef)
# self.db.dump()
else:
if ((winner1 != "null") & (loser1 != "null") &
(winner1 != "") & (loser1 != "")):
# game of singles
gamedef = {}
victorlist = []
loserlist = []
victorlist.append(winner1)
loserlist.append(loser1)
gamedef['v'] = victorlist
gamedef['l'] = loserlist
print(gamedef)
self.db.ladd('games', gamedef)
# self.db.dump()
else:
print("Illegal game rejected")
games = self.db.lgetall('games')
return '{"data": ' + json.dumps(games) + '}'
@cherrypy.expose
def removegames(self, password):
if password == DELETE_PASSWORD:
entries = cherrypy.request.body.read().split(',')
for pos in entries:
self.db.lpop('games', int(pos))
if __name__ == '__main__':
conf = {
'/':
{'tools.staticdir.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd()),
'tools.staticdir.dir': './'
}
}
server = cherrypy._cpserver.Server()
server.socket_host = '0.0.0.0'
server.socket_port = 90
server.subscribe()
server = cherrypy._cpserver.Server()
server.socket_host = '0.0.0.0'
server.socket_port = 8081
server.subscribe()
cherrypy.server.unsubscribe()
cherrypy.tree.mount(Footy(), '/', conf)
cherrypy.engine.start()
cherrypy.engine.block()