Skip to content

Commit

Permalink
WebHook支持设置令牌
Browse files Browse the repository at this point in the history
  • Loading branch information
猿人易 committed Feb 12, 2025
1 parent fddb270 commit 7610d11
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
25 changes: 20 additions & 5 deletions Stardust.Server/Services/MonitorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ public void WebHook(AppTracer app, TraceModel model)
actor.Tell(model);
}

class WebHookActor : Actor
public class WebHookActor : Actor
{
public AppTracer App { get; set; }

private ApiHttpClient _client;
private String _server;
private String _action;
private String _token;

private ApiHttpClient GetClient()
public ApiHttpClient GetClient()
{
var addr = App.WebHook;
if (addr.IsNullOrEmpty()) return null;
Expand All @@ -44,13 +45,27 @@ private ApiHttpClient GetClient()
}

if (addr.IsNullOrEmpty()) return null;
_server = addr;

_client = new ApiHttpClient(addr);
var p = addr.IndexOf("#token=", StringComparison.OrdinalIgnoreCase);
if (p > 0)
{
_token = addr[(p + 7)..];
addr = addr[..p];
}

_server = addr;
//p = addr.IndexOf('?');
//if (p > 0)
//{
// _action = addr[(p + 1)..];
// addr = addr[..p];
//}

var uri = new Uri(addr);
_action = uri.AbsolutePath;
_action = uri.PathAndQuery;

_client = new ApiHttpClient($"{uri.Scheme}://{uri.Authority}");
_client.Token = _token;

return _client;
}
Expand Down
28 changes: 28 additions & 0 deletions Stardust.ServerTests/Services/MonitorServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using NewLife.Reflection;
using Stardust.Data.Monitors;
using Xunit;
using static Stardust.Server.Services.MonitorService;

namespace Stardust.ServerTests.Services;

public class MonitorServiceTests
{
[Fact]
public void GetClientTest()
{
var actor = new WebHookActor();
actor.App = new AppTracer { WebHook = "https://newlifex.com/monitor/push?id=1234#token=abcd" };

var uri = new Uri(actor.App.WebHook);
Assert.Equal("newlifex.com", uri.Host);
Assert.Equal("/monitor/push", uri.AbsolutePath);
Assert.Equal("?id=1234", uri.Query);
Assert.Equal("/monitor/push?id=1234", uri.PathAndQuery);

var client = actor.GetClient();
Assert.NotNull(client);
Assert.Equal("/monitor/push?id=1234", actor.GetValue("_action"));
Assert.Equal("abcd", actor.GetValue("_token"));
}
}

0 comments on commit 7610d11

Please sign in to comment.