forked from zigzap/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendfile.zig
49 lines (41 loc) · 1.5 KB
/
sendfile.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const std = @import("std");
const zap = @import("zap");
var buffer: [1024]u8 = undefined;
var read_len: ?usize = null;
const testfile = @embedFile("testfile.txt");
pub fn on_request(r: zap.SimpleRequest) void {
// Sends a file if present in the filesystem orelse returns an error.
//
// - efficiently sends a file using gzip compression
// - also handles range requests if `Range` or `If-Range` headers are present in the request.
// - sends the response headers and the specified file (the response's body).
//
// On success, the `r.h` handle will be consumed and invalid.
// On error, the handle will still be valid and should be used to send an error response
//
// Important: sets last-modified and cache-control headers with a max-age value of 1 hour!
// In this example, we disable caching
r.setHeader("Cache-Control", "no-cache") catch unreachable;
if (r.sendFile("examples/sendfile/testfile.txt")) {} else |err| {
std.log.err("Unable to send file: {any}", .{err});
}
}
pub fn main() !void {
// setup listener
var listener = zap.SimpleHttpListener.init(
.{
.port = 3000,
.on_request = on_request,
.log = true,
.max_clients = 10,
.max_body_size = 1 * 1024, // careful here
},
);
zap.enableDebugLog();
try listener.listen();
std.debug.print("Visit me on http://127.0.0.1:3000\n", .{});
zap.start(.{
.threads = 1,
.workers = 0,
});
}