Big refactor

This commit is contained in:
Stephen Cochrane 2021-02-11 17:48:46 +02:00
parent 2521a4bcf6
commit 6ca85cfc5b
3 changed files with 83 additions and 14 deletions

View File

@ -0,0 +1,19 @@
<style>
body {
background-color:black;
color:white
}
.header {
padding: 60px;
text-align: center;
font-size: 30px;
}
.fail {
text-align: center;
color:red
}
</style>
<div class=header>ERROR</div>
<div class=fail><h2>404</h2></div>

View File

@ -19,6 +19,14 @@
font-size:15
}
.succ {
color:lime
}
.fail {
color:red
}
input[type=text] {
border: 2px solid black;
border-radius: 4px;
@ -26,7 +34,15 @@
</style>
<div class=header>{{ TITLE }}</div>
<div class=para>
{% for res in results %}
<p>{{ res }}</p>
{% autoescape false %}
{{ INFO }}
{% endautoescape %}
<h2>Status</h2>
{% for res in up%}
<p class=succ>{{ res }}</p>
{% endfor %}
{% for res in down%}
<p class=fail>{{ res }}</p>
{% endfor %}
{{ ERROR }}
</div>

View File

@ -5,30 +5,64 @@ from datetime import datetime
import os
# My libs
main_domain = 'skiqqy.xyz'
domains = ['git', 'irc', 'proj', 'blog', 'pay', 'wiki', 'files', 'social', 'music', 'dev'] # TODO: Save this in file to reduce coupling with dns.py
domain = 'skiqqy.xyz'
domains = ['git', 'irc', 'proj', 'blog', 'wiki', 'music', 'files', 'social', 'dev'] # TODO: Save this in file to reduce coupling with dns.py
app = Flask(__name__)
### Helper Functions ###
def setup():
app.template_folder = "../assets/api_templates/"
def getip(dom):
return os.popen('ping -c 1 -w 5 %s | head -1 | cut -d \" \" -f 3' % dom).read()
# Checks to see that we are not being redirected to an nginx page.
def not_nginx(dom):
# Yes i know i can use requests, and I know its not efficeint to pass
# calls to the shell, but this is just temporary.
nginx = os.popen('curl -s %s | head -5 | grep nginx' % dom).read()
if nginx == '':
return True
return False
def is_service_up(dom):
if sip == getip(dom) and not_nginx('https://' + dom):
return True
return False
### End Helper Functions ###
sip = getip(domain)
@app.route('/')
def main():
return render_template('index.html')
# Check domain status -> Mainly used to check that ip's have updated after loadshedding.
@app.route('/domain/status/<dom>', methods = ["GET"])
def domstatus(dom):
results = []
info = "<div class=succ>Green: Service is up </div><div class=fail>Red: Service is down</div>"
up = []
down = []
if dom == 'all':
for sub in domains:
ip = os.popen('ping -c 1 -w 5 %s.%s | head -1 | cut -d \" \" -f 3' % (sub, main_domain)).read()
results.append("%s.%s:%s" % (sub, main_domain, ip))
res = "%s.%s" % (sub, domain)
if is_service_up(sub + '.' + domain):
up.append(res)
else:
down.append(res)
elif dom not in domains:
results = ["unknown domain"]
return render_template('domain_status.html', ERROR="Unknown Domain", TITLE="ERROR")
else:
ip = os.popen('ping -c 1 -w 5 %s.%s | head -1 | cut -d \" \" -f 3' % (dom, main_domain)).read()
results.append("%s.%s:%s" % (dom, main_domain, ip))
return render_template('domain_status.html', results=results, TITLE=str("Results for " + dom))
res = "%s.%s" % (dom, domain)
if is_service_up(dom + '.' + domain):
up.append(res)
else:
down.append(res)
return render_template('domain_status.html', up=up, down=down, TITLE=str("Results for " + dom), INFO=info)
def setup():
app.template_folder = "../assets/api_templates/"
@app.errorhandler(404)
def err(error):
return render_template('404.html'), 404
setup()