| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- 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 async HttpResult handle_request(HttpContext context, RouteContext 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 application = new WebApplication(8080);
-
- application.container.register_scoped<Endpoint>(() => new RemoteAddressEndpoint())
- .with_metadata<EndpointRoute>(new EndpointRoute("*"));
-
- print("Remote Address Example Server running on port 8080\n");
- print("Try: http://localhost:8080/\n");
-
- application.run();
- }
|