mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
added https example
This commit is contained in:
parent
edf585c220
commit
5e789ee87a
2 changed files with 82 additions and 0 deletions
|
@ -26,6 +26,7 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
src: []const u8,
|
src: []const u8,
|
||||||
}{
|
}{
|
||||||
.{ .name = "hello", .src = "examples/hello/hello.zig" },
|
.{ .name = "hello", .src = "examples/hello/hello.zig" },
|
||||||
|
.{ .name = "https", .src = "examples/https/https.zig" },
|
||||||
.{ .name = "hello2", .src = "examples/hello2/hello2.zig" },
|
.{ .name = "hello2", .src = "examples/hello2/hello2.zig" },
|
||||||
.{ .name = "routes", .src = "examples/routes/routes.zig" },
|
.{ .name = "routes", .src = "examples/routes/routes.zig" },
|
||||||
.{ .name = "serve", .src = "examples/serve/serve.zig" },
|
.{ .name = "serve", .src = "examples/serve/serve.zig" },
|
||||||
|
@ -47,6 +48,7 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
}) |excfg| {
|
}) |excfg| {
|
||||||
const ex_name = excfg.name;
|
const ex_name = excfg.name;
|
||||||
const ex_src = excfg.src;
|
const ex_src = excfg.src;
|
||||||
|
|
||||||
const ex_build_desc = try std.fmt.allocPrint(
|
const ex_build_desc = try std.fmt.allocPrint(
|
||||||
b.allocator,
|
b.allocator,
|
||||||
"build the {s} example",
|
"build the {s} example",
|
||||||
|
|
80
examples/https/https.zig
Normal file
80
examples/https/https.zig
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const zap = @import("zap");
|
||||||
|
|
||||||
|
fn on_request_verbose(r: zap.SimpleRequest) void {
|
||||||
|
if (r.path) |the_path| {
|
||||||
|
std.debug.print("PATH: {s}\n", .{the_path});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r.query) |the_query| {
|
||||||
|
std.debug.print("QUERY: {s}\n", .{the_query});
|
||||||
|
}
|
||||||
|
r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_request_minimal(r: zap.SimpleRequest) void {
|
||||||
|
r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn help_and_exit(filename: []const u8, err: anyerror) void {
|
||||||
|
std.debug.print(
|
||||||
|
\\ Error: File `{s}` : {any}
|
||||||
|
\\
|
||||||
|
\\ To generate both the certificate file and the key file, use the following command:
|
||||||
|
\\
|
||||||
|
\\ **********************************************************************************************
|
||||||
|
\\ openssl req -x509 -nodes -days 365 -sha256 -newkey rsa:2048 -keyout mykey.pem -out mycert.pem
|
||||||
|
\\ **********************************************************************************************
|
||||||
|
\\
|
||||||
|
\\ After that, run this example again
|
||||||
|
,
|
||||||
|
.{ filename, err },
|
||||||
|
);
|
||||||
|
std.os.exit(1);
|
||||||
|
}
|
||||||
|
pub fn main() !void {
|
||||||
|
const CERT_FILE = "mycert.pem";
|
||||||
|
const KEY_FILE = "mykey.pem";
|
||||||
|
|
||||||
|
std.fs.cwd().access(CERT_FILE, .{}) catch |err| {
|
||||||
|
help_and_exit(CERT_FILE, err);
|
||||||
|
};
|
||||||
|
|
||||||
|
std.fs.cwd().access(KEY_FILE, .{}) catch |err| {
|
||||||
|
help_and_exit(KEY_FILE, err);
|
||||||
|
};
|
||||||
|
|
||||||
|
const tls = zap.fio_tls_new(
|
||||||
|
"localhost:4443",
|
||||||
|
CERT_FILE,
|
||||||
|
KEY_FILE,
|
||||||
|
null, // key file is not password-protected
|
||||||
|
);
|
||||||
|
|
||||||
|
var listener = zap.SimpleHttpListener.init(.{
|
||||||
|
.port = 4443,
|
||||||
|
.on_request = on_request_verbose,
|
||||||
|
.log = true,
|
||||||
|
.max_clients = 100000,
|
||||||
|
.tls = tls,
|
||||||
|
});
|
||||||
|
try listener.listen();
|
||||||
|
|
||||||
|
std.debug.print("Listening on 0.0.0.0:4443\n", .{});
|
||||||
|
std.debug.print("", .{});
|
||||||
|
std.debug.print(
|
||||||
|
\\
|
||||||
|
\\ ***********************************************
|
||||||
|
\\ *** Try me with: curl -k -v localhost:4443/ ***
|
||||||
|
\\ ***********************************************
|
||||||
|
\\
|
||||||
|
\\Your browser may lie to you, indicate a non-secure connection because of the self-created certificate, and make you believe that HTTPS / TLS "does not work".
|
||||||
|
\\
|
||||||
|
, .{});
|
||||||
|
|
||||||
|
// start worker threads
|
||||||
|
zap.start(.{
|
||||||
|
.threads = 2,
|
||||||
|
.workers = 1,
|
||||||
|
});
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue