From bbfc6f2a6d3a1055e86e54d03b4e5a00d12f5c54 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Sat, 31 Jan 2026 23:08:47 +0900 Subject: [PATCH] Refactor IApiEndpoint and update ResourceServer usage Expanded the IApiEndpoint interface to support advanced routing with a new CanHandle overload and improved documentation. Renamed internal variables in ResourceServer from _tools to _apis to better reflect their purpose and updated all related logic accordingly. --- .../WelsonJS.Launcher/IApiEndpoint.cs | 52 +++++++++++++++---- .../WelsonJS.Launcher/ResourceServer.cs | 26 +++++----- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/WelsonJS.Augmented/WelsonJS.Launcher/IApiEndpoint.cs b/WelsonJS.Augmented/WelsonJS.Launcher/IApiEndpoint.cs index 6633774..07ccc62 100644 --- a/WelsonJS.Augmented/WelsonJS.Launcher/IApiEndpoint.cs +++ b/WelsonJS.Augmented/WelsonJS.Launcher/IApiEndpoint.cs @@ -1,4 +1,4 @@ -// IResourceTool.cs +// IApiEndpoint.cs // SPDX-License-Identifier: GPL-3.0-or-later // SPDX-FileCopyrightText: 2025 Catswords OSS and WelsonJS Contributors // https://github.com/gnh1201/welsonjs @@ -9,22 +9,56 @@ using System.Threading.Tasks; namespace WelsonJS.Launcher { /// - /// Defines a contract for resource tools that can handle specific HTTP requests. + /// Defines a contract for API endpoints that can selectively handle incoming HTTP requests. /// public interface IApiEndpoint { /// - /// Determines whether this tool can handle the specified path. + /// Determines whether this endpoint can handle a request + /// based solely on the request path. /// - /// The request path to check. - /// True if this tool can handle the request; otherwise, false. + /// + /// This method is typically used for fast, lightweight routing decisions + /// before inspecting headers, HTTP methods, or request bodies. + /// Implementations should avoid side effects and expensive operations. + /// + /// The normalized request path (e.g. "/api/status"). + /// + /// true if this endpoint is responsible for the given path; + /// otherwise, false. + /// bool CanHandle(string path); + /// - /// Asynchronously processes the HTTP request for the specified path. + /// Determines whether this endpoint can handle the given HTTP request + /// using the full . /// - /// The HTTP listener context containing request and response objects. - /// The request path to handle. - /// A task representing the asynchronous operation. + /// + /// This overload allows more advanced routing decisions based on + /// HTTP method, headers, query parameters, authentication state, + /// or other contextual information. + /// + /// + /// The HTTP listener context containing request and connection details. + /// + /// + /// true if this endpoint can process the request; + /// otherwise, false. + /// + bool CanHandle(HttpListenerContext context); + + /// + /// Asynchronously processes the HTTP request handled by this endpoint. + /// + /// + /// The HTTP listener context containing request and response objects. + /// + /// + /// The request path associated with this handler. + /// + /// + /// A task that represents the asynchronous handling operation. + /// Task HandleAsync(HttpListenerContext context, string path); } } \ No newline at end of file diff --git a/WelsonJS.Augmented/WelsonJS.Launcher/ResourceServer.cs b/WelsonJS.Augmented/WelsonJS.Launcher/ResourceServer.cs index f5055c5..9113dbb 100644 --- a/WelsonJS.Augmented/WelsonJS.Launcher/ResourceServer.cs +++ b/WelsonJS.Augmented/WelsonJS.Launcher/ResourceServer.cs @@ -31,7 +31,7 @@ namespace WelsonJS.Launcher private bool _isRunning; private string _prefix; private string _resourceName; - private List _tools = new List(); + private List _apis = new List(); private BlobConfig _blobConfig; private readonly ILog _logger; @@ -66,15 +66,15 @@ namespace WelsonJS.Launcher _logger?.Error($"FetchBlobConfig failed: {t.Exception}"); }, TaskScheduler.Default); - // Add resource tools - _tools.Add(new ResourceTools.Completion(this, _httpClient, _logger)); - _tools.Add(new ResourceTools.Settings(this, _httpClient, _logger)); - _tools.Add(new ResourceTools.ChromiumDevTools(this, _httpClient, _logger)); - _tools.Add(new ResourceTools.DnsQuery(this, _httpClient, _logger)); - _tools.Add(new ResourceTools.IpQuery(this, _httpClient, _logger)); - _tools.Add(new ResourceTools.TwoFactorAuth(this, _httpClient, _logger)); - _tools.Add(new ResourceTools.Whois(this, _httpClient, _logger)); - _tools.Add(new ResourceTools.ImageColorPicker(this, _httpClient, _logger)); + // Add API endpoints + _apis.Add(new ResourceTools.Completion(this, _httpClient, _logger)); + _apis.Add(new ResourceTools.Settings(this, _httpClient, _logger)); + _apis.Add(new ResourceTools.ChromiumDevTools(this, _httpClient, _logger)); + _apis.Add(new ResourceTools.DnsQuery(this, _httpClient, _logger)); + _apis.Add(new ResourceTools.IpQuery(this, _httpClient, _logger)); + _apis.Add(new ResourceTools.TwoFactorAuth(this, _httpClient, _logger)); + _apis.Add(new ResourceTools.Whois(this, _httpClient, _logger)); + _apis.Add(new ResourceTools.ImageColorPicker(this, _httpClient, _logger)); // Register the prefix _listener.Prefixes.Add(prefix); @@ -152,11 +152,11 @@ namespace WelsonJS.Launcher } // Serve from a resource tool - foreach (var tool in _tools) + foreach (var api in _apis) { - if (tool.CanHandle(path)) + if (api.CanHandle(path)) { - await tool.HandleAsync(context, path); + await api.HandleAsync(context, path); return; } }