Skip to main content

Zero-dependency web router.

Project description

muxy

A router/http request multiplexer for composing servers conforming to Granian's Rust Server Gateway Interface (RSGI)

Inspiration

Go's stdlib has a minimalist but fully-functional router that allows simple composition of endpoint handlers. The routing uses a radix-tree which is far more efficient than Starlette's linear search of regex patterns. Whether this practically makes a difference, I don't really care, I just think it's a nice to do things properly.
package main

import (
	"fmt"
	"log"
	"net/http"
)

func helloWorld(w http.ResponseWriter, req *http.Request) { // implements `HandlerFunc` interface
	fmt.Print(w, "Hello world")
}

func main() {
    // i want a way to compose RSGI apps like this
	childMux := http.NewServeMux()                // implements `http.Handler` interface
	childMux.HandleFunc("GET /hello", helloWorld) // mount a `HandlerFunc`

	rootMux := http.NewServeMux()
	rootMux.Handle("/child/", http.StripPrefix("/child", childMux)) // nest an `http.Handler`

    // could then pass the root RSGI app to granian, which provides the `ListenAndServe`
	log.Fatal(http.ListenAndServe("localhost:8080", rootMux))
}

Planning

RSGI is our equivalent interface to `http.Handler`
from asyncio import AbstractEventLoop
from typing import Protocol
from granian.rsgi import HTTPProtocol, Scope, WebsocketProtocol
class RSGI(Protocol):
    async def __rsgi__(
        self, __scope: Scope, __proto: HTTPProtocol | WebsocketProtocol
    ): ...
    def __rsgi_init__(self, __loop: AbstractEventLoop): ...
    def __rsgi_del__(self, __loop: AbstractEventLoop): ...

The request metadata & headers are in Scope and it's body is accessible in HTTPProtocol. The response is writable using the HTTPProtocol.

Scope:

  • scheme
  • method
  • path
  • query_string
  • headers
  • server

Request body:

  • proto()
  • async for chunk in proto()

Response:

  • proto.response_empty(status, headers)
  • proto.response_{str,bytes}(status, headers, body)
  • proto.response_{file}(status, headers, path)
  • proto.response_{stream}(status, headers).send_{str,bytes}(data)

We need to implement a way to create and compose instances of applications implementing the RSGI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

muxy-0.1.0.dev1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

muxy-0.1.0.dev1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file muxy-0.1.0.dev1.tar.gz.

File metadata

  • Download URL: muxy-0.1.0.dev1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for muxy-0.1.0.dev1.tar.gz
Algorithm Hash digest
SHA256 09b26663c0be48777c054d70301c36cc4410f0419d2e3e97fef3623e7db2dec1
MD5 56d3f48499b0f4a24202282b71321b08
BLAKE2b-256 c1628ad8fd9a6227925a4da4d52621bd55fb1a992f46d43e6c537b988d626a38

See more details on using hashes here.

Provenance

The following attestation bundles were made for muxy-0.1.0.dev1.tar.gz:

Publisher: release.yaml on oliverlambson/muxy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file muxy-0.1.0.dev1-py3-none-any.whl.

File metadata

  • Download URL: muxy-0.1.0.dev1-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for muxy-0.1.0.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 4cda7df8fd0a969a795f84ebbfed6c4d02cee30bcaee315bceb5a5c3c12740b8
MD5 553b41e7c0621e527ef2727af18ac80f
BLAKE2b-256 059f51c6b00aa2501dc7c756cb4215036f2112a5ff576dd9ca6e15e7916079a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for muxy-0.1.0.dev1-py3-none-any.whl:

Publisher: release.yaml on oliverlambson/muxy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page