This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
76 lines (60 loc) · 1.66 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
import traceback
import uuid
import sys
import flask
import flask.json
import gevent.pywsgi
import pymysql
import redis
from platformshconfig import Config
app = flask.Flask(__name__)
config = Config()
@app.route('/')
def root():
tests = {
"database": wrap_test(test_mysql, config.credentials("database")),
"redis": wrap_test(test_redis, config.credentials("rediscache"))
}
return flask.json.jsonify(tests)
def wrap_test(callback, *args, **kwargs):
try:
result = callback(*args, **kwargs)
return {
"status": "OK",
"return": result,
}
except Exception:
return {
"status": "ERROR",
"error": traceback.format_exception(*sys.exc_info())
}
def test_mysql(instance):
connection = pymysql.connect(
host=instance["host"],
port=instance["port"],
user=instance["username"],
password=instance["password"],
db=instance["path"],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor,
)
try:
with connection.cursor() as cursor:
cursor.execute("SELECT 1")
cursor.fetchone()
finally:
connection.close()
def test_redis(instance):
r = redis.StrictRedis(
host=instance["host"],
port=instance["port"],
db=0,
)
key_name = "foo-%s" + str(uuid.uuid4())
value = b"bar"
r.set(key_name, "bar")
assert value == r.get(key_name)
if __name__ == "__main__":
http_server = gevent.pywsgi.WSGIServer(('127.0.0.1', int(config.port)), app)
print("Listening on port {0}".format(config.port))
http_server.serve_forever()