using Astralis; using Invercargill; using Invercargill.DataStructures; /** * RemoteAddress Example * * Demonstrates accessing client remote address information. */ // Endpoint that responds with client information public class RemoteAddressEndpoint : Object, Endpoint { public string route { get { return "*"; } } public Method[] methods { owned get { return { Method.GET }; } } public async HttpResult handle_request(HttpContext context, RouteInformation route) throws Error { var request = context.request; // Access the remote address var remote_address = request.remote_address; // Build JSON response with client info using string interpolation var json = @"{ \"method\": \"$(request.method)\", \"path\": \"$(request.raw_path)\", \"remote_address\": \"$(remote_address ?? "unknown")\", \"user_agent\": \"$(request.user_agent ?? "unknown")\" }"; var response = new HttpStringResult(json) .set_header("Content-Type", "application/json") .set_header("Access-Control-Allow-Origin", "*"); // Log the request to console print(@"[$(get_current_time())] $(request.method) $(request.raw_path) from $(remote_address ?? "unknown")\n"); return response; } private static string get_current_time() { var now = new DateTime.now_local(); return now.format("%Y-%m-%d %H:%M:%S"); } } void main() { var router = new EndpointRouter() .add_endpoint(new RemoteAddressEndpoint()); var pipeline = new Pipeline() .add_component(router); var server = new Server(8080, pipeline); print("Remote Address Example Server running on port 8080\n"); print("Try: http://localhost:8080/\n"); server.run(); }