-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
125 lines (106 loc) · 3.29 KB
/
app.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
from flask import Flask, Response
from prometheus_flask_exporter import PrometheusMetrics
from flask_sqlalchemy import SQLAlchemy
import sqlalchemy
import os
import sys
import random
import platform
from config import Config
animal = Config.ANIMAL
hostname = platform.node()
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
metrics = PrometheusMetrics(app)
print(app.config)
db = SQLAlchemy(app)
class Type(db.Model):
__tablename__ = 'types'
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.String(128))
def __init__(self, type):
self.type = type
def __repr__(self):
return f"{self.type}"
class Name(db.Model):
__tablename__ = 'names'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128))
def __init__(self, name):
self.name = name
def __repr__(self):
return f"{self.name}"
def populate_types():
for t in ["cat", "dog"]:
exists = Type.query.filter_by(type=t).first()
if not exists:
a = Type(type=t)
db.session.add(a)
def populate_names():
for n in list(open("data/names")):
n = n.rstrip()
exists = Name.query.filter_by(name=n).first()
if not exists:
a = Name(name=n)
db.session.add(a)
@app.route('/readyz')
@metrics.do_not_track()
def readyz():
if animal:
try:
exists = Type.query.filter_by(type=animal).first()
except sqlalchemy.exc.DatabaseError:
db.create_all()
except Exception as e:
return Response(f"Database error: {e}", mimetype='text/plain',status=500)
if exists:
return Response("ok", mimetype='text/plain',status=200)
else:
return Response(f"missing type", mimetype='text/plain',status=500)
return Response(f"unknown type {animal}", mimetype='text/plain',status=500)
@app.route('/livez')
@metrics.do_not_track()
def livez():
return Response('ok', mimetype='text/plain',status=200)
@app.route('/')
def catdog():
body = ''
status = 500
try:
names = list(Name.query.all())
except sqlalchemy.exc.DatabaseError:
db.create_all()
return Response(f"There is no {animal} living on {hostname}.", mimetype='text/plain', status=200)
except sqlalchemy.exc.DisconnectionError:
return Response(f"Database disconnected.", mimetype='text/plain', status=500)
except Exception as e:
return Response(f"Database error: {e}", mimetype='text/plain', status=500)
if animal:
if names:
name = random.choice(names)
body = f"It's a {animal} named {name} living on {hostname}."
else:
body = f"There is no {animal} living on {hostname}."
status = 200
else:
body = f"It's something weird on {hostname}."
status = 500
return Response(body, mimetype='text/plain', status=status)
@app.route('/version')
def version():
return Response(f"{Config.VERSION}", mimetype='text/plain', status=200)
if __name__ == "__main__":
with app.test_request_context():
try:
db.create_all()
except sqlalchemy.exc.OperationalError as e:
print(f"Can't connect to database.")
sys.exit(-1)
except Exception as e:
print(f"Database error: {e}")
sys.exit(-2)
populate_names()
populate_types()
db.session.commit()
app.run(port=Config.PORT, host=Config.HOST)