-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
61 lines (43 loc) · 1.41 KB
/
server.py
File metadata and controls
61 lines (43 loc) · 1.41 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
from flask import Flask, request, abort
import redis
application = Flask(__name__)
application.config['PREFIX'] = 'terraform'
application.config['REDIS_HOST'] = 'localhost'
application.config['REDIS_PORT'] = 6379
application.config.from_envvar('BACKEND_SETTINGS', silent=True)
r = redis.Redis(
host=application.config['REDIS_HOST'],
port=application.config['REDIS_PORT'],
db=0
)
@application.route("/")
def hello():
return "Hello World!"
@application.route('/<path:path>', methods=['GET'])
def get(path):
key = "%s/state/%s" % (application.config['PREFIX'], path)
return r.get(key) or ""
@application.route('/<path:path>', methods=['POST'])
def post(path):
key = "%s/state/%s" % (application.config['PREFIX'], path)
r.set(key, request.get_data())
return ""
@application.route('/<path:path>', methods=['DELETE'])
def delete(path):
key = "%s/state/%s" % (application.config['PREFIX'], path)
r.delete(key)
return ""
@application.route('/<path:path>', methods=['LOCK'])
def lock(path):
key = "%s/lock/%s" % (application.config['PREFIX'], path)
if r.setnx(key, request.get_data()):
return ""
else:
abort(409)
@application.route('/<path:path>', methods=['UNLOCK'])
def unlock(path):
key = "%s/lock/%s" % (application.config['PREFIX'], path)
r.delete(key)
return ""
if __name__ == "__main__":
application.run(host="0.0.0.0", port=8080)