mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
wip clean architecture, rm autofac, etc.
This commit is contained in:
parent
50abb3ea05
commit
cd5ceb668d
14 changed files with 97 additions and 62 deletions
|
|
@ -1,9 +1,9 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.Infrastructure;
|
||||
using FilterLists.Agent.Core.Interfaces;
|
||||
using MediatR;
|
||||
|
||||
namespace FilterLists.Agent.ListArchiver
|
||||
namespace FilterLists.Agent.Application.Archiver
|
||||
{
|
||||
public static class CaptureLists
|
||||
{
|
||||
|
|
@ -13,18 +13,18 @@ public class Command : IRequest
|
|||
|
||||
public class Handler : AsyncRequestHandler<Command>
|
||||
{
|
||||
private readonly IFilterListsApiClient _apiClient;
|
||||
private readonly IMediator _mediator;
|
||||
private readonly IListInfoRepository _repo;
|
||||
|
||||
public Handler(IFilterListsApiClient apiClient, IMediator mediator)
|
||||
public Handler(IMediator mediator, IListInfoRepository listInfoRepository)
|
||||
{
|
||||
_apiClient = apiClient;
|
||||
_repo = listInfoRepository;
|
||||
_mediator = mediator;
|
||||
}
|
||||
|
||||
protected override async Task Handle(Command request, CancellationToken cancellationToken)
|
||||
{
|
||||
var lists = await _apiClient.GetListInfo();
|
||||
var lists = await _repo.GetAll();
|
||||
await _mediator.Send(new DownloadLists.Command(lists), cancellationToken);
|
||||
await _mediator.Send(new CommitDownloadedLists.Command(), cancellationToken);
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
using LibGit2Sharp;
|
||||
using MediatR;
|
||||
|
||||
namespace FilterLists.Agent.ListArchiver
|
||||
namespace FilterLists.Agent.Application.Archiver
|
||||
{
|
||||
public static class CommitDownloadedLists
|
||||
{
|
||||
|
|
@ -4,13 +4,13 @@
|
|||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.Entities;
|
||||
using FilterLists.Agent.Infrastructure;
|
||||
using FilterLists.Agent.Core.Entities;
|
||||
using FilterLists.Agent.Infrastructure.Clients;
|
||||
using MediatR;
|
||||
|
||||
//TODO: upsert into MariaDB Rules table https://stackoverflow.com/questions/15271202/mysql-load-data-infile-with-on-duplicate-key-update
|
||||
|
||||
namespace FilterLists.Agent.ListArchiver
|
||||
namespace FilterLists.Agent.Application.Archiver
|
||||
{
|
||||
public static class DownloadList
|
||||
{
|
||||
|
|
@ -3,10 +3,10 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks.Dataflow;
|
||||
using FilterLists.Agent.Entities;
|
||||
using FilterLists.Agent.Core.Entities;
|
||||
using MediatR;
|
||||
|
||||
namespace FilterLists.Agent.ListArchiver
|
||||
namespace FilterLists.Agent.Application.Archiver
|
||||
{
|
||||
public static class DownloadLists
|
||||
{
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
namespace FilterLists.Agent.ListArchiver
|
||||
namespace FilterLists.Agent.Application.Archiver
|
||||
{
|
||||
public enum FileType
|
||||
{
|
||||
|
|
@ -5,7 +5,7 @@
|
|||
using SharpCompress.Archives.SevenZip;
|
||||
using SharpCompress.Readers;
|
||||
|
||||
namespace FilterLists.Agent.ListArchiver
|
||||
namespace FilterLists.Agent.Application.Archiver
|
||||
{
|
||||
public static class StreamExtensions
|
||||
{
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
using System;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace FilterLists.Agent.Entities
|
||||
namespace FilterLists.Agent.Core.Entities
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class ListInfo
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
11
src/FilterLists.Agent/Core/Interfaces/IListInfoRepository.cs
Normal file
11
src/FilterLists.Agent/Core/Interfaces/IListInfoRepository.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.Core.Entities;
|
||||
|
||||
namespace FilterLists.Agent.Core.Interfaces
|
||||
{
|
||||
public interface IListInfoRepository
|
||||
{
|
||||
Task<IEnumerable<ListInfo>> GetAll();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
using FilterLists.Agent.Infrastructure.Clients;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FilterLists.Agent.Extensions
|
||||
{
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static void RegisterAgentServices(this IServiceCollection serviceCollection)
|
||||
{
|
||||
var configuration = GetConfiguration();
|
||||
serviceCollection.AddLogging(b =>
|
||||
{
|
||||
b.AddConsole();
|
||||
b.AddApplicationInsights(configuration["ApplicationInsights:InstrumentationKey"] ?? "");
|
||||
});
|
||||
serviceCollection.AddMediatR(typeof(Program).Assembly);
|
||||
serviceCollection.AddHttpClient<AgentHttpClient>();
|
||||
serviceCollection.AddSingleton<IFilterListsApiClient, FilterListsApiClient>();
|
||||
}
|
||||
|
||||
private static IConfigurationRoot GetConfiguration()
|
||||
{
|
||||
return new ConfigurationBuilder()
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -27,8 +27,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac" Version="4.9.2" />
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.4.0" />
|
||||
<PackageReference Include="JetBrains.Annotations" Version="2019.1.1" />
|
||||
<PackageReference Include="LibGit2Sharp" Version="0.26.0" />
|
||||
<PackageReference Include="MediatR" Version="7.0.0" />
|
||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace FilterLists.Agent.Infrastructure
|
||||
namespace FilterLists.Agent.Infrastructure.Clients
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class AgentHttpClient
|
||||
{
|
||||
public AgentHttpClient(HttpClient client)
|
||||
|
|
@ -1,14 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.Entities;
|
||||
using RestSharp;
|
||||
|
||||
namespace FilterLists.Agent.Infrastructure
|
||||
namespace FilterLists.Agent.Infrastructure.Clients
|
||||
{
|
||||
public interface IFilterListsApiClient
|
||||
{
|
||||
Task<IEnumerable<ListInfo>> GetListInfo();
|
||||
Task<TResponse> ExecuteAsync<TResponse>(IRestRequest request);
|
||||
}
|
||||
|
||||
public class FilterListsApiClient : IFilterListsApiClient
|
||||
|
|
@ -22,13 +20,7 @@ public FilterListsApiClient()
|
|||
_restClient = new RestClient(FilterListsApiBaseUrl) {UserAgent = "FilterLists.Agent"};
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ListInfo>> GetListInfo()
|
||||
{
|
||||
var listsRequest = new RestRequest("lists");
|
||||
return await ExecuteAsync<IEnumerable<ListInfo>>(listsRequest);
|
||||
}
|
||||
|
||||
private async Task<TResponse> ExecuteAsync<TResponse>(IRestRequest request)
|
||||
public async Task<TResponse> ExecuteAsync<TResponse>(IRestRequest request)
|
||||
{
|
||||
var response = await _restClient.ExecuteTaskAsync<TResponse>(request);
|
||||
if (response.ErrorException == null)
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Agent.Core.Entities;
|
||||
using FilterLists.Agent.Core.Interfaces;
|
||||
using FilterLists.Agent.Infrastructure.Clients;
|
||||
using RestSharp;
|
||||
|
||||
namespace FilterLists.Agent.Infrastructure.Repositories
|
||||
{
|
||||
public class ListInfoRepository : IListInfoRepository
|
||||
{
|
||||
private readonly IFilterListsApiClient _apiClient;
|
||||
|
||||
public ListInfoRepository(IFilterListsApiClient apiClient)
|
||||
{
|
||||
_apiClient = apiClient;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ListInfo>> GetAll()
|
||||
{
|
||||
var listsRequest = new RestRequest("lists");
|
||||
return await _apiClient.ExecuteAsync<IEnumerable<ListInfo>>(listsRequest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,9 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Autofac;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using FilterLists.Agent.Infrastructure;
|
||||
using FilterLists.Agent.ListArchiver;
|
||||
using FilterLists.Agent.Application.Archiver;
|
||||
using FilterLists.Agent.Extensions;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FilterLists.Agent
|
||||
{
|
||||
|
|
@ -17,40 +13,17 @@ public static class Program
|
|||
|
||||
public static async Task Main()
|
||||
{
|
||||
RegisterServices();
|
||||
BuildServiceProvider();
|
||||
|
||||
var mediator = _serviceProvider.GetService<IMediator>();
|
||||
await mediator.Send(new CaptureLists.Command());
|
||||
|
||||
((IDisposable) _serviceProvider).Dispose();
|
||||
}
|
||||
|
||||
private static void RegisterServices()
|
||||
private static void BuildServiceProvider()
|
||||
{
|
||||
var configuration = GetConfiguration();
|
||||
var serviceCollection = new ServiceCollection();
|
||||
|
||||
// register Agent services
|
||||
serviceCollection.AddLogging(b =>
|
||||
{
|
||||
b.AddConsole();
|
||||
b.AddApplicationInsights(configuration["ApplicationInsights:InstrumentationKey"] ?? "");
|
||||
});
|
||||
serviceCollection.AddMediatR(typeof(Program).Assembly);
|
||||
serviceCollection.AddHttpClient<AgentHttpClient>();
|
||||
serviceCollection.AddSingleton<IFilterListsApiClient, FilterListsApiClient>();
|
||||
|
||||
var containerBuilder = new ContainerBuilder();
|
||||
containerBuilder.Populate(serviceCollection);
|
||||
var container = containerBuilder.Build();
|
||||
_serviceProvider = new AutofacServiceProvider(container);
|
||||
}
|
||||
|
||||
private static IConfigurationRoot GetConfiguration()
|
||||
{
|
||||
return new ConfigurationBuilder()
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
serviceCollection.RegisterAgentServices();
|
||||
_serviceProvider = serviceCollection.BuildServiceProvider();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue