adapt handle function type
This commit is contained in:
parent
f6b25566f6
commit
0c8ec8e8a8
4 changed files with 9 additions and 9 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
zig-cache/
|
10
README.md
10
README.md
|
@ -14,17 +14,17 @@ const Server = zrv.Server;
|
||||||
const Route = zrv.Route;
|
const Route = zrv.Route;
|
||||||
const allocator = std.heap.page_allocator; // Choose any allocator you want!
|
const allocator = std.heap.page_allocator; // Choose any allocator you want!
|
||||||
|
|
||||||
fn index(req: Request) Response {
|
fn index(req: *Request) Response {
|
||||||
_=req;
|
_=req;
|
||||||
return Response.write("hello!");
|
return Response.write("hello!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn about(req: Request) Response {
|
fn about(req: *Request) Response {
|
||||||
_=req;
|
_=req;
|
||||||
return Response.write("about site");
|
return Response.write("about site");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn writeJson(req: Request) Response {
|
fn writeJson(req: *Request) Response {
|
||||||
_=req;
|
_=req;
|
||||||
Response.json("[1, 2, 3, 4]");
|
Response.json("[1, 2, 3, 4]");
|
||||||
}
|
}
|
||||||
|
@ -57,11 +57,11 @@ const rt = [_]Route{.{"/hello", helloFunction}};
|
||||||
|
|
||||||
### Handler Functions
|
### Handler Functions
|
||||||
|
|
||||||
Every Request is handled by a handler function. It has to be of this type: `fn(req: Request) Response`
|
Every Request is handled by a handler function. It has to be of this type: `fn(req: *Request) Response`
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```zig
|
```zig
|
||||||
fn hello(req: Request) Response {
|
fn hello(req: *Request) Response {
|
||||||
_ = req;
|
_ = req;
|
||||||
return Response.write("hello"); // `Server` will return a Reponse with body "hello". You will see "hello" on your browser.
|
return Response.write("hello"); // `Server` will return a Reponse with body "hello". You will see "hello" on your browser.
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ pub const Server = struct {
|
||||||
const server_options: std.net.StreamServer.Options = .{};
|
const server_options: std.net.StreamServer.Options = .{};
|
||||||
var server = std.net.StreamServer.init(server_options);
|
var server = std.net.StreamServer.init(server_options);
|
||||||
defer server.deinit();
|
defer server.deinit();
|
||||||
defer server.close();
|
|
||||||
const addr = try std.net.Address.parseIp(ip, port);
|
const addr = try std.net.Address.parseIp(ip, port);
|
||||||
|
|
||||||
try server.listen(addr);
|
try server.listen(addr);
|
||||||
|
@ -67,7 +66,7 @@ pub const Server = struct {
|
||||||
// Check if there is a match
|
// Check if there is a match
|
||||||
if (eql(u8, req_path, req.uri)) {
|
if (eql(u8, req_path, req.uri)) {
|
||||||
// Change response with handling function in case of match.
|
// Change response with handling function in case of match.
|
||||||
res = r[1](req);
|
res = r[1](&req);
|
||||||
// Exit loop in case of match
|
// Exit loop in case of match
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -164,6 +163,6 @@ test "Run server" {
|
||||||
try Server.listen("0.0.0.0", 8080, &rt, std.testing.allocator);
|
try Server.listen("0.0.0.0", 8080, &rt, std.testing.allocator);
|
||||||
}
|
}
|
||||||
// Function for test "Run Server"
|
// Function for test "Run Server"
|
||||||
fn handlefn(_: types.Request) types.Response {
|
fn handlefn(_: *types.Request) types.Response {
|
||||||
return types.Response.write("<h1>Run Server Test OK!</h1>");
|
return types.Response.write("<h1>Run Server Test OK!</h1>");
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ const stat = @import("./status.zig");
|
||||||
/// e.g. `const rt = Route{"/home", home};`
|
/// e.g. `const rt = Route{"/home", home};`
|
||||||
/// It it usual that a webapp handles more than one path so you can declare an array of `Route`
|
/// It it usual that a webapp handles more than one path so you can declare an array of `Route`
|
||||||
/// e.g. `const rt =[_]Route{.{"/index", index}, .{"/home", home}};`
|
/// e.g. `const rt =[_]Route{.{"/index", index}, .{"/home", home}};`
|
||||||
pub const Route = tuple(&.{ []const u8, *const fn (Request) Response });
|
pub const Route = tuple(&.{ []const u8, *const fn (*Request) Response });
|
||||||
|
|
||||||
/// A header of a `Request` or a `Response`.
|
/// A header of a `Request` or a `Response`.
|
||||||
/// It is usual that more than one is sent, so you can declare an array.
|
/// It is usual that more than one is sent, so you can declare an array.
|
||||||
|
|
Loading…
Add table
Reference in a new issue