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.
This commit is contained in:
Namhyeon, Go 2026-01-31 23:08:47 +09:00
parent 55c33d1750
commit bbfc6f2a6d
2 changed files with 56 additions and 22 deletions

View File

@ -1,4 +1,4 @@
// IResourceTool.cs // IApiEndpoint.cs
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2025 Catswords OSS and WelsonJS Contributors // SPDX-FileCopyrightText: 2025 Catswords OSS and WelsonJS Contributors
// https://github.com/gnh1201/welsonjs // https://github.com/gnh1201/welsonjs
@ -9,22 +9,56 @@ using System.Threading.Tasks;
namespace WelsonJS.Launcher namespace WelsonJS.Launcher
{ {
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
public interface IApiEndpoint public interface IApiEndpoint
{ {
/// <summary> /// <summary>
/// Determines whether this tool can handle the specified path. /// Determines whether this endpoint can handle a request
/// based solely on the request path.
/// </summary> /// </summary>
/// <param name="path">The request path to check.</param> /// <remarks>
/// <returns>True if this tool can handle the request; otherwise, false.</returns> /// 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.
/// </remarks>
/// <param name="path">The normalized request path (e.g. "/api/status").</param>
/// <returns>
/// <c>true</c> if this endpoint is responsible for the given path;
/// otherwise, <c>false</c>.
/// </returns>
bool CanHandle(string path); bool CanHandle(string path);
/// <summary> /// <summary>
/// Asynchronously processes the HTTP request for the specified path. /// Determines whether this endpoint can handle the given HTTP request
/// using the full <see cref="HttpListenerContext"/>.
/// </summary> /// </summary>
/// <param name="context">The HTTP listener context containing request and response objects.</param> /// <remarks>
/// <param name="path">The request path to handle.</param> /// This overload allows more advanced routing decisions based on
/// <returns>A task representing the asynchronous operation.</returns> /// HTTP method, headers, query parameters, authentication state,
/// or other contextual information.
/// </remarks>
/// <param name="context">
/// The HTTP listener context containing request and connection details.
/// </param>
/// <returns>
/// <c>true</c> if this endpoint can process the request;
/// otherwise, <c>false</c>.
/// </returns>
bool CanHandle(HttpListenerContext context);
/// <summary>
/// Asynchronously processes the HTTP request handled by this endpoint.
/// </summary>
/// <param name="context">
/// The HTTP listener context containing request and response objects.
/// </param>
/// <param name="path">
/// The request path associated with this handler.
/// </param>
/// <returns>
/// A task that represents the asynchronous handling operation.
/// </returns>
Task HandleAsync(HttpListenerContext context, string path); Task HandleAsync(HttpListenerContext context, string path);
} }
} }

View File

@ -31,7 +31,7 @@ namespace WelsonJS.Launcher
private bool _isRunning; private bool _isRunning;
private string _prefix; private string _prefix;
private string _resourceName; private string _resourceName;
private List<IApiEndpoint> _tools = new List<IApiEndpoint>(); private List<IApiEndpoint> _apis = new List<IApiEndpoint>();
private BlobConfig _blobConfig; private BlobConfig _blobConfig;
private readonly ILog _logger; private readonly ILog _logger;
@ -66,15 +66,15 @@ namespace WelsonJS.Launcher
_logger?.Error($"FetchBlobConfig failed: {t.Exception}"); _logger?.Error($"FetchBlobConfig failed: {t.Exception}");
}, TaskScheduler.Default); }, TaskScheduler.Default);
// Add resource tools // Add API endpoints
_tools.Add(new ResourceTools.Completion(this, _httpClient, _logger)); _apis.Add(new ResourceTools.Completion(this, _httpClient, _logger));
_tools.Add(new ResourceTools.Settings(this, _httpClient, _logger)); _apis.Add(new ResourceTools.Settings(this, _httpClient, _logger));
_tools.Add(new ResourceTools.ChromiumDevTools(this, _httpClient, _logger)); _apis.Add(new ResourceTools.ChromiumDevTools(this, _httpClient, _logger));
_tools.Add(new ResourceTools.DnsQuery(this, _httpClient, _logger)); _apis.Add(new ResourceTools.DnsQuery(this, _httpClient, _logger));
_tools.Add(new ResourceTools.IpQuery(this, _httpClient, _logger)); _apis.Add(new ResourceTools.IpQuery(this, _httpClient, _logger));
_tools.Add(new ResourceTools.TwoFactorAuth(this, _httpClient, _logger)); _apis.Add(new ResourceTools.TwoFactorAuth(this, _httpClient, _logger));
_tools.Add(new ResourceTools.Whois(this, _httpClient, _logger)); _apis.Add(new ResourceTools.Whois(this, _httpClient, _logger));
_tools.Add(new ResourceTools.ImageColorPicker(this, _httpClient, _logger)); _apis.Add(new ResourceTools.ImageColorPicker(this, _httpClient, _logger));
// Register the prefix // Register the prefix
_listener.Prefixes.Add(prefix); _listener.Prefixes.Add(prefix);
@ -152,11 +152,11 @@ namespace WelsonJS.Launcher
} }
// Serve from a resource tool // 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; return;
} }
} }