From b7c3e43ac4ebc8858d59d2815aa68ca30edca2af Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Mon, 18 May 2020 23:52:34 -0400 Subject: [PATCH] Update other examples --- examples/counter.py | 1 - examples/guestbook.py | 11 ++++++++--- examples/http_proxy.py | 35 ++++++++++++++++------------------- jetforce/__init__.py | 2 ++ 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/examples/counter.py b/examples/counter.py index ec5f7b6..fbec028 100644 --- a/examples/counter.py +++ b/examples/counter.py @@ -15,7 +15,6 @@ requests. Try requesting this endpoint over two connections simultaneously. > jetforce-client gemini://localhost > jetforce-client gemini://localhost """ - import time from jetforce import GeminiServer, JetforceApplication, Response, Status diff --git a/examples/guestbook.py b/examples/guestbook.py index 7e4d945..76b3516 100644 --- a/examples/guestbook.py +++ b/examples/guestbook.py @@ -2,10 +2,15 @@ A simple guestbook application that accepts and displays text messages. 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. -""" +retrieve their response by parsing the URL query string. +This example stores the guestbook inside of a persistent sqlite database. +Because each request will run inside of a separate thread, we must create a new +connection object inside of the request handler instead of re-using a global +database connection. This thread-safety can be disabled in sqlite3 by using the +check_same_thread=False argument, but then it's up to you to ensure that only +connection request is writing to the database at any given time. +""" import sqlite3 from datetime import datetime diff --git a/examples/http_proxy.py b/examples/http_proxy.py index 1708c2c..42a46a1 100644 --- a/examples/http_proxy.py +++ b/examples/http_proxy.py @@ -1,15 +1,22 @@ """ -This is an example of setting up a Gemini server to proxy requests to other -protocols. This application will accept HTTP URLs, download and render them -locally using the `w3m` tool, and render the output to the client as plain text. +A server that proxies HTTP websites over gemini. + +This example demonstrates how your application routes aren't just limited to +gemini URLs. The server will accept any HTTP URL, download the page and +render it using the external `w3m` tool, and then render the output to the +client as plain-text. + +Most gemini clients won't be able to make this request, because the hostname +in the URL doesn't match the hostname of the server. You can test this out +using jetforce-client like this: + +> jetforce-client https://mozz.us --host localhost """ -import asyncio import subprocess -import jetforce -from jetforce import Response, Status +from jetforce import GeminiServer, JetforceApplication, Response, Status -app = jetforce.JetforceApplication() +app = JetforceApplication() @app.route(scheme="https", strict_hostname=False) @@ -26,15 +33,5 @@ def proxy_request(request): if __name__ == "__main__": - args = jetforce.command_line_parser().parse_args() - ssl_context = jetforce.make_ssl_context( - 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()) + server = GeminiServer(app) + server.run() diff --git a/jetforce/__init__.py b/jetforce/__init__.py index 1d9df64..072636d 100644 --- a/jetforce/__init__.py +++ b/jetforce/__init__.py @@ -3,6 +3,8 @@ isort:skip_file """ from .__version__ import __version__ from .app.base import JetforceApplication, Request, Response, RoutePattern, Status +from .app.static import StaticDirectoryApplication +from .app.composite import CompositeApplication from .protocol import GeminiProtocol from .server import GeminiServer