Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/frmdstryr/zhp
A Http server written in Zig
https://github.com/frmdstryr/zhp
http zig
Last synced: 2 months ago
JSON representation
A Http server written in Zig
- Host: GitHub
- URL: https://github.com/frmdstryr/zhp
- Owner: frmdstryr
- License: mit
- Archived: true
- Created: 2019-12-03T21:11:48.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-06-02T15:23:08.000Z (over 2 years ago)
- Last Synced: 2024-09-26T17:27:58.391Z (3 months ago)
- Topics: http, zig
- Language: Zig
- Homepage:
- Size: 809 KB
- Stars: 355
- Watchers: 14
- Forks: 24
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-zig - frmdstryr/zhp
- awesome-zig - zhpšļøA Http server written in Zig
README
# ZHP
[![status](https://github.com/frmdstryr/zhp/actions/workflows/ci.yml/badge.svg)](https://github.com/frmdstryr/zhp/actions)
A (work in progress) Http server written in [Zig](https://ziglang.org/).
If you have suggestions on improving the design please feel free to comment!
### Features
- A zero-copy parser and aims to compete with these [parser_benchmarks](https://github.com/rust-bakery/parser_benchmarks/tree/master/http)
while still rejecting nonsense requests. It currently runs around ~1000MB/s.
- Regex url routing thanks to [ctregex](https://github.com/alexnask/ctregex.zig)
- Struct based handlers where the method maps to the function name
- A builtin static file handler, error page handler, and not found page handler
- Middleware support
- Parses forms encoded with `multipart/form-data`
- Streaming responses
- WebsocketsSee how it compares in the [http benchmarks](https://gist.github.com/kprotty/3f369f46293a421f09190b829cfb48f7#file-newresults-md)
done by kprotty (now very old).It's a work in progress... feel free to contribute!
### Demo
Try out the demo at [https://zhp.codelv.com](https://zhp.codelv.com).
> Note: If you try to benchmark the server it'll ban you, please run it locally
> or on your own server to do benchmarks.To make and deploy your own app see:
- [demo project](https://github.com/frmdstryr/zhp-demo)
- [zig buildpack](https://github.com/frmdstryr/zig-buildpack)### Example
See the `example` folder for a more detailed example.
```zig
const std = @import("std");
const web = @import("zhp");pub const io_mode = .evented;
pub const log_level = .info;const MainHandler = struct {
pub fn get(self: *MainHandler, request: *web.Request, response: *web.Response) !void {
_ = self;
_ = request;
try response.headers.put("Content-Type", "text/plain");
_ = try response.stream.write("Hello, World!");
}};
pub const routes = [_]web.Route{
web.Route.create("home", "/", MainHandler),
};pub const middleware = [_]web.Middleware{
web.Middleware.create(web.middleware.LoggingMiddleware),
};pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(!gpa.deinit());
const allocator = gpa.allocator();var app = web.Application.init(allocator, .{.debug=true});
defer app.deinit();try app.listen("127.0.0.1", 9000);
try app.start();
}```