Merge pull request #391 from gnh1201/dev
Some checks failed
CodeQL / Analyze (javascript) (push) Has been cancelled
Deploy Jekyll with GitHub Pages dependencies preinstalled / build (push) Has been cancelled
Deploy Jekyll with GitHub Pages dependencies preinstalled / deploy (push) Has been cancelled

Refactor IApiEndpoint and update ResourceServer usage
This commit is contained in:
Namhyeon Go 2026-01-31 23:33:13 +09:00 committed by GitHub
commit d6004ca4fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 82 additions and 48 deletions

View File

@ -11,7 +11,7 @@ using System.Net.Http;
using System.Security;
using System.Threading.Tasks;
namespace WelsonJS.Launcher.ResourceTools
namespace WelsonJS.Launcher.ApiEndpoints
{
public class ChromiumDevTools : IApiEndpoint
{
@ -29,9 +29,9 @@ namespace WelsonJS.Launcher.ResourceTools
_logger = logger;
}
public bool CanHandle(string path)
public bool CanHandle(HttpListenerContext context, string path)
{
return path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
return path != null && path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
}
public async Task HandleAsync(HttpListenerContext context, string path)

View File

@ -16,7 +16,7 @@ using System.Net.Http;
using System.Collections.Concurrent;
using log4net;
namespace WelsonJS.Launcher.ResourceTools
namespace WelsonJS.Launcher.ApiEndpoints
{
public class Completion : IApiEndpoint
{
@ -38,9 +38,9 @@ namespace WelsonJS.Launcher.ResourceTools
Task.Run(async () => await SafeDiscoverAsync(DiscoverFromProgramDirectories));
}
public bool CanHandle(string path)
public bool CanHandle(HttpListenerContext context, string path)
{
return path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
return path != null && path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
}
public async Task HandleAsync(HttpListenerContext context, string path)

View File

@ -13,7 +13,7 @@ using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace WelsonJS.Launcher.ResourceTools
namespace WelsonJS.Launcher.ApiEndpoints
{
public class DnsQuery : IApiEndpoint
{
@ -36,9 +36,9 @@ namespace WelsonJS.Launcher.ResourceTools
DnsServer = Program.GetAppConfig("DnsServerAddress");
}
public bool CanHandle(string path)
public bool CanHandle(HttpListenerContext context, string path)
{
return path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
return path != null && path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
}
public async Task HandleAsync(HttpListenerContext context, string path)

View File

@ -14,7 +14,7 @@ using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace WelsonJS.Launcher.ResourceTools
namespace WelsonJS.Launcher.ApiEndpoints
{
/// <summary>
/// POST image-color-picker/ with a unified JSON body:
@ -45,9 +45,9 @@ namespace WelsonJS.Launcher.ResourceTools
_logger = logger;
}
public bool CanHandle(string path)
public bool CanHandle(HttpListenerContext context, string path)
{
return path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
return path != null && path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
}
public async Task HandleAsync(HttpListenerContext context, string path)

View File

@ -25,7 +25,7 @@ using System.Threading.Tasks;
using System.Web;
using System.Xml.Linq;
namespace WelsonJS.Launcher.ResourceTools
namespace WelsonJS.Launcher.ApiEndpoints
{
public class IpQuery : IApiEndpoint
{
@ -42,9 +42,9 @@ namespace WelsonJS.Launcher.ResourceTools
_logger = logger;
}
public bool CanHandle(string path)
public bool CanHandle(HttpListenerContext context, string path)
{
return path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
return path != null && path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
}
public async Task HandleAsync(HttpListenerContext context, string path)
{

View File

@ -15,7 +15,7 @@ using System.Resources;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace WelsonJS.Launcher.ResourceTools
namespace WelsonJS.Launcher.ApiEndpoints
{
public class Settings : IApiEndpoint
{
@ -32,9 +32,9 @@ namespace WelsonJS.Launcher.ResourceTools
_logger = logger;
}
public bool CanHandle(string path)
public bool CanHandle(HttpListenerContext context, string path)
{
return path.Equals(Prefix, StringComparison.OrdinalIgnoreCase);
return path != null && path.Equals(Prefix, StringComparison.OrdinalIgnoreCase);
}
public async Task HandleAsync(HttpListenerContext context, string path)

View File

@ -14,7 +14,7 @@ using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace WelsonJS.Launcher.ResourceTools
namespace WelsonJS.Launcher.ApiEndpoints
{
public class TwoFactorAuth : IApiEndpoint
{
@ -33,9 +33,9 @@ namespace WelsonJS.Launcher.ResourceTools
_logger = logger;
}
public bool CanHandle(string path)
public bool CanHandle(HttpListenerContext context, string path)
{
return path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
return path != null && path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
}
public async Task HandleAsync(HttpListenerContext context, string path)

View File

@ -10,7 +10,7 @@ using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace WelsonJS.Launcher.ResourceTools
namespace WelsonJS.Launcher.ApiEndpoints
{
public class Whois : IApiEndpoint
{
@ -27,9 +27,9 @@ namespace WelsonJS.Launcher.ResourceTools
_logger = logger;
}
public bool CanHandle(string path)
public bool CanHandle(HttpListenerContext context, string path)
{
return path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
return path != null && path.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
}
public async Task HandleAsync(HttpListenerContext context, string path)

View File

@ -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
{
/// <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>
public interface IApiEndpoint
{
/// <summary>
/// Determines whether this tool can handle the specified path.
/// Determines whether this endpoint is able to handle the given HTTP request.
/// </summary>
/// <param name="path">The request path to check.</param>
/// <returns>True if this tool can handle the request; otherwise, false.</returns>
bool CanHandle(string path);
/// <remarks>
/// This method is invoked by the request dispatcher to decide which endpoint
/// should process the incoming request.
///
/// Implementations may use the provided <paramref name="path"/> for fast
/// routing decisions, while the full <paramref name="context"/> can be
/// inspected for HTTP method, headers, query parameters, authentication
/// state, or other request-specific information.
///
/// This method should be free of side effects and must not write to the response.
/// </remarks>
/// <param name="context">
/// The HTTP listener context containing the incoming request details.
/// </param>
/// <param name="path">
/// The normalized request path extracted from the request URL
/// (e.g. "/api/status").
/// </param>
/// <returns>
/// <c>true</c> if this endpoint is responsible for handling the request;
/// otherwise, <c>false</c>.
/// </returns>
bool CanHandle(HttpListenerContext context, string path);
/// <summary>
/// Asynchronously processes the HTTP request for the specified path.
/// Asynchronously handles the HTTP request assigned to this endpoint.
/// </summary>
/// <param name="context">The HTTP listener context containing request and response objects.</param>
/// <param name="path">The request path to handle.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <remarks>
/// This method is called only after <see cref="CanHandle"/> has returned
/// <c>true</c> for the same request.
///
/// Implementations are responsible for writing the response and properly
/// terminating the request lifecycle.
/// </remarks>
/// <param name="context">
/// The HTTP listener context containing request and response objects.
/// </param>
/// <param name="path">
/// The request path associated with this endpoint.
/// </param>
/// <returns>
/// A task that represents the asynchronous handling operation.
/// </returns>
Task HandleAsync(HttpListenerContext context, string path);
}
}

View File

@ -31,7 +31,7 @@ namespace WelsonJS.Launcher
private bool _isRunning;
private string _prefix;
private string _resourceName;
private List<IApiEndpoint> _tools = new List<IApiEndpoint>();
private readonly List<IApiEndpoint> _apis = new List<IApiEndpoint>();
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 ApiEndpoints.Completion(this, _httpClient, _logger));
_apis.Add(new ApiEndpoints.Settings(this, _httpClient, _logger));
_apis.Add(new ApiEndpoints.ChromiumDevTools(this, _httpClient, _logger));
_apis.Add(new ApiEndpoints.DnsQuery(this, _httpClient, _logger));
_apis.Add(new ApiEndpoints.IpQuery(this, _httpClient, _logger));
_apis.Add(new ApiEndpoints.TwoFactorAuth(this, _httpClient, _logger));
_apis.Add(new ApiEndpoints.Whois(this, _httpClient, _logger));
_apis.Add(new ApiEndpoints.ImageColorPicker(this, _httpClient, _logger));
// Register the prefix
_listener.Prefixes.Add(prefix);
@ -151,12 +151,12 @@ namespace WelsonJS.Launcher
return;
}
// Serve from a resource tool
foreach (var tool in _tools)
// Serve via API endpoints
foreach (var api in _apis)
{
if (tool.CanHandle(path))
if (api.CanHandle(context, path))
{
await tool.HandleAsync(context, path);
await api.HandleAsync(context, path);
return;
}
}