1- using System . Diagnostics . CodeAnalysis ;
2- using System . Threading . RateLimiting ;
3-
41namespace WolfApi ;
52
3+ using Microsoft . Extensions . Logging . Abstractions ;
64using Microsoft . Extensions . Configuration ;
75using System . Collections . Concurrent ;
86using NSwagWolfApi ;
97using NSwagDocker ;
108using System . Net . Sockets ;
119using Microsoft . Extensions . Hosting ;
1210using Microsoft . Extensions . Logging ;
11+ using System . Diagnostics . CodeAnalysis ;
12+ using System . Threading . RateLimiting ;
1313
14- public partial class Api : IHostedService , IApiEventPublisher
14+ public partial class Api : IHostedService
1515{
1616 public NSwagDocker DockerApi { get ; }
1717 public NSwagWolfApi WolfApi { get ; }
1818 private readonly ILogger < Api > _logger ;
1919 private readonly HttpClient _httpClient ;
2020
21+ public string WolfSocketPath { get ; set ; } = "unix:///etc/wolf/cfg/wolf.sock" ;
22+ public string BaseAddress { get ; set ; } = "http://localhost/api/v1/" ;
23+
2124 public ConcurrentQueue < Profile > ? Profiles { get ; private set ; }
25+
26+ public Api ( ) : this ( NullLogger < Api > . Instance ) { }
2227
28+ public Api ( ILogger < Api > logger ) :
29+ this ( logger ,
30+ new ConfigurationBuilder ( )
31+ . AddInMemoryCollection ( new Dictionary < string , string >
32+ {
33+ { "SOCKET_PATH" , "/etc/wolf/cfg/wolf.sock" }
34+ } ! ) . Build ( ) )
35+ { }
2336
24- // ReSharper disable once ConvertToPrimaryConstructor
25- public Api ( WolfApiHttpClient httpClient , ILogger < Api > logger )
26- {
27- DockerApi = new ( httpClient ) ;
28- WolfApi = new ( httpClient ) ;
29- _httpClient = httpClient ;
30- _logger = logger ;
31- }
37+ public Api ( IConfiguration config ) : this ( NullLogger < Api > . Instance , config ) { }
3238
3339 public Api ( ILogger < Api > logger , IConfiguration configuration )
3440 {
35- configuration . GetValue < string > ( "SOCKET_PATH" ) ;
41+ var socketPath = configuration . GetValue < string ? > ( "SOCKET_PATH" ) ?? WolfSocketPath ;
42+ BaseAddress = configuration . GetValue < string ? > ( "BASE_ADDRESS" ) ?? BaseAddress ;
3643
3744 var options = new TokenBucketRateLimiterOptions
3845 {
@@ -44,29 +51,41 @@ public Api(ILogger<Api> logger, IConfiguration configuration)
4451 AutoReplenishment = true
4552 } ;
4653
47- _httpClient = new HttpClient (
48- handler : new ClientSideRateLimitedHandler (
49- limiter : new TokenBucketRateLimiter ( options ) ,
50- httpMessageHandler : new SocketsHttpHandler
51- {
52- ConnectCallback = async ( _ , token ) =>
54+ if ( socketPath . StartsWith ( "unix://" ) )
55+ {
56+ socketPath = socketPath [ "unix://" . Length ..] ;
57+ _httpClient = new HttpClient (
58+ handler : new ClientSideRateLimitedHandler (
59+ limiter : new TokenBucketRateLimiter ( options ) ,
60+ httpMessageHandler : new SocketsHttpHandler
5361 {
54- var endpointPath = Environment . GetEnvironmentVariable ( "WOLF_SOCKET_PATH" ) ??
55- "/etc/wolf/cfg/wolf.sock" ;
56-
57- if ( ! Path . Exists ( endpointPath ) )
62+ ConnectCallback = async ( _ , token ) =>
5863 {
59- throw new FileNotFoundException ( endpointPath ) ;
60- }
64+ if ( ! Path . Exists ( socketPath ) )
65+ {
66+ throw new FileNotFoundException ( socketPath ) ;
67+ }
6168
62- var socket = new Socket ( AddressFamily . Unix , SocketType . Stream , ProtocolType . IP ) ;
63- var endpoint = new UnixDomainSocketEndPoint ( endpointPath ) ;
64- await socket . ConnectAsync ( endpoint , token ) ;
65- return new NetworkStream ( socket , ownsSocket : true ) ;
69+ var socket = new Socket ( AddressFamily . Unix , SocketType . Stream , ProtocolType . IP ) ;
70+ var endpoint = new UnixDomainSocketEndPoint ( socketPath ) ;
71+ await socket . ConnectAsync ( endpoint , token ) ;
72+ return new NetworkStream ( socket , ownsSocket : true ) ;
73+ }
6674 }
67- }
68- )
69- ) ;
75+ )
76+ ) ;
77+ }
78+ else
79+ {
80+ _httpClient = new HttpClient (
81+ handler : new ClientSideRateLimitedHandler (
82+ limiter : new TokenBucketRateLimiter ( options ) ,
83+ httpMessageHandler : new HttpClientHandler ( )
84+ )
85+ ) ;
86+ }
87+
88+
7089
7190 DockerApi = new ( _httpClient ) ;
7291 WolfApi = new ( _httpClient ) ;
@@ -82,7 +101,6 @@ public async Task UpdateProfiles()
82101 {
83102 Profiles . Enqueue ( profile ) ;
84103 }
85- //ProfilesUpdatedEvent?.Invoke(Profiles.ToList());
86104 await OnProfilesUpdatedEvent ( profiles ) ;
87105 }
88106
0 commit comments