forked from shahwali/celer
Super thin wrapper around Zig tcp connections + http parsing.
|
|
||
|---|---|---|
| src | init | |
| .gitignore | init | |
| build.zig | init | |
| build.zig.zon | init | |
| README.md | init | |
Celer
Super thin wrapper around Zig tcp connections + http parsing.
conststd=@import("std");constceler=@import("celer");constIo=std.Io;consthello_world=@embedFile("index.html");constServer=struct{io:Io,allocator:std.mem.Allocator,server:celer.Server,constrouter:celer.Router=.init(.{.{celer.Route{.method=.GET,.path="/"},Server.index,},.{celer.Route{.method=.GET,.path="/users/{id}"},Server.getUser,},});constSelf=@This();pubfninit(io:Io,allocator:std.mem.Allocator)!Self{returnSelf{.io=io,.allocator=allocator,.server=try.init(.{.requestHandler=handler,.port=8080,});,};}pubfndeinit(self:*constSelf)void{self.server.deinit();}pubfnstart(self:*Self)!void{std.log.info("Starting server at: http://localhost:{d}",.{config.port});tryself.server.start(self.io,self.allocator);}fnindex(server:*celer.Server,arena:std.mem.Allocator,req:*celer.Request)anyerror!void{constself:*Server=@fieldParentPtr("server",server);_=self;_=arena;tryreq.respond(celer.Response{.body=hello_world,.options=.{.extra_headers=&.{.{.name="Content-Type",.value="text/html"},},},});}constGetUserParams=struct{id:[]constu8,};fngetUser(ctx:*celer.Server,arena:std.mem.Allocator,req:*celer.Request,params:GetUserParams)!void{constself:*Server=@fieldParentPtr("server",ctx);_=self;tryreq.respond(celer.Response{.body=trystd.fmt.allocPrint(arena,"{s}",.{params.id}),.options=.{.extra_headers=&.{.{.name="Content-Type",.value="text/plain"},},},});}};pubfnmain(init:std.process.Init)!void{varserver=tryServer.init(init.io,init.gpa);deferserver.deinit();tryserver.start();}