-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
93 lines (79 loc) · 3.68 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using GooglePhotoSync.Google;
using GooglePhotoSync.Google.Api;
using GooglePhotoSync.Local;
using GooglePhotoSync.Sync;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Refit;
using System;
using System.Threading.Tasks;
namespace GooglePhotoSync
{
// ReSharper disable once ClassNeverInstantiated.Global
internal class Program
{
public static async Task Main(string[] args)
{
var configuration = LoadConfiguration();
var services = new ServiceCollection()
.AddLogging(builder =>
{
builder.AddConfiguration(configuration.GetSection("Logging"));
builder.AddConsole();
})
//.AddSingleton<GoogleSettings>()
.AddTransient<SyncPhotos>()
.AddSingleton<GoogleLogin>()
.AddTransient<AuthenticatedHttpClientHandler>()
.AddSingleton<GoogleBearerTokenRetriever>()
.AddTransient<GoogleSource>()
.ConfigureSettings<LocalSettings>(configuration)
.AddTransient<LocalSource>()
.AddTransient<CollectionComparer>()
.AddTransient<CollectionSync>()
.AddTransient<AlbumSync>()
.ConfigureSettings<SyncSettings>(configuration)
//.AddSingleton<Func<AlbumSync>>(sp => sp.GetService<AlbumSync>)
;
var googleSettings = services.ConfigureAndGet<GoogleSettings>(configuration);
services.AddRefitClient<IGooglePhotosApi>()
.ConfigureHttpClient((_, c) =>
{
c.BaseAddress = new Uri(googleSettings.GooglePhotosApiBaseUrl);
c.Timeout = TimeSpan.FromMinutes(3);
})
.AddHttpMessageHandler<AuthenticatedHttpClientHandler>();
services.AddRefitClient<IAuthToken>()
.ConfigureHttpClient((_, c) => c.BaseAddress = new Uri(googleSettings.GoogleTokenUrl));
var sync = services.BuildServiceProvider().GetRequiredService<SyncPhotos>();
await sync.Sync();
}
private static IConfiguration LoadConfiguration()
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
#if DEBUG
builder.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true);
#else
builder.AddJsonFile("appsettings.Production.json", optional: true, reloadOnChange: true);
#endif
return builder.Build();
}
}
public static class ServiceCollectionExtensions
{
public static IServiceCollection ConfigureSettings<T>(this IServiceCollection services, IConfiguration configuration) where T : class
{
var settings = configuration.GetRequiredSection(typeof(T).Name);
services.Configure<T>(settings);
return services;
}
public static T ConfigureAndGet<T>(this IServiceCollection services, IConfiguration configuration) where T : class
{
var settings = configuration.GetRequiredSection(typeof(T).Name);
services.Configure<T>(settings);
return settings.Get<T>();
}
}
}