From f60220f3029c98abe52270d13a8e070a7976b5a6 Mon Sep 17 00:00:00 2001 From: mcinj <98779161+mcinj@users.noreply.github.com> Date: Fri, 13 May 2022 16:55:52 -0400 Subject: [PATCH] web - show config and logs in webserver --- requirements.txt | 3 +- src/ConfigReader.py | 11 ++++- src/run.py | 36 ++++++++++----- src/static/css/main.css | 76 ++++++++++++++++++++++++++++++++ src/templates/configuration.html | 25 +++++++++++ src/templates/log.html | 35 +++++++++++++++ 6 files changed, 172 insertions(+), 14 deletions(-) create mode 100644 src/static/css/main.css create mode 100644 src/templates/configuration.html create mode 100644 src/templates/log.html diff --git a/requirements.txt b/requirements.txt index 2bb5028..30e751c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ beautifulsoup4==4.11.1 urllib3==1.26.9 sqlalchemy==1.4.36 sqlalchemy_utils==0.38.2 -python-dateutil==2.8.2 \ No newline at end of file +python-dateutil==2.8.2 +Flask==2.1.2 \ No newline at end of file diff --git a/src/ConfigReader.py b/src/ConfigReader.py index 2d4714c..66e49ad 100644 --- a/src/ConfigReader.py +++ b/src/ConfigReader.py @@ -25,8 +25,8 @@ def choose_user_agent(): ] return user_agents[randrange(0, len(user_agents))] -class ConfigReader(ConfigParser): +class ConfigReader(ConfigParser): required_values = { 'DEFAULT': { @@ -44,6 +44,10 @@ class ConfigReader(ConfigParser): }, 'NOTIFICATIONS': { 'pushover.enabled': ('true', 'false'), + }, + 'WEB': { + 'web.enabled': ('true', 'false'), + 'web.port': '%s' % (value_range(1, 65535)) } } default_values = { @@ -68,7 +72,10 @@ class ConfigReader(ConfigParser): 'pushover.enabled': 'false', 'pushover.token': '', 'pushover.user_key': '', - + }, + 'WEB': { + 'web.enabled': 'false', + 'web.port': '9647' } } deprecated_values = { diff --git a/src/run.py b/src/run.py index 5acf7de..8cfe941 100644 --- a/src/run.py +++ b/src/run.py @@ -2,6 +2,8 @@ import threading from random import randint from time import sleep +from pygtail import Pygtail + import log from ConfigReader import ConfigReader, ConfigException from SteamGifts import SteamGifts, SteamGiftsException @@ -15,21 +17,32 @@ logger = log.get_logger(__name__) class WebServerThread(threading.Thread): def run_webserver(self): - import http.server - import socketserver + from flask import Flask + from flask import render_template - PORT = 8000 + app = Flask(__name__) - class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler): - def do_GET(self): - self.path = 'index.html' - return http.server.SimpleHTTPRequestHandler.do_GET(self) + @app.route("/") + def config(): + with open('../config/config.ini', 'r') as f: + 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: - print("Http Server Serving at port", PORT) - httpd.serve_forever() + @app.route("/stream") + def stream(): + 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): # Variable that stores the exception, if raised by someFunction @@ -138,6 +151,7 @@ def run(): w = WebServerThread() w.setName("WebServer") + # if the giveaway thread dies then this daemon thread will die by definition w.setDaemon(True) w.start() diff --git a/src/static/css/main.css b/src/static/css/main.css new file mode 100644 index 0000000..9613db1 --- /dev/null +++ b/src/static/css/main.css @@ -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; +} \ No newline at end of file diff --git a/src/templates/configuration.html b/src/templates/configuration.html new file mode 100644 index 0000000..05b6f59 --- /dev/null +++ b/src/templates/configuration.html @@ -0,0 +1,25 @@ + + +
+{{config}}+ {% block content %} + {% endblock %} +