1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 07:04:08 +00:00

added header to example main source files showing how to build & run

This commit is contained in:
renerocksai 2025-03-30 19:40:08 +02:00
parent cc6d55fbf7
commit 4c59132a08
No known key found for this signature in database
22 changed files with 157 additions and 6 deletions

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build accept`.
//! Run me with `zig build run-accept`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build app_auth`.
//! Run me with `zig build run-app_auth`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build app_basic`.
//! Run me with `zig build run-app_basic`.
//!
const std = @import("std");
const Allocator = std.mem.Allocator;
@ -32,7 +38,7 @@ const SimpleEndpoint = struct {
}
// handle GET requests
pub fn get(e: *SimpleEndpoint, arena: Allocator, context: *MyContext, r: zap.Request) anyerror!void {
pub fn get(e: *SimpleEndpoint, arena: Allocator, context: *MyContext, r: zap.Request) !void {
const thread_id = std.Thread.getCurrentId();
r.setStatus(.ok);
@ -55,11 +61,11 @@ const SimpleEndpoint = struct {
}
// empty stubs for all other request methods
pub fn post(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) anyerror!void {}
pub fn put(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) anyerror!void {}
pub fn delete(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) anyerror!void {}
pub fn patch(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) anyerror!void {}
pub fn options(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) anyerror!void {}
pub fn post(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
pub fn put(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
pub fn delete(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
pub fn patch(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
pub fn options(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
};
pub fn main() !void {

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build bindataformpost`.
//! Run me with `zig build run-bindataformpost`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build cookies`.
//! Run me with `zig build run-cookies`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -0,0 +1,22 @@
const std = @import("std");
const zap = @import("zap");
/// A simple endpoint listening on the /error route that causes an error on GET requests, which gets logged to the response (=browser) by default
pub const ErrorEndpoint = @This();
path: []const u8 = "/error",
error_strategy: zap.Endpoint.ErrorStrategy = .log_to_response,
pub fn get(e: *ErrorEndpoint, r: zap.Request) !void {
_ = e;
_ = r;
return error.@"Oh-no!";
}
pub fn post(_: *ErrorEndpoint, _: zap.Request) !void {}
// pub fn post(_: *ErrorEndpoint, _: zap.Request) !void {}
pub fn put(_: *ErrorEndpoint, _: zap.Request) !void {}
pub fn delete(_: *ErrorEndpoint, _: zap.Request) !void {}
pub fn patch(_: *ErrorEndpoint, _: zap.Request) !void {}
pub fn options(_: *ErrorEndpoint, _: zap.Request) !void {}

View file

@ -1,7 +1,14 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build endpoint`.
//! Run me with `zig build run-endpoint`.
//!
const std = @import("std");
const zap = @import("zap");
const UserWeb = @import("userweb.zig");
const StopEndpoint = @import("stopendpoint.zig");
const ErrorEndpoint = @import("error.zig");
// this is just to demo that we can catch arbitrary slugs as fallback
fn on_request(r: zap.Request) !void {
@ -39,10 +46,12 @@ pub fn main() !void {
defer userWeb.deinit();
var stopEp = StopEndpoint.init("/stop");
var errorEp: ErrorEndpoint = .{};
// register endpoints with the listener
try listener.register(&userWeb);
try listener.register(&stopEp);
try listener.register(&errorEp);
// fake some users
var uid: usize = undefined;

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build hello`.
//! Run me with `zig build run-hello`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build hello2`.
//! Run me with `zig build run-hello2`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build hello_json`.
//! Run me with `zig build run-hello_json`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build http_params`.
//! Run me with `zig build run-http_params`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build -Dopenssl=true https`.
//! Run me with `zig build -Dopenssl=true run-https`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build middleware`.
//! Run me with `zig build run-middleware`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build middleware_with_endpoint`.
//! Run me with `zig build run-middleware_with_endpoint`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build mustache`.
//! Run me with `zig build run-mustache`.
//!
const std = @import("std");
const zap = @import("zap");
const Mustache = @import("zap").Mustache;

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build routes`.
//! Run me with `zig build run-routes`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build senderror`.
//! Run me with `zig build run-senderror`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build sendfile`.
//! Run me with `zig build run-sendfile`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build serve`.
//! Run me with `zig build run-serve`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build simple_router`.
//! Run me with `zig build run-simple_router`.
//!
const std = @import("std");
const zap = @import("zap");
const Allocator = std.mem.Allocator;

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build userpass_session`.
//! Run me with `zig build run-userpass_session`.
//!
const std = @import("std");
const zap = @import("zap");

View file

@ -1,3 +1,9 @@
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build websockets`.
//! Run me with `zig build run-websockets`.
//!
const std = @import("std");
const zap = @import("zap");
const WebSockets = zap.WebSockets;