Skip to content

fazliddinio/creatine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Creatine

A lightweight Python web framework with WSGI and ASGI support.

Installation

pip install creatine

For development:

pip install creatine[dev]

Quick Start

from creatine import Creatine

app = Creatine()


@app.route("/")
def home(req, resp):
    resp.text = "Hello, World!"


@app.route("/greeting/{name}")
def greeting(req, resp, name):
    resp.text = f"Hello, {name}"


@app.route("/book")
class BookResource:
    def get(self, req, resp):
        resp.text = "Books Page"

    def post(self, req, resp):
        resp.text = "Book created"


@app.route("/json")
def json_handler(req, resp):
    resp.json = {"message": "hello"}


@app.route("/template")
def template_handler(req, resp):
    resp.html = app.template("index.html", context={"title": "Hello"})

Running the Server

# Built-in dev server
creatine runserver app:app

# With Gunicorn (WSGI)
gunicorn app:app

# With Uvicorn (ASGI)
uvicorn app:asgi_app

ASGI & Async Handlers

Creatine supports both sync and async handlers when running under ASGI:

app = Creatine()

asgi_app = app.as_asgi()

@app.route("/async")
async def async_handler(req, resp):
    resp.json = {"async": True}

Middleware

from creatine import Middleware
from creatine.log import LoggingMiddleware

# Built-in request logging
app.add_middleware(LoggingMiddleware)

# Custom middleware
class AuthMiddleware(Middleware):
    def process_request(self, req):
        print(f"Auth check: {req.url}")

    def process_response(self, req, resp):
        print(f"Completed: {resp.status_code}")

app.add_middleware(AuthMiddleware)

ORM

Built-in SQLite ORM for simple data persistence:

from creatine import Database, Table, Column, ForeignKey

db = Database("./app.db")

class Author(Table):
    name = Column(str)
    age = Column(int)

class Book(Table):
    title = Column(str)
    author = ForeignKey(Author)

db.create(Author)
db.create(Book)

author = Author(name="Jane", age=30)
db.save(author)
db.update(author)
db.delete(Author, id=1)

Features

  • WSGI and ASGI compatible
  • Async handler support
  • Parameterized and basic routing
  • Class-based and function-based handlers
  • Built-in SQLite ORM with foreign keys
  • Jinja2 templates
  • Static files via WhiteNoise
  • Middleware pipeline with built-in logging
  • Custom exception handlers
  • Test client (based on Requests)
  • CLI (creatine runserver)
  • GitHub Actions CI with pytest and mypy

Tests

pytest

Project Structure

creatine/
├── __init__.py      # Package exports and version
├── __main__.py      # CLI entry point
├── api.py           # Core Creatine application class
├── asgi.py          # ASGI adapter
├── exceptions.py    # HTTPError
├── log.py           # Logging middleware
├── middleware.py     # Middleware base class
├── orm.py           # SQLite ORM
├── response.py      # Response wrapper
├── route.py         # Route matching and dispatch
└── utils.py         # Static file and test helpers

License

MIT

About

A lightweight Python web framework with WSGI and ASGI support.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors