pub fn handle_request(request) {
// Get request details
let method = request.method;
let path = request.path;
let query = request.query;
let body = request.body;
match path.parts() {
// Example: Simple string response (will be 200 OK)
["ping"] => "pong",
// Example: Echo endpoint that returns the request body
["echo"] => body,
// Example: Mock API with JSON response
["api", "users"] => [ //
#{ "id": 1, "name": "Alice" },
#{ "id": 2, "name": "Bob" },
],
// Example: Mock authentication endpoint
["api", "login"] if method == "POST" => #{ "token": "mock-jwt-token-abc123", "user": "test_user" },
// Example: Simulate different HTTP status codes
["error", "404"] => (404, "Not Found"),
["error", "500"] => (500, "Internal Server Error"),
// Example: Handle all paths under /api/mock
["api", "mock", ..] => #{ mocked: true, path, method, query },
// Example: Mock API with JSON response
["demo"] => [ //
#{ id: 1, name: "Alice" },
#{ id: 2, name: "Bob", details: #{ "age": 30, "city": "New York" } },
],
}
// if nothing is returned, it will be treated as unhandled and the request will be sent to the
// upstream server, if it is configured
//
// You can also explicitly return () to indicate that the request is unhandled
}