- show config and logs in webserver
This commit is contained in:
mcinj 2022-05-13 16:55:52 -04:00
parent 4f4709776b
commit f60220f302
6 changed files with 172 additions and 14 deletions

View file

@ -4,3 +4,4 @@ urllib3==1.26.9
sqlalchemy==1.4.36 sqlalchemy==1.4.36
sqlalchemy_utils==0.38.2 sqlalchemy_utils==0.38.2
python-dateutil==2.8.2 python-dateutil==2.8.2
Flask==2.1.2

View file

@ -25,8 +25,8 @@ def choose_user_agent():
] ]
return user_agents[randrange(0, len(user_agents))] return user_agents[randrange(0, len(user_agents))]
class ConfigReader(ConfigParser):
class ConfigReader(ConfigParser):
required_values = { required_values = {
'DEFAULT': { 'DEFAULT': {
@ -44,6 +44,10 @@ class ConfigReader(ConfigParser):
}, },
'NOTIFICATIONS': { 'NOTIFICATIONS': {
'pushover.enabled': ('true', 'false'), 'pushover.enabled': ('true', 'false'),
},
'WEB': {
'web.enabled': ('true', 'false'),
'web.port': '%s' % (value_range(1, 65535))
} }
} }
default_values = { default_values = {
@ -68,7 +72,10 @@ class ConfigReader(ConfigParser):
'pushover.enabled': 'false', 'pushover.enabled': 'false',
'pushover.token': '', 'pushover.token': '',
'pushover.user_key': '', 'pushover.user_key': '',
},
'WEB': {
'web.enabled': 'false',
'web.port': '9647'
} }
} }
deprecated_values = { deprecated_values = {

View file

@ -2,6 +2,8 @@ import threading
from random import randint from random import randint
from time import sleep from time import sleep
from pygtail import Pygtail
import log import log
from ConfigReader import ConfigReader, ConfigException from ConfigReader import ConfigReader, ConfigException
from SteamGifts import SteamGifts, SteamGiftsException from SteamGifts import SteamGifts, SteamGiftsException
@ -15,21 +17,32 @@ logger = log.get_logger(__name__)
class WebServerThread(threading.Thread): class WebServerThread(threading.Thread):
def run_webserver(self): def run_webserver(self):
import http.server from flask import Flask
import socketserver from flask import render_template
PORT = 8000 app = Flask(__name__)
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler): @app.route("/")
def do_GET(self): def config():
self.path = 'index.html' with open('../config/config.ini', 'r') as f:
return http.server.SimpleHTTPRequestHandler.do_GET(self) content = f.read()
return render_template('configuration.html', config=content)
Handler = MyHttpRequestHandler @app.route("/log")
def logs():
return render_template('log.html')
with socketserver.TCPServer(("", PORT), Handler) as httpd: @app.route("/stream")
print("Http Server Serving at port", PORT) def stream():
httpd.serve_forever() def generate():
with open('../config/debug.log') as f:
while True:
yield f.read()
sleep(10)
return app.response_class(generate(), mimetype='text/plain')
app.run(port=9647)
def run(self): def run(self):
# Variable that stores the exception, if raised by someFunction # Variable that stores the exception, if raised by someFunction
@ -138,6 +151,7 @@ def run():
w = WebServerThread() w = WebServerThread()
w.setName("WebServer") w.setName("WebServer")
# if the giveaway thread dies then this daemon thread will die by definition
w.setDaemon(True) w.setDaemon(True)
w.start() w.start()

76
src/static/css/main.css Normal file
View file

@ -0,0 +1,76 @@
body {
margin: 0;
padding: 0;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #444;
}
/*
* Formatting the header area
*/
header {
background-color: #DFB887;
height: 35px;
width: 100%;
opacity: .9;
margin-bottom: 10px;
}
header h1.logo {
margin: 0;
font-size: 1.7em;
color: #fff;
text-transform: uppercase;
float: left;
}
header h1.logo:hover {
color: #fff;
text-decoration: none;
}
/*
* Centering the body content
*/
.container {
width: 1200px;
margin: 0 auto;
}
div.home {
padding: 10px 0 30px 0;
background-color: #E6E6FA;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
}
div.about {
padding: 10px 0 30px 0;
background-color: #E6E6FA;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
}
h2 {
font-size: 3em;
margin-top: 40px;
text-align: center;
letter-spacing: -2px;
}
h3 {
font-size: 1.7em;
font-weight: 100;
margin-top: 30px;
text-align: center;
letter-spacing: -1px;
color: #999;
}
.menu {
float: right;
margin-top: 8px;
}
.menu li {
display: inline;
}
.menu li + li {
margin-left: 35px;
}
.menu li a {
color: #444;
text-decoration: none;
}

View file

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Flask app</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
</head>
<body>
<header>
<div class="container">
<h1 class="logo">Steamgifts Bot</h1>
<strong><nav>
<ul class="menu">
<li><a href="{{ url_for('config') }}">Config</a></li>
<li><a href="{{ url_for('logs') }}">Logs</a></li>
</ul>
</nav></strong>
</div>
</header>
<div class="container">
<pre>{{config}}</pre>
{% block content %}
{% endblock %}
</div>
</body>
</html>

35
src/templates/log.html Normal file
View file

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<title>Flask app</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
</head>
<body>
<header>
<div class="container">
<h1 class="logo">Steamgifts Bot</h1>
<strong><nav>
<ul class="menu">
<li><a href="{{ url_for('config') }}">Config</a></li>
<li><a href="{{ url_for('logs') }}">Logs</a></li>
</ul>
</nav></strong>
</div>
</header>
<div class="container">
<pre id="output"></pre>
<script>
var output = document.getElementById('output');
var xhr = new XMLHttpRequest();
xhr.open('GET', '{{ url_for('stream') }}');
xhr.send();
output.textContent = xhr.responseText;
setInterval(function() {
output.textContent = xhr.responseText;
}, 10000);
</script>
</div>
</body>
</html>