mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 05:56:07 +00:00
Public routing path
Set `public_routing_path` config option to specify a request path prefix for public requests.
This commit is contained in:
parent
fe49185c32
commit
2dd4a19e2e
@ -66,6 +66,10 @@ pub const jetzig_options = struct {
|
||||
// Path relative to cwd() to serve public content from. Symlinks are not followed.
|
||||
pub const public_content_path = "public";
|
||||
|
||||
/// Request path to map to public directory, e.g. if `public_routing_path` is `"/foo"` then a
|
||||
/// request to `/foo/bar.png` will serve static content found in `public/bar.png`
|
||||
pub const public_routing_path = "/";
|
||||
|
||||
// HTTP buffer. Must be large enough to store all headers. This should typically not be modified.
|
||||
pub const http_buffer_size: usize = std.math.pow(usize, 2, 16);
|
||||
|
||||
|
@ -84,6 +84,10 @@ pub const max_connections: u16 = 512;
|
||||
/// Path relative to cwd() to serve public content from. Symlinks are not followed.
|
||||
pub const public_content_path = "public";
|
||||
|
||||
/// Request path to map to public directory, e.g. if `public_routing_path` is `"/foo"` then a
|
||||
/// request to `/foo/bar.png` will serve static content found in `public/bar.png`
|
||||
pub const public_routing_path = "/";
|
||||
|
||||
/// Middleware chain. Add any custom middleware here, or use middleware provided in
|
||||
/// `jetzig.middleware` (e.g. `jetzig.middleware.HtmxMiddleware`).
|
||||
pub const middleware = &.{};
|
||||
|
@ -754,9 +754,16 @@ fn matchStaticResource(self: *Server, request: *jetzig.http.Request) !?StaticRes
|
||||
}
|
||||
|
||||
fn matchPublicContent(self: *Server, request: *jetzig.http.Request) !?StaticResource {
|
||||
if (request.path.file_path.len <= 1) return null;
|
||||
const public_routing_path = comptime jetzig.config.get([]const u8, "public_routing_path");
|
||||
if (!std.mem.startsWith(u8, request.path.file_path, public_routing_path)) return null;
|
||||
if (request.method != .GET) return null;
|
||||
|
||||
const match_path = if (comptime std.mem.endsWith(u8, public_routing_path, "/"))
|
||||
request.path.file_path[public_routing_path.len..]
|
||||
else if (request.path.file_path.len <= public_routing_path.len + 1) {
|
||||
return null;
|
||||
} else request.path.file_path[public_routing_path.len + 1 ..];
|
||||
|
||||
var iterable_dir = std.fs.cwd().openDir(
|
||||
jetzig.config.get([]const u8, "public_content_path"),
|
||||
.{ .iterate = true, .no_follow = true },
|
||||
@ -777,7 +784,7 @@ fn matchPublicContent(self: *Server, request: *jetzig.http.Request) !?StaticReso
|
||||
_ = std.mem.replace(u8, file.path, std.fs.path.sep_str_windows, std.fs.path.sep_str_posix, &path_buffer);
|
||||
break :blk path_buffer[0..file.path.len];
|
||||
} else file.path;
|
||||
if (std.mem.eql(u8, file_path, request.path.file_path[1..])) {
|
||||
if (std.mem.eql(u8, file_path, match_path)) {
|
||||
const content = try iterable_dir.readFileAlloc(
|
||||
request.allocator,
|
||||
file_path,
|
||||
|
Loading…
x
Reference in New Issue
Block a user