1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
Time-stamp: <2026-05-04 13h50 UTC>
# Keeping track of thoughts.md as a webpage
I realise that this is not exactly in the spirit of the smol web, and i have yet to decide on way to make this accessible over gemini, but here's a quick solution i found to scratch an itch i had.
## The "problem"
I have a file called `thoughts.md` on my laptop, where i keep track of things that i ought to write down™ lest they become eroded by steady march of entropy, so that i have a central place where i can forget about things. But, and this is important, that file is on my laptop --- only.
What if it wasn't?
## Enter python, bottle, nginx, systemd
As i'm already hosting a website, what about a quick and dirty script that served a webpage with a textarea, and loaded the latest %YYYYMMDDHHMMSS.md file from a directory. If the user posts with "save", then make a new file.
There are many, many features that this could grow, but for now having a text area i can reach from anyway, behind some auth'd endpoint from anywhere is all that i need.
## Implementation details
This takes a tiny amount of glue, but here's the basic idea
```systemd unit file
[Unit]
Description=Note server
After=network.target
[Service]
Type=simple
DynamicUser=yes
StateDirectory=notes
WorkingDirectory=/var/lib/private/notes
ExecStart=/usr/bin/python3 /usr/local/bin/note.py /var/lib/private/notes --host 127.0.0.1 --port NNN-CHANGE-ME
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
```
```nginx config snippet
location /CHANGE-ME-XXX {
rewrite ^/CHANGE-ME-XXX/?(.*)$ /$1 break;
proxy_pass http://127.0.0.1:18088;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
auth_basic "notes";
auth_basic_user_file /etc/nginx/.htpasswd;
}
```
```python webserver
#!/usr/bin/env python3
import argparse
import re
from datetime import datetime
from pathlib import Path
import bottle
from bottle import request, run
def latest_file(directory: Path) -> Path | None:
files = [p for p in directory.glob("*.md") if re.match(r"^\d{14}\.md$", p.name)]
return max(files, key=lambda p: p.stat().st_mtime) if files else None
def save(directory: Path, text: str) -> Path:
filename = f"{datetime.now():%Y%m%d%H%M%S}.md"
path = directory / filename
_ = path.write_text(text, encoding="utf-8")
return path
def make_page(directory: Path) -> str:
latest = latest_file(directory)
content = latest.read_text(encoding="utf-8") if latest else ""
filename = latest.name if latest else "(none)"
return f"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>notes</title>
<style>
body {{ max-width: 800px; margin: 2em auto; padding: 0 1em; font-family: system-ui, sans-serif; }}
.meta {{ color: #666; font-size: 0.85em; margin-bottom: 1em; }}
textarea {{ width: 100%; height: 75vh; font-family: monospace; font-size: 14px; padding: 0.5em; }}
button {{ padding: 0.4em 1.2em; margin-top: 0.5em; }}
</style>
</head>
<body>
<div class="meta">latest: {filename}</div>
<form method="POST" action="save">
<textarea name="text" spellcheck="true">{bottle.html_escape(content)}</textarea>
<br><button>save</button>
</form>
</body>
</html>"""
def main():
parser = argparse.ArgumentParser(description="note server")
parser.add_argument("directory", type=Path, help="where to store .md files")
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--port", type=int, default=8080)
args = parser.parse_args()
args.directory.mkdir(parents=True, exist_ok=True)
@bottle.route("/")
def index():
return make_page(args.directory)
@bottle.route("/save", method="POST")
def save_route():
text = request.forms.get("text", "")
path = save(args.directory, text)
return make_page(args.directory)
run(host=args.host, port=args.port)
if __name__ == "__main__":
main()
```
|