| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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, RequestHandler {
- 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<string, string> get_json_headers() {
- var headers = new Catalogue<string, string>();
- headers.add("Content-Type", "application/json");
- headers.add("Access-Control-Allow-Origin", "*");
- return headers;
- }
- }
|