Package: thunderlight
⚡️Thunderlight is the fastest Python HTTP ServerThunderlight 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.InstallationInstall Thunderlight withpip.pipinstallthunderlightDocumentationgetRegister a get request handler to the default app.@get('/posts/:id')asyncdefposts(ctx:Ctx)->None:ctx.res.json((awaitPost.id(ctx.req.args.id)).tojson())postRegister a post request handler to the default app.@post('/posts')asyncdefposts(ctx:Ctx)->None:ctx.res.json(Post(**(awaitctx.req.json())).save().tojson())patchRegister a patch request handler to the default app.@patch('/posts/:id')asyncdefposts(ctx:Ctx)->None:ctx.res.json((awaitPost.id(ctx.req.args.id)).set(awaitctx.req.json()).save().tojson())deleteRegister a delete request handler to the default app.@delete('/posts/:id')asyncdefposts(ctx:Ctx)->None:ctx.res.empty((awaitPost.id(ctx.req.args.id)).delete())useAppend this middleware to the default app's middleware stack.@useasyncdefmeasure_time(ctx:Ctx,next:Next)->None:start=time()awaitnext(ctx)time_elapsed=time()-startprint(f'request{ctx.req.path}takes{time_elapsed}second.')applyApply a middleware to a request handler. This is aka middleware for a single
request handler.asyncdefvalidate_user(ctx:Ctx,next:Next)->None:ifctx.req.headers['Authorization']isnotNone:awaitnext(ctx)else:ctx.res.code=401ctx.res.json({"error":{"type":"Unauthorized"}})@get('/users')@apply(validate_user)asyncdefusers(ctx:Ctx)->None:ctx.res.json(awaitUser.find())AppCreate a new server application.app=App()@app.get('/articles')asyncdefarticles(ctx:Ctx)->None:ctx.res.json(awaitArticle.find())CtxThe context represents a request session.ctx.reqThe request object. This object is readonly.ctx.resThe response object. Modify this object to present data to the user.ctx.stateThe custom state object that is internal to your custom handling code.ReqThis class represents the incoming request. This object is readonly.req.clientThe request client.req.schemeThe request HTTP scheme.req.versionThe request HTTP version.req.methodThe request HTTP method.req.pathThe path of the request URL.req.argsThe args matched from url routing.req.qsThe query string of the request URL.req.headersThe request headers.await req.body()The raw request body.await req.json()The request's json body.await req.form()The request's form body.await req.dict()The request's json body or form body.ResThe class represents the response returned to the frontend.res.codeThe status code.res.headersThe response headers.res.bodyThe response body.res.json()Response json to the frontend.res.text()Response text to the frontendres.html()Response html text to the frontendres.redirect()Response a redirect request to the frontendres.file()Response a file to the frontendres.empty()Empty responseStateYou can attach anything to the state and fetch by key. This state shares the
same state across middlewares and the route handler.@app.get('/articles')asyncdefarticles(ctx:Ctx)->None:ctx.state.user=custom_userctx.state.user# custom_userChangelog0.6.0 (Mar 25th, 2022)Added generic response headersAdded wildcard URL route matchingAdded static file serving0.5.1 (Mar 8th, 2022)Temporarily add static file0.5.0 (Feb 24th, 2022)Revert to Python implementation as the C implementation is not stable0.2.8 (Jan 28th, 2022)Fixed a bug that causes patch and delete requests to crash0.2.7 (Jan 25th, 2022)Fixed a bug that causes errors in handlers not propagatedAdd__contains__method toReqHeaders0.2.6 (Jan 25th, 2022)Fixed a bug that would cause delete routes crashing0.2.5 (Jan 24th, 2022)Remove uvicorn and related dependenciesRewrite with CFix req.json property0.1.0 (Dec 10th, 2021)Global methods added:get,post,patch,delete,useandapply.Classes added:App,Ctx,Req,Res,State.Function definition added:Handler,NextandMiddleware.Global helper methods added:main,gimmeandmake.