Fastest Python HTTP Server, written in C.
Project description
⚡️Thunderlight is the fastest Python HTTP Server
Thunderlight is the fastest Python HTTP Server. It's dedicated for micro service and JSON API. In the future, a C version of this API is also supported. You can write Python or C in your server project to speed things up.
Installation
Install Thunderlight with pip
.
pip install thunderlight
Documentation
get
Register a get request handler to the default app.
@get('/posts/:id')
async def posts(ctx: Ctx) -> None:
ctx.res.json((await Post.id(ctx.req.args.id)).tojson())
post
Register a post request handler to the default app.
@post('/posts')
async def posts(ctx: Ctx) -> None:
ctx.res.json(Post(**(await ctx.req.json())).save().tojson())
patch
Register a patch request handler to the default app.
@patch('/posts/:id')
async def posts(ctx: Ctx) -> None:
ctx.res.json((await Post.id(ctx.req.args.id)).set(await ctx.req.json()).save().tojson())
delete
Register a delete request handler to the default app.
@delete('/posts/:id')
async def posts(ctx: Ctx) -> None:
ctx.res.empty((await Post.id(ctx.req.args.id)).delete())
use
Append this middleware to the default app's middleware stack.
@use
async def measure_time(ctx: Ctx, next: Next) -> None:
start = time()
await next(ctx)
time_elapsed = time() - start
print(f'request {ctx.req.path} takes {time_elapsed} second.')
apply
Apply a middleware to a request handler. This is aka middleware for a single request handler.
async def validate_user(ctx: Ctx, next: Next) -> None:
if ctx.req.headers['Authorization'] is not None:
await next(ctx)
else:
ctx.res.code = 401
ctx.res.json({"error": {"type": "Unauthorized"}})
@get('/users')
@apply(validate_user)
async def users(ctx: Ctx) -> None:
ctx.res.json(await User.find())
App
Create a new server application.
app = App()
@app.get('/articles')
async def articles(ctx: Ctx) -> None:
ctx.res.json(await Article.find())
Changelog
0.2.4 (Jan 24th, 2022)
- Remove uvicorn and related dependencies
- Rewrite with C
0.1.0 (Dec 10th, 2021)
- Global methods added:
get
,post
,patch
,delete
,use
andapply
. - Classes added:
App
,Ctx
,Req
,Res
,State
. - Function definition added:
Handler
,Next
andMiddleware
. - Global helper methods added:
main
,gimme
andmake
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for thunderlight-0.2.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cbf020f1d4359b680a95514c2464249782ce97e124b92bd4e7a4dbd62923e2c |
|
MD5 | f4f5cb23395239dad41409d45ae0144a |
|
BLAKE2b-256 | 5e2218ecc074872ddfdb8ecc3dc2ec772efb99f94d53ed7c7c429c87437ca609 |