Update guestbook example
This commit is contained in:
parent
b2727748ef
commit
78e94f3091
|
@ -1,60 +1,63 @@
|
||||||
"""
|
"""
|
||||||
A guestbook application that accepts input from guests and stores messages in
|
A simple guestbook application that accepts and displays text messages.
|
||||||
a simple text file.
|
|
||||||
|
This is an example of how to return a 10 INPUT request to the client and
|
||||||
|
retrieve their response by parsing the URL query string. This example stores
|
||||||
|
the guestbook inside of a persistent sqlite database.
|
||||||
"""
|
"""
|
||||||
import asyncio
|
|
||||||
import pathlib
|
import sqlite3
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import jetforce
|
from jetforce import GeminiServer, JetforceApplication, Response, Status
|
||||||
from jetforce import Response, Status
|
|
||||||
|
|
||||||
guestbook = pathlib.Path("guestbook.txt")
|
DB = "/tmp/guestbook.sqlite"
|
||||||
|
|
||||||
|
SCHEMA = """
|
||||||
|
CREATE TABLE IF NOT EXISTS guestbook (
|
||||||
|
ip_address TEXT,
|
||||||
|
created_at timestamp,
|
||||||
|
message TEXT
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
with sqlite3.connect(DB) as c:
|
||||||
|
c.execute(SCHEMA)
|
||||||
|
|
||||||
|
|
||||||
app = jetforce.JetforceApplication()
|
app = JetforceApplication()
|
||||||
|
|
||||||
|
|
||||||
@app.route("", strict_trailing_slash=False)
|
@app.route("", strict_trailing_slash=False)
|
||||||
def index(request):
|
def index(request):
|
||||||
data = ["Guestbook", "=>/submit Sign the Guestbook", ""]
|
lines = ["Guestbook", "=>/submit Sign the Guestbook"]
|
||||||
|
|
||||||
guestbook.touch(exist_ok=True)
|
with sqlite3.connect(DB, detect_types=sqlite3.PARSE_DECLTYPES) as c:
|
||||||
with guestbook.open("r") as fp:
|
for row in c.execute("SELECT * FROM guestbook ORDER BY created_at"):
|
||||||
for line in fp:
|
ip_address, created_at, message = row
|
||||||
line = line.strip()
|
line = f"{created_at:%Y-%m-%d} - [{ip_address}] {message}"
|
||||||
if line.startswith("=>"):
|
lines.append("")
|
||||||
data.append(line[2:])
|
lines.append(line)
|
||||||
else:
|
|
||||||
data.append(line)
|
|
||||||
|
|
||||||
data.extend(["", "...", ""])
|
lines.extend(["", "...", ""])
|
||||||
return Response(Status.SUCCESS, "text/gemini", "\n".join(data))
|
body = "\n".join(lines)
|
||||||
|
|
||||||
|
return Response(Status.SUCCESS, "text/gemini", body)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/submit")
|
@app.route("/submit")
|
||||||
def submit(request):
|
def submit(request):
|
||||||
if request.query:
|
if request.query:
|
||||||
message = request.query[:256]
|
message = request.query[:256]
|
||||||
created = datetime.utcnow()
|
created = datetime.now()
|
||||||
with guestbook.open("a") as fp:
|
ip_address = request.environ["REMOTE_HOST"]
|
||||||
fp.write(f"\n[{created:%Y-%m-%d %I:%M %p}]\n{message}\n")
|
with sqlite3.connect(DB) as c:
|
||||||
|
values = (ip_address, created, message)
|
||||||
|
c.execute("INSERT INTO guestbook VALUES (?, ?, ?)", values)
|
||||||
return Response(Status.REDIRECT_TEMPORARY, "")
|
return Response(Status.REDIRECT_TEMPORARY, "")
|
||||||
else:
|
else:
|
||||||
return Response(Status.INPUT, "Enter your message (max 256 characters)")
|
return Response(Status.INPUT, "Enter your message (max 256 characters)")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
args = jetforce.command_line_parser().parse_args()
|
server = GeminiServer(app)
|
||||||
ssl_context = jetforce.make_ssl_context(
|
server.run()
|
||||||
args.hostname, args.certfile, args.keyfile, args.cafile, args.capath
|
|
||||||
)
|
|
||||||
server = jetforce.GeminiServer(
|
|
||||||
host=args.host,
|
|
||||||
port=args.port,
|
|
||||||
ssl_context=ssl_context,
|
|
||||||
hostname=args.hostname,
|
|
||||||
app=app,
|
|
||||||
)
|
|
||||||
asyncio.run(server.run())
|
|
||||||
|
|
Loading…
Reference in New Issue