using Astralis; using Invercargill; using Invercargill.DataStructures; public class RemoteAddressExample : Object { public static int main(string[] args) { // Create a simple handler that logs the client's remote address var handler = new SimpleHttpHandler(); // Start the server on port 8080 var server = new Server(8080, handler); server.run(); return 0; } } // Simple handler that responds with client information public class SimpleHttpHandler : Object, HttpHandler { public async HttpResult handle(HttpContext context) { 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 BufferedHttpResult.from_string( json, StatusCode.OK, get_json_headers() ); // Log the request to console print(@"[$(get_current_time())] $(request.method) $(request.raw_path) from $(remote_address ?? "unknown")"); return response; } private static string get_current_time() { var now = new DateTime.now_local(); return now.format("%Y-%m-%d %H:%M:%S"); } private static Catalogue get_json_headers() { var headers = new Catalogue(); headers.add("Content-Type", "application/json"); headers.add("Access-Control-Allow-Origin", "*"); return headers; } }