-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
55 lines (36 loc) · 1.2 KB
/
backend.py
File metadata and controls
55 lines (36 loc) · 1.2 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
import bottle
import json
from models import Robot, model_json
from runner import run_game
@bottle.route('/')
def index():
return bottle.static_file('index.html', root='client/src/')
@bottle.route('/client/<path:path>')
def static(path):
return bottle.static_file(path, root='client')
@bottle.post('/match')
def run_match():
json = bottle.request.json
return {'history': run_game(json['player1'], json['player2'])}
@bottle.route('/v1/robots')
def robot_list():
bottle.response.content_type = 'application/json'
return json.dumps(map(model_json, Robot.select()))
@bottle.post('/v1/robots')
def robot_create():
json = bottle.request.json
new_robot = Robot.create(**json)
return {'robot': model_json(new_robot)}
@bottle.post('/v1/robots/<id:int>')
def robot_update(id):
json = bottle.request.json
robot = Robot.get(Robot.id == id)
robot.name = json['name']
robot.code = json['code']
robot.save()
return {'robot': model_json(robot)}
@bottle.delete('/v1/robots/<id:int>')
def robot_delete(id):
return {'rows': Robot.get(Robot.id == id).delete_instance()}
if __name__ == '__main__':
bottle.run(host='localhost', port=8080, debug=True, reloader=True)