From 07beafa9de8f7740bb6db9244c251a2e1d574e84 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 4 Mar 2021 02:50:38 +0300 Subject: update README, add some comments, etc. --- app.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'app.py') diff --git a/app.py b/app.py index 020d55a..96ffc1d 100755 --- a/app.py +++ b/app.py @@ -5,6 +5,40 @@ # For details, see https://github.com/egor-tensin/linux-status. # Distributed under the MIT License. +# Initially it was just index.html and a bunch of shell scripts in cgi-bin. +# The web server could be launched using just `python -m http.server --cgi`, +# and all was well. Until I decided that I want to package this little app +# of mine so that it can be easily installed and run on a variety of systems. +# There was a problem with that, however: the app was designed to be run by +# a regular user, not root (mostly due to all the `systemctl --user` calls), +# and it didn't play nicely with the whole package idea. Plus, I've long +# wanted to make it show all the systemd user instances, not just the running +# user's. In addition, I wanted to dedupe the small chunks of code in cgi-bin. +# +# So this script was born. It could handle a lot more complexity than the +# shell scripts; it could be run as root and as a regular user, etc. It was +# still a CGI script (and it still is BTW), and a separate Python instance was +# used by the web server to execute it. +# +# There was a problem, of course. The thing is, I for once had a very strict +# performance requirement for this script: it had to work nicely on my rather +# old Raspberry Pi 1 Model B+. It, being a Python script, didn't. After +# some investigation, it was obvious that the main problem were the imports +# (especially those of the cgi & logging modules); they would take more time +# than the interval between requests. So I had to find a way to make it more +# performant. +# +# I played with Lua for a bit, but couldn't quite make it work in a +# satisfactory way. Then I had an idea: I could use http.server's HTTP server +# classes, overriding them to handle the "CGI" requests using the code in this +# script. That way, the imports would only be done once for the entire life +# of the server. In addition, no more tricks with sudo were required +# (http.server runs CGI scripts as user nobody, which is a no-no for this app; +# some trickery like whitelisting nobody in /etc/sudoers.d were employed). +# +# So that's where we are now, and it works! Doesn't load my good ol' Raspberry +# no more. + import abc import cgi from collections import namedtuple -- cgit v1.2.3