mirror of
https://github.com/gnh1201/welsonjs.git
synced 2026-04-18 18:18:42 +00:00
Merge pull request #391 from gnh1201/dev
Refactor IApiEndpoint and update ResourceServer usage
This commit is contained in:
commit
d6004ca4fd
|
|
@ -11,7 +11,7 @@ using System.Net.Http;
|
||||||
using System.Security;
|
using System.Security;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace WelsonJS.Launcher.ResourceTools
|
namespace WelsonJS.Launcher.ApiEndpoints
|
||||||
{
|
{
|
||||||
public class ChromiumDevTools : IApiEndpoint
|
public class ChromiumDevTools : IApiEndpoint
|
||||||
{
|
{
|
||||||
|
|
@ -29,9 +29,9 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
_logger = logger;
|
_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)
|
public async Task HandleAsync(HttpListenerContext context, string path)
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ using System.Net.Http;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using log4net;
|
using log4net;
|
||||||
|
|
||||||
namespace WelsonJS.Launcher.ResourceTools
|
namespace WelsonJS.Launcher.ApiEndpoints
|
||||||
{
|
{
|
||||||
public class Completion : IApiEndpoint
|
public class Completion : IApiEndpoint
|
||||||
{
|
{
|
||||||
|
|
@ -38,9 +38,9 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
Task.Run(async () => await SafeDiscoverAsync(DiscoverFromProgramDirectories));
|
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)
|
public async Task HandleAsync(HttpListenerContext context, string path)
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace WelsonJS.Launcher.ResourceTools
|
namespace WelsonJS.Launcher.ApiEndpoints
|
||||||
{
|
{
|
||||||
public class DnsQuery : IApiEndpoint
|
public class DnsQuery : IApiEndpoint
|
||||||
{
|
{
|
||||||
|
|
@ -36,9 +36,9 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
DnsServer = Program.GetAppConfig("DnsServerAddress");
|
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)
|
public async Task HandleAsync(HttpListenerContext context, string path)
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace WelsonJS.Launcher.ResourceTools
|
namespace WelsonJS.Launcher.ApiEndpoints
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// POST image-color-picker/ with a unified JSON body:
|
/// POST image-color-picker/ with a unified JSON body:
|
||||||
|
|
@ -45,9 +45,9 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
_logger = logger;
|
_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)
|
public async Task HandleAsync(HttpListenerContext context, string path)
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ using System.Threading.Tasks;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
|
|
||||||
namespace WelsonJS.Launcher.ResourceTools
|
namespace WelsonJS.Launcher.ApiEndpoints
|
||||||
{
|
{
|
||||||
public class IpQuery : IApiEndpoint
|
public class IpQuery : IApiEndpoint
|
||||||
{
|
{
|
||||||
|
|
@ -42,9 +42,9 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
_logger = logger;
|
_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)
|
public async Task HandleAsync(HttpListenerContext context, string path)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ using System.Resources;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
|
|
||||||
namespace WelsonJS.Launcher.ResourceTools
|
namespace WelsonJS.Launcher.ApiEndpoints
|
||||||
{
|
{
|
||||||
public class Settings : IApiEndpoint
|
public class Settings : IApiEndpoint
|
||||||
{
|
{
|
||||||
|
|
@ -32,9 +32,9 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
_logger = logger;
|
_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)
|
public async Task HandleAsync(HttpListenerContext context, string path)
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace WelsonJS.Launcher.ResourceTools
|
namespace WelsonJS.Launcher.ApiEndpoints
|
||||||
{
|
{
|
||||||
public class TwoFactorAuth : IApiEndpoint
|
public class TwoFactorAuth : IApiEndpoint
|
||||||
{
|
{
|
||||||
|
|
@ -33,9 +33,9 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
_logger = logger;
|
_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)
|
public async Task HandleAsync(HttpListenerContext context, string path)
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace WelsonJS.Launcher.ResourceTools
|
namespace WelsonJS.Launcher.ApiEndpoints
|
||||||
{
|
{
|
||||||
public class Whois : IApiEndpoint
|
public class Whois : IApiEndpoint
|
||||||
{
|
{
|
||||||
|
|
@ -27,9 +27,9 @@ namespace WelsonJS.Launcher.ResourceTools
|
||||||
_logger = logger;
|
_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)
|
public async Task HandleAsync(HttpListenerContext context, string path)
|
||||||
|
|
|
||||||
|
|
@ -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 is able to handle the given HTTP request.
|
||||||
/// </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 invoked by the request dispatcher to decide which endpoint
|
||||||
bool CanHandle(string path);
|
/// 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>
|
/// <summary>
|
||||||
/// Asynchronously processes the HTTP request for the specified path.
|
/// Asynchronously handles the HTTP request assigned to this endpoint.
|
||||||
/// </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 method is called only after <see cref="CanHandle"/> has returned
|
||||||
/// <returns>A task representing the asynchronous operation.</returns>
|
/// <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);
|
Task HandleAsync(HttpListenerContext context, string path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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 readonly 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 ApiEndpoints.Completion(this, _httpClient, _logger));
|
||||||
_tools.Add(new ResourceTools.Settings(this, _httpClient, _logger));
|
_apis.Add(new ApiEndpoints.Settings(this, _httpClient, _logger));
|
||||||
_tools.Add(new ResourceTools.ChromiumDevTools(this, _httpClient, _logger));
|
_apis.Add(new ApiEndpoints.ChromiumDevTools(this, _httpClient, _logger));
|
||||||
_tools.Add(new ResourceTools.DnsQuery(this, _httpClient, _logger));
|
_apis.Add(new ApiEndpoints.DnsQuery(this, _httpClient, _logger));
|
||||||
_tools.Add(new ResourceTools.IpQuery(this, _httpClient, _logger));
|
_apis.Add(new ApiEndpoints.IpQuery(this, _httpClient, _logger));
|
||||||
_tools.Add(new ResourceTools.TwoFactorAuth(this, _httpClient, _logger));
|
_apis.Add(new ApiEndpoints.TwoFactorAuth(this, _httpClient, _logger));
|
||||||
_tools.Add(new ResourceTools.Whois(this, _httpClient, _logger));
|
_apis.Add(new ApiEndpoints.Whois(this, _httpClient, _logger));
|
||||||
_tools.Add(new ResourceTools.ImageColorPicker(this, _httpClient, _logger));
|
_apis.Add(new ApiEndpoints.ImageColorPicker(this, _httpClient, _logger));
|
||||||
|
|
||||||
// Register the prefix
|
// Register the prefix
|
||||||
_listener.Prefixes.Add(prefix);
|
_listener.Prefixes.Add(prefix);
|
||||||
|
|
@ -151,12 +151,12 @@ namespace WelsonJS.Launcher
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve from a resource tool
|
// Serve via API endpoints
|
||||||
foreach (var tool in _tools)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user