@@ -16,25 +16,26 @@ const FsDir = @import("fs_dir.zig").FsDir;
16
16
const Context = @import ("../context.zig" ).Context ;
17
17
const Layer = @import ("middleware.zig" ).Layer ;
18
18
19
+ const MiddlewareWithData = @import ("middleware.zig" ).MiddlewareWithData ;
20
+
19
21
pub const HandlerFn = * const fn (* const Context , usize ) anyerror ! Respond ;
20
22
pub fn TypedHandlerFn (comptime T : type ) type {
21
23
return * const fn (* const Context , T ) anyerror ! Respond ;
22
24
}
23
25
24
26
pub const HandlerWithData = struct {
25
27
handler : HandlerFn ,
28
+ middlewares : []const MiddlewareWithData ,
26
29
data : usize ,
27
30
};
28
31
29
32
/// Structure of a server route definition.
30
33
pub const Route = struct {
31
- const Self = @This ();
32
-
33
34
/// Defined route path.
34
35
path : []const u8 ,
35
36
36
- /// Route handlers .
37
- handlers : [9 ]? HandlerWithData = [ _ ] ? HandlerWithData {null } ** 9 ,
37
+ /// Route Handlers .
38
+ handlers : [9 ]? HandlerWithData = . {null } ** 9 ,
38
39
39
40
fn method_to_index (method : Method ) u32 {
40
41
return switch (method ) {
@@ -51,13 +52,13 @@ pub const Route = struct {
51
52
}
52
53
53
54
/// Initialize a route for the given path.
54
- pub fn init (path : []const u8 ) Self {
55
- return Self { .path = path };
55
+ pub fn init (path : []const u8 ) Route {
56
+ return Route { .path = path };
56
57
}
57
58
58
59
/// Returns a comma delinated list of allowed Methods for this route. This
59
60
/// is meant to be used as the value for the 'Allow' header in the Response.
60
- pub fn get_allowed (self : Self , allocator : std.mem.Allocator ) ! []const u8 {
61
+ pub fn get_allowed (self : Route , allocator : std.mem.Allocator ) ! []const u8 {
61
62
// This gets allocated within the context of the connection's arena.
62
63
const allowed_size = comptime blk : {
63
64
var size = 0 ;
@@ -89,82 +90,84 @@ pub const Route = struct {
89
90
90
91
/// Get a defined request handler for the provided method.
91
92
/// Return NULL if no handler is defined for this method.
92
- pub fn get_handler (self : Self , method : Method ) ? HandlerWithData {
93
+ pub fn get_handler (self : Route , method : Method ) ? HandlerWithData {
93
94
return self .handlers [method_to_index (method )];
94
95
}
95
96
96
- pub fn layer (self : Self ) Layer {
97
+ pub fn layer (self : Route ) Layer {
97
98
return .{ .route = self };
98
99
}
99
100
100
101
/// Set a handler function for the provided method.
101
102
inline fn inner_route (
102
103
comptime method : Method ,
103
- self : Self ,
104
+ self : Route ,
104
105
data : anytype ,
105
106
handler_fn : TypedHandlerFn (@TypeOf (data )),
106
- ) Self {
107
+ ) Route {
107
108
const wrapped = wrap (usize , data );
108
109
var new_handlers = self .handlers ;
109
110
new_handlers [comptime method_to_index (method )] = .{
110
111
.handler = @ptrCast (handler_fn ),
112
+ .middlewares = &.{},
111
113
.data = wrapped ,
112
114
};
113
115
114
- return Self { .path = self .path , .handlers = new_handlers };
116
+ return Route { .path = self .path , .handlers = new_handlers };
115
117
}
116
118
117
119
/// Set a handler function for all methods.
118
- pub fn all (self : Self , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Self {
120
+ pub fn all (self : Route , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Route {
119
121
const wrapped = wrap (usize , data );
120
122
var new_handlers = self .handlers ;
121
123
122
124
for (& new_handlers ) | * new_handler | {
123
125
new_handler .* = .{
124
126
.handler = @ptrCast (handler_fn ),
127
+ .middlewares = &.{},
125
128
.data = wrapped ,
126
129
};
127
130
}
128
131
129
- return Self {
132
+ return Route {
130
133
.path = self .path ,
131
134
.handlers = new_handlers ,
132
135
};
133
136
}
134
137
135
- pub fn get (self : Self , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Self {
138
+ pub fn get (self : Route , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Route {
136
139
return inner_route (.GET , self , data , handler_fn );
137
140
}
138
141
139
- pub fn head (self : Self , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Self {
142
+ pub fn head (self : Route , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Route {
140
143
return inner_route (.HEAD , self , handler_fn );
141
144
}
142
145
143
- pub fn post (self : Self , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Self {
146
+ pub fn post (self : Route , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Route {
144
147
return inner_route (.POST , self , data , handler_fn );
145
148
}
146
149
147
- pub fn put (self : Self , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Self {
150
+ pub fn put (self : Route , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Route {
148
151
return inner_route (.PUT , self , data , handler_fn );
149
152
}
150
153
151
- pub fn delete (self : Self , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Self {
154
+ pub fn delete (self : Route , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Route {
152
155
return inner_route (.DELETE , self , data , handler_fn );
153
156
}
154
157
155
- pub fn connect (self : Self , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Self {
158
+ pub fn connect (self : Route , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Route {
156
159
return inner_route (.CONNECT , self , data , handler_fn );
157
160
}
158
161
159
- pub fn options (self : Self , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Self {
162
+ pub fn options (self : Route , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Route {
160
163
return inner_route (.OPTIONS , self , data , handler_fn );
161
164
}
162
165
163
- pub fn trace (self : Self , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Self {
166
+ pub fn trace (self : Route , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Route {
164
167
return inner_route (.TRACE , self , data , handler_fn );
165
168
}
166
169
167
- pub fn patch (self : Self , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Self {
170
+ pub fn patch (self : Route , data : anytype , handler_fn : TypedHandlerFn (@TypeOf (data ))) Route {
168
171
return inner_route (.PATCH , self , data , handler_fn );
169
172
}
170
173
@@ -177,10 +180,10 @@ pub const Route = struct {
177
180
178
181
/// Define a GET handler to serve an embedded file.
179
182
pub fn embed_file (
180
- self : * const Self ,
183
+ self : * const Route ,
181
184
comptime opts : ServeEmbeddedOptions ,
182
185
comptime bytes : []const u8 ,
183
- ) Self {
186
+ ) Route {
184
187
return self .get ({}, struct {
185
188
fn handler_fn (ctx : * const Context , _ : void ) ! Respond {
186
189
var header_list = try std .ArrayListUnmanaged ([2 ][]const u8 ).initCapacity (ctx .allocator , 3 );
0 commit comments