yield deferLater(reactor, sleeptime, lambda: b'') col = 0 if b == b'\n': yield deferLater(reactor, sleeptime, lambda: linebuf) nextline_ts += line_interval col = 0 linebuf = b"" if col >= 80: # Wrap to 80 cols yield deferLater(reactor, sleeptime, lambda: linebuf + b'\r\n') nextline_ts += line_interval col = 0 linebuf = b"" gemini_app = JetforceApplication() http_app = HackyHttpApplication() def route(path, mimetype, http_path=None): """Route for both Gemini and HTTP""" def wrap(fn): @gemini_app.route(path) def gemini_route(req, *args, **kwargs): content = fn(*args, **kwargs) if content is None: return Response(Status.NOT_FOUND, "Not found") return Response(Status.SUCCESS, mimetype, content) @http_app.route(http_path or path) def http_route(req, *args, **kwargs):
listener.callback(message) except AlreadyCalledError: # The connection has disconnected, ignore it pass def subscribe(self): # Register a deferred response that will trigger whenever the next # message is published to the queue d = Deferred() self.listeners.append(d) return d queue = MessageQueue("/tmp/jetforce_chat.txt") app = JetforceApplication() HOMEPAGE = r""" # Gemini Chat A live, unmoderated chat room over gemini:// ``` It's better than grass! _________________________ < It's better than grass! > ------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
#!/usr/local/env python3 """ This example shows how you can implement rate limiting on a per-endpoint basis. """ from jetforce import GeminiServer, JetforceApplication, RateLimiter, Response, Status # Setup a global rate limiter that will be applied to all requests global_rate_limiter = RateLimiter("100/m") app = JetforceApplication(rate_limiter=global_rate_limiter) # Setup some custom rate limiting for specific endpoints short_rate_limiter = RateLimiter("5/30s") long_rate_limiter = RateLimiter("60/5m") INDEX_PAGE = """\ # Rate Limiting Demo =>/short short rate limiter (5/30s) =>/long long rate limiter (60/5m) """ @app.route("", strict_trailing_slash=False) def index(request): return Response(Status.SUCCESS, "text/gemini", INDEX_PAGE) @app.route("/short") @short_rate_limiter.apply def short(request): return Response(Status.SUCCESS, "text/gemini", "Request was successful")