Giter Site home page Giter Site logo

routez's Introduction

Routez

HTTP server for Zig.

Example

Run with zig build basic

const std = @import("std");
const Address = std.net.Address;
usingnamespace @import("routez");
const allocator = std.heap.page_allocator;

pub const io_mode = .evented;

pub fn main() !void {
    var server = Server.init(
        allocator,
        .{},
        .{
            all("/", indexHandler),
            get("/about", aboutHandler),
            get("/about/more", aboutHandler2),
            get("/post/{post_num}/?", postHandler),
            static("./", "/static"),
            all("/counter", counterHandler),
        },
    );
    var addr = try Address.parseIp("127.0.0.1", 8080);
    try server.listen(addr);
}

fn indexHandler(req: Request, res: Response) !void {
    try res.sendFile("examples/index.html");
}

fn aboutHandler(req: Request, res: Response) !void {
    try res.write("Hello from about\n");
}

fn aboutHandler2(req: Request, res: Response) !void {
    try res.write("Hello from about2\n");
}

fn postHandler(req: Request, res: Response, args: *const struct {
    post_num: []const u8,
}) !void {
    try res.print("Hello from post, post_num is {}\n", args.post_num);
}

var counter = std.atomic.Int(usize).init(0);
fn counterHandler(req: Request, res: Response) !void {
    try res.print("Page loaded {} times\n", counter.fetchAdd(1));
}

routez's People

Contributors

alichraghi avatar elerch avatar frett27 avatar hnakamur avatar joachimschmidt557 avatar tonis2 avatar vexu avatar vrischmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

routez's Issues

Server doesn't parse query string

Hi!

In file routez/http/parser.zig there's this code (line 24):

    if (req.path.len > 1) {
        const uri = try Uri.parse(ctx.buf[cur .. ctx.index - 1], true);
        req.path = try Uri.resolvePath(req.headers.list.allocator, uri.path);
        req.query = uri.query;
    } else {
        req.path = try req.headers.list.allocator.dupe(u8, ctx.buf[cur .. ctx.index - 1]);
    }

I don't understand why you do this test, as it will always use the else branch (req.path is initialized to "" at this point). Else brach will copy both path and query string in req.path, leaving req.query = "".

I tested removing the if / else and it jost works as expected:

    const uri = try Uri.parse(ctx.buf[cur .. ctx.index - 1], true);
    req.path = try Uri.resolvePath(req.headers.list.allocator, uri.path);
    req.query = uri.query;

Maybe I'm missing some other aspect?

Thanks!

Quiting from server has delay

Hi,

When I create a debug server and try to fire some route, for example I run the basic.zig from examples and ping http://localhost:8080/counter.

Now I want to restart my server, for example I made some changes in the code,
I close the server with ctrl+c and rerun it, then I get error error: AddressInUse

The server address becomes available and is released after some minutes of waiting, so how could I close my server immediately ?

My OS is Ubuntu 20.10

`zig build basic` fails with error in build.zig

Hi,
will trying out your code :

zig build basic
./build.zig:17:9: error: no member named 'path' in struct 'std.build.Pkg'
        .path = .{ .path = "src/routez.zig" },
        ^

Since I am new to zig, I have no clue to help out debugging, but thought to report it here anyway.

When looking at this kind of error on other zig projects, it seems this is related to changes in build from 0.9 to zig version 0.10 with I am currently running.

zig version 
0.10.0-dev.3456+40babaa53

Best.

Segfault on `zig build`

output:

1/12 src.routez.test "routez"...OK
2/12 src.routez.http.test "http"...OK
3/12 src.routez.routes.test "index"...OK
4/12 src.routez.routes.test "args"...OK
5/12 src.routez.routes.test "delim string"...OK
6/12 src.routez.routes.test "subRoute"...OK
7/12 src.routez.routes.test "static files"...OK
8/12 src.routez.routes.test "optional char"...OK
9/12 src.routez.http.parser.test "parse headers"...OK
10/12 src.routez.http.parser.test "HTTP/0.9"...Segmentation fault at address 0x0
/git-owner/home/romigui/git/zig/build/lib/zig/std/event/loop.zig:633:13: 0x23516d in std.event.loop.Loop.beginOneEvent (test)
        _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
            ^
/git-owner/home/romigui/git/zig/build/lib/zig/std/event/loop.zig:427:27: 0x23506f in std.event.loop.Loop.linuxAddFd (test)
        self.beginOneEvent();
                          ^
/git-owner/home/romigui/git/zig/build/lib/zig/std/event/loop.zig:461:32: 0x234daf in std.event.loop.Loop.linuxWaitFd (test)
            try self.linuxAddFd(fd, &resume_node.base, flags);
                               ^
/git-owner/home/romigui/git/routez/src/routez/http/parser.zig:176:9: 0x208938 in src.routez.http.parser.test "HTTP/0.9" (test)
    try noasync parse(&req, &ctx);
        ^
/git-owner/home/romigui/git/zig/build/lib/zig/std/special/test_runner.zig:13:25: 0x240e41 in std.special.main (test)
        if (test_fn.func()) |_| {
                        ^
/git-owner/home/romigui/git/zig/build/lib/zig/std/special/start.zig:204:37: 0x23fcb5 in std.special.posixCallMainAndExit (test)
            const result = root.main() catch |err| {
                                    ^
/git-owner/home/romigui/git/zig/build/lib/zig/std/special/start.zig:102:5: 0x23fb2f in std.special._start (test)
    @noInlineCall(posixCallMainAndExit);
    ^

Tests failed. Use the following command to reproduce the failure:
/git-owner/home/romigui/git/routez/zig-cache/o/CfXmeKnBdPtAuIGxAcp2wOuVYFyvmR9sve2_yKRFv64mAmNSJTfnhKdyBmYkTQLu/test
The following command exited with error code 255:
/git-owner/home/romigui/git/zig/build/zig test /git-owner/home/romigui/git/routez/test.zig --cache-dir /git-owner/home/romigui/git/routez/zig-cache --name test --pkg-begin zuri /git-owner/home/romigui/git/routez/zuri/src/zuri.zig --pkg-end

Build failed. The following command failed:
/git-owner/home/romigui/git/routez/zig-cache/o/LCQKgXoXPU8miEvYpSoreZBaumqj6t-mMGyq68r3QsWcBPcg6yKYIbKUibbOu8n5/build /git-owner/home/romi

Routez on Techempower

Hi there

I am learning Zig and was wondering if i can pick up a small project like adding Routez to Techempower benchmarks.

I fully understand that Zig and Routez are very nascent right now, and performance is likely not a major area of focus. - but I think it will be interesting to know how well it currently does against established web servers.

Would it be ok with you if I were to add Routez to the techempower benchmarks?

Changing status_code in handler

Hei,

What would be the preferred way to change the status_code inside the route handler,
Currently, this line https://github.com/Vexu/routez/blob/master/src/routez/router.zig#L59 ,
always overwrites my custom set status.

For example in my route I would like to use res.status_code = .BadRequest;
It will still be sent as .Ok

Just commenting the above line out and setting it to .Ok here https://github.com/Vexu/routez/blob/master/src/routez/server.zig#L181, fixes the issue, am I breaking something with this ?

error: function type cannot have a name

When i run my code, it gives me this error. How i can fix it?

error: function type cannot have a name
pub const HandlerFn = fn handle(Request, Response, []const u8) callconv(.Async) anyerror!void;

routez.vexu.eu

routez.vexu.eu seems to timeout whenever I visit it,
is it normal?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.