mirror of
https://github.com/zigzap/zap.git
synced 2025-10-21 07:34:08 +00:00
added example for sendFile
This commit is contained in:
parent
48e30ca35c
commit
b988454a72
6 changed files with 53 additions and 6 deletions
|
@ -33,6 +33,8 @@ Here's what works:
|
||||||
dispatching on the HTTP path
|
dispatching on the HTTP path
|
||||||
- **[serve](examples/serve/serve.zig)**: the traditional static web
|
- **[serve](examples/serve/serve.zig)**: the traditional static web
|
||||||
server with optional dynamic request handling
|
server with optional dynamic request handling
|
||||||
|
- **[sendfile](examples/sendfile/sendfile.zig)**: simple example of how to send
|
||||||
|
a file, honoring compression headers, etc.
|
||||||
- **[hello_json](examples/hello_json/hello_json.zig)**: serves you json
|
- **[hello_json](examples/hello_json/hello_json.zig)**: serves you json
|
||||||
dependent on HTTP path
|
dependent on HTTP path
|
||||||
- **[endpoint](examples/endpoint/)**: a simple JSON REST API example featuring
|
- **[endpoint](examples/endpoint/)**: a simple JSON REST API example featuring
|
||||||
|
|
|
@ -54,6 +54,7 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
.{ .name = "cookies", .src = "examples/cookies/cookies.zig" },
|
.{ .name = "cookies", .src = "examples/cookies/cookies.zig" },
|
||||||
.{ .name = "websockets", .src = "examples/websockets/websockets.zig" },
|
.{ .name = "websockets", .src = "examples/websockets/websockets.zig" },
|
||||||
.{ .name = "userpass_session", .src = "examples/userpass_session_auth/userpass_session_auth.zig" },
|
.{ .name = "userpass_session", .src = "examples/userpass_session_auth/userpass_session_auth.zig" },
|
||||||
|
.{ .name = "sendfile", .src = "examples/sendfile/sendfile.zig" },
|
||||||
}) |excfg| {
|
}) |excfg| {
|
||||||
const ex_name = excfg.name;
|
const ex_name = excfg.name;
|
||||||
const ex_src = excfg.src;
|
const ex_src = excfg.src;
|
||||||
|
@ -148,10 +149,6 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
sendfile_tests.linkLibrary(facil_dep.artifact("facil.io"));
|
sendfile_tests.linkLibrary(facil_dep.artifact("facil.io"));
|
||||||
sendfile_tests.addModule("zap", zap_module);
|
sendfile_tests.addModule("zap", zap_module);
|
||||||
const run_sendfile_tests = b.addRunArtifact(sendfile_tests);
|
const run_sendfile_tests = b.addRunArtifact(sendfile_tests);
|
||||||
// TODO: for some reason, tests aren't run more than once unless
|
|
||||||
// dependencies have changed.
|
|
||||||
// So, for now, we just force the exe to be built, so in order that
|
|
||||||
// we can call it again when needed.
|
|
||||||
const install_sendfile_tests = b.addInstallArtifact(sendfile_tests);
|
const install_sendfile_tests = b.addInstallArtifact(sendfile_tests);
|
||||||
|
|
||||||
// test commands
|
// test commands
|
||||||
|
|
46
examples/sendfile/sendfile.zig
Normal file
46
examples/sendfile/sendfile.zig
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
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!
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
}
|
1
examples/sendfile/testfile.txt
Normal file
1
examples/sendfile/testfile.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
All your codebase are belong to us!
|
|
@ -38,7 +38,7 @@ test "send file" {
|
||||||
// setup listener
|
// setup listener
|
||||||
var listener = zap.SimpleHttpListener.init(
|
var listener = zap.SimpleHttpListener.init(
|
||||||
.{
|
.{
|
||||||
.port = 3001,
|
.port = 3002,
|
||||||
.on_request = on_request,
|
.on_request = on_request,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -48,7 +48,7 @@ test "send file" {
|
||||||
zap.enableDebugLog();
|
zap.enableDebugLog();
|
||||||
try listener.listen();
|
try listener.listen();
|
||||||
|
|
||||||
const thread = try makeRequestThread(allocator, "http://127.0.0.1:3001/?file=src/tests/testfile.txt");
|
const thread = try makeRequestThread(allocator, "http://127.0.0.1:3002/?file=src/tests/testfile.txt");
|
||||||
defer thread.join();
|
defer thread.join();
|
||||||
zap.start(.{
|
zap.start(.{
|
||||||
.threads = 1,
|
.threads = 1,
|
||||||
|
|
|
@ -12,4 +12,5 @@ pkghash
|
||||||
cookies
|
cookies
|
||||||
websockets
|
websockets
|
||||||
userpass_session
|
userpass_session
|
||||||
|
sendfile
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue