1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 23:24:09 +00:00

added setStatus, updated README

This commit is contained in:
Rene Schallner 2023-01-16 00:12:41 +01:00
parent 0f135a2ff4
commit 4aeb8b0f1e
4 changed files with 34 additions and 35 deletions

View file

@ -3,11 +3,12 @@
Zap is intended to become the [zig](https://ziglang.org) replacement for the Zap is intended to become the [zig](https://ziglang.org) replacement for the
kind of REST APIs I used to write in [python](https://python.org) with kind of REST APIs I used to write in [python](https://python.org) with
[Flask](https://flask.palletsprojects.com) and [Flask](https://flask.palletsprojects.com) and
[mongodb](https://www.mongodb.com), etc. [mongodb](https://www.mongodb.com), etc. It can be considered to be a
microframework for web applications.
What I need for that is a blazingly fast, robust HTTP server that I can use with What I need for that is a blazingly fast, robust HTTP server that I can use with
zig. While facil.io supports TLS, I don't mind HTTPS. In production, I use zig. While facil.io supports TLS, I don't care about HTTPS support. In
[nginx](https://www.nginx.com) as a reverse proxy anyway. production, I use [nginx](https://www.nginx.com) as a reverse proxy anyway.
Zap wraps and patches [facil.io - the C web application Zap wraps and patches [facil.io - the C web application
framework](https://facil.io). framework](https://facil.io).
@ -230,7 +231,7 @@ const std = @import("std");
const zap = @import("zap"); const zap = @import("zap");
fn on_request(r: zap.SimpleRequest) void { fn on_request(r: zap.SimpleRequest) void {
// TODO: send 404 response r.setStatus(404);
_ = r.sendBody("<html><body><h1>404 - File not found</h1></body></html>"); _ = r.sendBody("<html><body><h1>404 - File not found</h1></body></html>");
} }

View file

@ -2,7 +2,7 @@ const std = @import("std");
const zap = @import("zap"); const zap = @import("zap");
fn on_request(r: zap.SimpleRequest) void { fn on_request(r: zap.SimpleRequest) void {
// TODO: send 404 response r.setStatus(404);
_ = r.sendBody("<html><body><h1>404 - File not found</h1></body></html>"); _ = r.sendBody("<html><body><h1>404 - File not found</h1></body></html>");
} }

View file

@ -3,11 +3,12 @@
Zap is intended to become my [zig](https://ziglang.org) replacement for the Zap is intended to become my [zig](https://ziglang.org) replacement for the
kind of REST APIs I used to write in [python](https://python.org) with kind of REST APIs I used to write in [python](https://python.org) with
[Flask](https://flask.palletsprojects.com) and [Flask](https://flask.palletsprojects.com) and
[mongodb](https://www.mongodb.com), etc. [mongodb](https://www.mongodb.com), etc. It can be considered to be a
microframework for web applications.
What I need for that is a blazingly fast, robust HTTP server that I can use with What I need for that is a blazingly fast, robust HTTP server that I can use with
zig. While facil.io supports TLS, I don't care about HTTPS. In production, I use zig. While facil.io supports TLS, I don't care about HTTPS support. In
[nginx](https://www.nginx.com) as a reverse proxy anyway. production, I use [nginx](https://www.nginx.com) as a reverse proxy anyway.
Zap wraps and patches [facil.io - the C web application Zap wraps and patches [facil.io - the C web application
framework](https://facil.io). framework](https://facil.io).
@ -18,12 +19,6 @@ At the time of writing, ZAP is only a few days old and aims to be:
- **fast** - **fast**
- **minimal** - **minimal**
**Side-note:**
It never ceases to amaze me how productive I can be in zig, eventhough I am
still considering myself to be a newbie. Sometimes, it's almost like writing
python but with all the nice speed and guarantees that zig gives you. Also, the
C integration abilities of zig are just phenomenal! I am super excited about
zig's future!
**⚡ZAP⚡ IS SUPER ALPHA** **⚡ZAP⚡ IS SUPER ALPHA**
@ -66,6 +61,13 @@ to a point where I can use it as the JSON REST API backend for real research
projects, serving thousands of concurrent clients. Now that the endpoint example projects, serving thousands of concurrent clients. Now that the endpoint example
works, ZAP has actually become pretty usable to me. works, ZAP has actually become pretty usable to me.
**Side-note:**
It never ceases to amaze me how productive I can be in zig, eventhough I am
still considering myself to be a newbie. Sometimes, it's almost like writing
python but with all the nice speed and guarantees that zig gives you. Also, the
C integration abilities of zig are just phenomenal! I am super excited about
zig's future!
Now, on to the guiding principles of Zap. Now, on to the guiding principles of Zap.
## robust ## robust
@ -76,7 +78,7 @@ notable and cool HTTP server implementations written in zig out there, at the
time of writing, most of them seem to a) depend on zig's async facilities which time of writing, most of them seem to a) depend on zig's async facilities which
are unsupported until ca. April 2023 when async will return to the self-hosted are unsupported until ca. April 2023 when async will return to the self-hosted
compiler, and b) have not matured to a point where **I** feel safe using them in compiler, and b) have not matured to a point where **I** feel safe using them in
production. These are all my opionions and they could be totally wrong. production. These are just my opionions and they could be totally wrong though.
However, when I conduct my next online research experiment with thousands of However, when I conduct my next online research experiment with thousands of
concurrent clients, I cannot afford to run into potential maturity-problems of concurrent clients, I cannot afford to run into potential maturity-problems of
@ -147,30 +149,31 @@ support TLS, I don't care about that - at least for now. Also, if you present
also set the status code to 404, so it can be OK to spare those nanoseconds. also set the status code to 404, so it can be OK to spare those nanoseconds.
Gotta go fast! Gotta go fast!
Facilio comes with Mustache parsing, TLS, websockets, redis support, concurrency Facilio comes with Mustache parsing, TLS via third-party libs, websockets, redis
stuff, Base64 support, logging facilities, pub/sub / cluster messages API, hash support, concurrency stuff, Base64 support, logging facilities, pub/sub /
algorithm implementations, its own memory allocator, and so forth. It is really cluster messages API, hash algorithm implementations, its own memory allocator,
an amazing project! and so forth. It is really an amazing project!
On the lower level, you can use all of the above by working with `zap.C`. I'll On the lower level, you can use all of the above by working with `zap.C`. I'll
zig-wrap what I need for my projects first, before adding fancy stuff. zig-wrap what I need for my projects first, before adding more fancy stuff.
Also, there are nice and well-typed zig implementations for some of the above Also, there are nice and well-typed zig implementations for some of the above
extra functionalities, and zap-wrapping them needs careful consideration. Might extra functionalities, and zap-wrapping them needs careful consideration. E.g.
not be worth the extra effort. Performance / out-of-the-box integration might be it might not be worth the extra effort to wrap facil.io's mustache support when
arguments pro wrapping them in zap. there is a good zig alternative already. Performance / out-of-the-box
integration might be arguments pro wrapping them in zap.
## wrapping up - zig is WYSIWYG code ## wrapping up - zig is WYSIWYG code
I am super excited about both zig and zap's future. I am still impressed by how I am super excited about both zig and zap's future. I am still impressed by how
easy it is to integrate a C codebase into a zig project, then benefiting from easy it is to integrate a C codebase into a zig project, then benefiting from
and building on top of a battle-tested high-performance C code. Additionally, and building on top of a battle-tested high-performance C code. Additionally,
with zig you get C-like performance with almost Python-like comfort. And be sure with zig you get C-like performance with almost Python-like comfort. And you can
no exception is trying to get you when you least expect it. No hidden be sure no exception is trying to get you when you least expect it. No hidden
allocations, no hidden control-flows, how cool is that? **WYSIWYG code!** allocations, no hidden control-flows, how cool is that? **WYSIWYG code!**
Provided that the incorporated C code is well-written and -tested, WYSIWYG even Provided that the incorporated C code is well-written and -tested, WYSIWYG even
holds true for combined Zig and C projects. holds mostly true for combined Zig and C projects.
You can truly build on the soulders of giants here. Mind you, it took me less You can truly build on the soulders of giants here. Mind you, it took me less
than a week to arrive at the current state of zap where I am confident that I than a week to arrive at the current state of zap where I am confident that I

View file

@ -33,7 +33,6 @@ pub fn start(args: C.fio_start_args) void {
} }
const ListenError = error{ const ListenError = error{
ValueNotZeroTerminated,
AlreadyListening, AlreadyListening,
ListenError, ListenError,
}; };
@ -99,6 +98,10 @@ pub const SimpleRequest = struct {
// C.fiobj_free(new_fiobj_str); // C.fiobj_free(new_fiobj_str);
} }
pub fn setStatus(self: *const Self, status: usize) void {
self.h.*.status = status;
}
pub fn nextParam(self: *const Self) ?HttpParam { pub fn nextParam(self: *const Self) ?HttpParam {
if (self.h.*.params == 0) return null; if (self.h.*.params == 0) return null;
var key: C.FIOBJ = undefined; var key: C.FIOBJ = undefined;
@ -158,10 +161,6 @@ pub const SimpleHttpListener = struct {
if (self.settings.public_folder) |pf| { if (self.settings.public_folder) |pf| {
pfolder_len = pf.len; pfolder_len = pf.len;
// TODO: make sure it's 0-terminated!!!
// if (pf[pf.len - 1] != 0) {
// return error.ValueNotZeroTerminated;
// }
pfolder = pf.ptr; pfolder = pf.ptr;
} }
@ -240,10 +239,6 @@ pub fn listen(port: [*c]const u8, interface: [*c]const u8, settings: ListenSetti
if (settings.public_folder) |pf| { if (settings.public_folder) |pf| {
pfolder_len = pf.len; pfolder_len = pf.len;
// TODO: make sure it's 0-terminated!!!
// if (pf[pf.len - 1] != 0) {
// return error.ValueNotZeroTerminated;
// }
pfolder = pf.ptr; pfolder = pf.ptr;
} }
var x: C.http_settings_s = .{ var x: C.http_settings_s = .{