askrobots / hello-server
Public
The smallest thing worth installing on a server of your own: a test for one-click installs
danq9
hello-server 2026.11v1: the smallest thing worth installing on your own server
7298f7b
8 hours, 48 minutes ago
History
server.py · 3.1 KB · 88 lines
| 1 | """hello-server: the smallest thing worth installing on a server of your own. |
| 2 | |
| 3 | It answers two addresses: / is a page saying where it runs and which version is installed, and /health is |
| 4 | the same as JSON, for an installer to check that the install really worked. Python's standard library only, |
| 5 | so a fresh Ubuntu server needs nothing added. |
| 6 | """ |
| 7 | import html |
| 8 | import json |
| 9 | import os |
| 10 | import socket |
| 11 | import time |
| 12 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
| 13 | |
| 14 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 15 | STARTED = time.time() |
| 16 | PORT = int(os.environ.get('PORT', '80')) |
| 17 | |
| 18 | |
| 19 | def installed(): |
| 20 | """What install.sh recorded: the version, the commit and when.""" |
| 21 | try: |
| 22 | with open(os.path.join(HERE, 'installed.json')) as f: |
| 23 | return json.load(f) |
| 24 | except (OSError, ValueError): |
| 25 | return {} |
| 26 | |
| 27 | |
| 28 | def facts(): |
| 29 | info = installed() |
| 30 | return {'ok': True, 'app': 'hello-server', 'version': info.get('version'), 'commit': info.get('commit'), |
| 31 | 'installed_at': info.get('installed_at'), 'host': socket.gethostname(), |
| 32 | 'up_seconds': int(time.time() - STARTED)} |
| 33 | |
| 34 | |
| 35 | PAGE = """<!doctype html> |
| 36 | <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"> |
| 37 | <title>hello-server is running</title> |
| 38 | <style> |
| 39 | :root {{ color-scheme: light dark; }} |
| 40 | body {{ font: 17px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; max-width: 640px; |
| 41 | margin: 64px auto; padding: 0 16px; }} |
| 42 | h1 {{ font-size: 1.9rem; margin: 0 0 8px; }} |
| 43 | dl {{ display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; }} |
| 44 | dt {{ opacity: .65; }} |
| 45 | dd {{ margin: 0; font-family: ui-monospace, Menlo, monospace; overflow-wrap: anywhere; }} |
| 46 | </style></head> |
| 47 | <body> |
| 48 | <h1>It works.</h1> |
| 49 | <p>This server is yours, and hello-server is running on it.</p> |
| 50 | <dl> |
| 51 | <dt>Server</dt><dd>{host}</dd> |
| 52 | <dt>Version</dt><dd>{version}</dd> |
| 53 | <dt>Commit</dt><dd>{commit}</dd> |
| 54 | <dt>Installed</dt><dd>{installed_at}</dd> |
| 55 | <dt>Running for</dt><dd>{up_seconds} seconds</dd> |
| 56 | </dl> |
| 57 | <p>The same as JSON: <a href="/health">/health</a></p> |
| 58 | </body></html> |
| 59 | """ |
| 60 | |
| 61 | |
| 62 | class Handler(BaseHTTPRequestHandler): |
| 63 | server_version = 'hello-server' |
| 64 | |
| 65 | def send(self, status, body, content_type): |
| 66 | data = body.encode('utf-8') |
| 67 | self.send_response(status) |
| 68 | self.send_header('Content-Type', content_type) |
| 69 | self.send_header('Content-Length', str(len(data))) |
| 70 | self.send_header('Cache-Control', 'no-store') |
| 71 | self.end_headers() |
| 72 | self.wfile.write(data) |
| 73 | |
| 74 | def do_GET(self): |
| 75 | if self.path == '/health': |
| 76 | self.send(200, json.dumps(facts()) + '\n', 'application/json') |
| 77 | elif self.path == '/': |
| 78 | shown = {k: html.escape(str(v if v is not None else 'unknown')) for k, v in facts().items()} |
| 79 | self.send(200, PAGE.format(**shown), 'text/html; charset=utf-8') |
| 80 | else: |
| 81 | self.send(404, 'Not here.\n', 'text/plain; charset=utf-8') |
| 82 | |
| 83 | def log_message(self, *args): |
| 84 | pass # journald gets nothing per request: this is a test app |
| 85 | |
| 86 | |
| 87 | if __name__ == '__main__': |
| 88 | ThreadingHTTPServer(('0.0.0.0', PORT), Handler).serve_forever() |