ptgbot-web: add simple service to serve static pages

This serves the static pages from the container directly

Change-Id: I86f3f5002f88005760e00c5714b0891c1892a6f2
This commit is contained in:
Ian Wienand 2021-10-05 12:50:41 +11:00
parent 82c0b9ab9d
commit 0e2f3d0a9d
23 changed files with 89 additions and 3 deletions

View File

@ -9,4 +9,5 @@ FROM opendevorg/python-base:3.9 as ptgbot
COPY --from=builder /output/ /output
RUN /output/install-from-bindep
CMD ["/usr/local/bin/ptgbot", "-d", "/etc/ptgbot/ptgbot.config"]
COPY init.sh init.sh
CMD ./init.sh

7
init.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
# Serves website as a daemon
/usr/local/bin/ptgbot-web --debug /etc/ptgbot/ptgbot.config
# Run in foreground as the main process
/usr/local/bin/ptgbot -d /etc/ptgbot/ptgbot.config

View File

Before

Width:  |  Height:  |  Size: 106 KiB

After

Width:  |  Height:  |  Size: 106 KiB

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

73
ptgbot/web.py Normal file
View File

@ -0,0 +1,73 @@
import argparse
import daemon
import daemon.pidfile
import http.server
import json
import logging
import os
import pkg_resources
import socketserver
CONFIG = {}
class RequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path.endswith('ptg.json'):
with open(CONFIG['db_filename'], 'rb') as fp:
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(fp.read())
else:
http.server.SimpleHTTPRequestHandler.do_GET(self)
def log_message(self, format, *args):
logging.debug("%s - - [%s] %s" % (self.address_string(),
self.log_date_time_string(),
format % args))
def start():
os.chdir(CONFIG['source_dir'])
with socketserver.TCPServer(("", CONFIG['port']), RequestHandler) as httpd:
httpd.serve_forever()
def main():
global CONFIG
parser = argparse.ArgumentParser(description='PTG web')
parser.add_argument('configfile', help='config file')
parser.add_argument('-d', dest='nodaemon', action='store_true',
help='do not run as daemon')
parser.add_argument('-p', '--port', dest='port', help='Port to listen on',
default=8000)
parser.add_argument('--debug', dest='debug', action='store_true')
args = parser.parse_args()
CONFIG['debug'] = True if args.debug else False
CONFIG['port'] = int(args.port)
with open(args.configfile, 'r') as fp:
file_config = json.load(fp)
CONFIG['db_filename'] = file_config['db_filename']
CONFIG['source_dir'] = pkg_resources.resource_filename(__name__, "html")
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)
logging.info('Starting daemon on port: %s' % args.port)
logging.info('Serving files from: %s' % CONFIG['source_dir'])
logging.info('JSON from: %s' % CONFIG['db_filename'])
logging.debug('Debugging on')
if not args.nodaemon:
os.makedirs('/var/run/ptgbot', exist_ok=True)
pid = daemon.pidfile.TimeoutPIDLockFile(
'/var/run/ptgbot/ptgbot-web.pid', 10)
with daemon.DaemonContext(pidfile=pid):
start()
start()
if __name__ == "__main__":
main()

View File

@ -1,5 +1,5 @@
# Pin irc module to 15.1.1 until ib3 releases https://github.com/bd808/python-ib3/commit/92da70155e5378478802cb38baaa2a00187201d6
irc==15.1.1
python-daemon
python-daemon >= 1.6
ib3
requests

View File

@ -19,6 +19,11 @@ classifier =
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
[files]
packages =
ptgbot
[entry_points]
console_scripts =
ptgbot = ptgbot.bot:main
ptgbot-web = ptgbot.web:main

View File

@ -1,2 +1,2 @@
hacking>=3.0,<3.1.0 # Apache-2.0
hacking>=3.0,<4.1.0 # Apache-2.0
stestr>=2.0.0 # Apache-2.0