~boringcactus/gemini-authoring-example

6c2191bd18fb068993e221e116a2b1aeab1158bd — Melody Horn 4 years ago main
Initial draft of proof-of-concept
2 files changed, 63 insertions(+), 0 deletions(-)

A cgi-bin/write.cgi
A index.gmi
A  => cgi-bin/write.cgi +62 -0
@@ 1,62 @@
#!/usr/bin/env python3

import base64
import os
from pathlib import Path
import sys
import urllib.parse

SESSIONS_DIR = Path('/tmp/gemini-authoring-sessions')
SESSIONS_DIR.mkdir(parents=True, exist_ok=True)

if 'TLS_CLIENT_HASH' not in os.environ:
    print('60 Need a client cert to start a session')
    sys.exit()

client_cert_hash = os.environ['TLS_CLIENT_HASH']
session_dir = SESSIONS_DIR / client_cert_hash

if not session_dir.exists():
    session_dir.mkdir()

data_file = session_dir / 'data'
meta_file = session_dir / 'meta'

if not data_file.exists():
    data_file.touch()

if '?' in os.environ['GEMINI_URL'] and len(os.environ['PATH_INFO']) > 0:
    query = urllib.parse.unquote(os.environ['QUERY_STRING'])
    line_num = os.environ['PATH_INFO'].lstrip('/')
    with data_file.open('r', encoding='utf-8') as data:
        lines = [line.rstrip('\n') for line in data.readlines()]
    print(lines, file=sys.stderr)
    if line_num == 'newline':
        lines.append(query)
    else:
        lines[int(line_num)] = query
    print(lines, file=sys.stderr)
    with data_file.open('w', encoding='utf-8') as data:
        data.writelines(line + '\n' for line in lines)
    print('30 /cgi-bin/write.cgi')
    sys.exit()
elif len(os.environ['PATH_INFO']) > 0:
    if os.environ['PATH_INFO'] == '/save':
        print('20 text/gemini')
        print('If this were a real application, your work would be saved now.')
        data_file.unlink()
        session_dir.rmdir()
        sys.exit()
    print('10 Enter the new line')
    sys.exit()

print('20 text/gemini')
with data_file.open('r', encoding='utf-8') as data:
    for i, line in enumerate(data):
        line = line.rstrip('\n')
        if len(line.strip()) == 0:
            line = "<empty line>"
        print(f'=> write.cgi/{i} {line}')
    print()
    print('=> write.cgi/newline New Line')
    print('=> write.cgi/save Save')

A  => index.gmi +1 -0
@@ 1,1 @@
=> cgi-bin/write.cgi