11const std = @import ("std" );
22const Server = @import ("server.zig" ).Server ;
3- const Config = @import ("server.zig" ).Config ;
3+ const ServerConfig = @import ("server.zig" ).ServerConfig ;
44const core = @import ("core.zig" );
5+ const Router = @import ("router.zig" ).Router ;
56
67// Example middleware that logs requests
78const LoggerMiddleware = struct {
@@ -27,17 +28,33 @@ const LoggerMiddleware = struct {
2728 }
2829};
2930
31+ // Helper function to set text response
32+ fn setText (ctx : * core.Context , text : []const u8 ) ! void {
33+ ctx .response .body = try ctx .allocator .dupe (u8 , text );
34+ try ctx .response .headers .put ("Content-Type" , "text/plain" );
35+ }
36+
37+ // Helper function to set JSON response
38+ fn setJson (ctx : * core.Context , data : anytype ) ! void {
39+ var json_string = std .ArrayList (u8 ).init (ctx .allocator );
40+ defer json_string .deinit ();
41+
42+ try std .json .stringify (data , .{}, json_string .writer ());
43+ ctx .response .body = try ctx .allocator .dupe (u8 , json_string .items );
44+ try ctx .response .headers .put ("Content-Type" , "application/json" );
45+ }
46+
3047// Example handlers
3148fn homeHandlerImpl (ctx : * core.Context ) ! void {
32- try ctx . text ( "Welcome to Zup!" );
49+ try setText ( ctx , "Welcome to Zup!" );
3350}
3451
3552fn jsonHandlerImpl (ctx : * core.Context ) ! void {
3653 const data = .{
3754 .message = "Hello, JSON!" ,
3855 .timestamp = std .time .timestamp (),
3956 };
40- try ctx . json ( data );
57+ try setJson ( ctx , data );
4158}
4259
4360fn userHandlerImpl (ctx : * core.Context ) ! void {
@@ -47,11 +64,15 @@ fn userHandlerImpl(ctx: *core.Context) !void {
4764 .name = "Example User" ,
4865 .email = "user@example.com" ,
4966 };
50- try ctx . json ( response );
67+ try setJson ( ctx , response );
5168}
5269
5370fn echoHandlerImpl (ctx : * core.Context ) ! void {
54- try ctx .text (ctx .request .body );
71+ if (ctx .request .body ) | body | {
72+ try setText (ctx , body );
73+ } else {
74+ try setText (ctx , "No body provided" );
75+ }
5576}
5677
5778pub fn main () ! void {
@@ -60,24 +81,28 @@ pub fn main() !void {
6081 defer _ = gpa .deinit ();
6182 const allocator = gpa .allocator ();
6283
63- // Create server with custom config
64- var server = try Server .init (allocator , .{
65- .address = "127.0.0.1" ,
66- .port = 8080 ,
67- .thread_count = 4 ,
68- });
69- defer server .deinit ();
84+ // Create router
85+ var router = Router .init (allocator );
86+ defer router .deinit ();
7087
7188 // Add global middleware
7289 var logger = LoggerMiddleware .init ();
7390 defer logger .deinit ();
74- try server .use (core .Middleware .init (logger , LoggerMiddleware .handle ));
91+ try router .use (core .Middleware .init (logger , LoggerMiddleware .handle ));
7592
7693 // Define routes
77- try server .get ("/" , homeHandlerImpl );
78- try server .get ("/json" , jsonHandlerImpl );
79- try server .get ("/users/:id" , userHandlerImpl );
80- try server .post ("/echo" , echoHandlerImpl );
94+ try router .get ("/" , homeHandlerImpl );
95+ try router .get ("/json" , jsonHandlerImpl );
96+ try router .get ("/users/:id" , userHandlerImpl );
97+ try router .post ("/echo" , echoHandlerImpl );
98+
99+ // Create server with custom config
100+ var server = try Server .init (allocator , .{
101+ .host = "127.0.0.1" ,
102+ .port = 8080 ,
103+ .thread_count = 4 ,
104+ }, & router );
105+ defer server .deinit ();
81106
82107 // Start server
83108 std .log .info ("Server running at http://127.0.0.1:8080" , .{});
@@ -88,33 +113,46 @@ test "basic routes" {
88113 const testing = std .testing ;
89114 const allocator = testing .allocator ;
90115
91- var server = try Server .init (allocator , .{
92- .port = 0 , // Random port for testing
93- });
94- defer server .deinit ();
116+ // Create router
117+ var router = Router .init (allocator );
118+ defer router .deinit ();
95119
96120 // Add test routes
97- try server .get ("/test" , & struct {
121+ try router .get ("/test" , & struct {
98122 fn handler (ctx : * core.Context ) ! void {
99- try ctx . text ( "test ok" );
123+ try setText ( ctx , "test ok" );
100124 }
101125 }.handler );
102126
103- try server .post ("/echo" , echoHandlerImpl );
127+ try router .post ("/echo" , echoHandlerImpl );
128+
129+ // Create server
130+ var server = try Server .init (allocator , .{
131+ .port = 0 , // Random port for testing
132+ .thread_count = 1 ,
133+ }, & router );
134+ defer server .deinit ();
104135
105136 // Start server in background
106- const thread = try std .Thread .spawn (.{}, Server .start , .{& server });
107- defer {
108- server .running .store (false , .release );
109- thread .join ();
110- }
137+ var running = true ;
138+ const thread = try std .Thread .spawn (.{}, struct {
139+ fn run (srv : * Server , is_running : * bool ) void {
140+ srv .start () catch | err | {
141+ std .debug .print ("Server error: {}\n " , .{err });
142+ };
143+ is_running .* = false ;
144+ }
145+ }.run , .{& server , & running });
111146
112147 // Wait a bit for server to start
113- std .time .sleep (10 * std .time .ns_per_ms );
148+ std .time .sleep (100 * std .time .ns_per_ms );
149+
150+ // Get server address
151+ const server_address = server .address ;
114152
115153 // Test GET request
116154 {
117- const client = try std .net .tcpConnectToAddress (server . listener . listen_address );
155+ const client = try std .net .tcpConnectToAddress (server_address );
118156 defer client .close ();
119157
120158 try client .writer ().writeAll (
@@ -134,7 +172,7 @@ test "basic routes" {
134172
135173 // Test POST request
136174 {
137- const client = try std .net .tcpConnectToAddress (server . listener . listen_address );
175+ const client = try std .net .tcpConnectToAddress (server_address );
138176 defer client .close ();
139177
140178 const body = "Hello, Echo!" ;
@@ -156,4 +194,8 @@ test "basic routes" {
156194
157195 try testing .expect (std .mem .indexOf (u8 , response , body ) != null );
158196 }
159- }
197+
198+ // Stop server
199+ server .stop ();
200+ thread .join ();
201+ }
0 commit comments