Metadata-Version: 2.4
Name: minihttp
Version: 0.1.1
Summary: minihttp is a lightweight server written in pure python
Author-email: Mizuki Hikaru <mizuki@hikaru.org>
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown

# minihttp

minihttp is a small production ready HTTP server that implements subset of the
HTTP protocol. For example, it only supports GET and POST and always closes the
connection.

## Usage

    from dataclasses import dataclass

    from minihttp import Headers, Server, html, css, text, file, response

    @dataclass
    class UserQuery:
        sort_by: str

    @dataclass
    class Group:
        name: str
        active: bool = True

    server = Server()

    @server.get("/groups/:group_id")
    def users(group_id: int, query: UserQuery, headers: Headers):
        # GET /groups/7?sort_by=name turns the query string into
        # UserQuery(sort_by="name"). Header lookup is case-insensitive.
        request_id = headers.get("X-Request-ID")
        return [group_id, query.sort_by, request_id]

    @server.post("/groups/new")
    def new_group(group: Group):
        # A JSON body such as {"name":"admins","active":false} is
        # deserialized directly into Group("admins", False).
        return group

    @app.get("/")
    def index():
        return html("""
            <!doctype html>
            <html>
                <head><link rel="stylesheet" href="/style.css"></head>
                <body><h1>Hello from minihttp</h1></body>
            </html>
        """)

    @app.get("/style.css")
    def style():
        return css("body { font-family: sans-serif; }")

    @app.get("/robots.txt")
    def robots():
        return text("User-agent: *\nDisallow:")

    @app.get("/logo.png")
    def logo():
        return file("static/logo.png")

    @app.get("/data.bin")
    def data():
        return response(
            b"\x00\x01\x02",
            content_type="application/octet-stream",
        )

    server.run("0.0.0.0", 2000)
