-
Notifications
You must be signed in to change notification settings - Fork 3
/
Bootstrapper.cs
72 lines (60 loc) · 2.57 KB
/
Bootstrapper.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
using Fusonic.GitBackup.Models;
using Fusonic.GitBackup.Services;
using Fusonic.GitBackup.Services.Api;
using Fusonic.GitBackup.Services.Git;
using Fusonic.GitBackup.Services.Heartbeat;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using RestEase;
using SimpleInjector;
using SimpleInjector.Lifestyles;
namespace Fusonic.GitBackup;
internal static class Bootstrapper
{
internal static Container CreateContainer()
{
var container = new Container()
{
Options =
{
DefaultScopedLifestyle = new AsyncScopedLifestyle(),
DefaultLifestyle = Lifestyle.Scoped,
}
};
container.Collection.Register<IGitService>(new[]
{
typeof(BitbucketService),
typeof(GitlabService),
typeof(GithubService)
});
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(builder => builder.AddConsole());
serviceCollection.AddSimpleInjector(container, action => action.AddLogging());
var serviceProvider = serviceCollection.BuildServiceProvider();
serviceProvider.UseSimpleInjector(container);
var builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("app-settings.json", false)
.AddJsonFile("app-settings.overwrite.json", true)
.AddUserSecrets<Program>();
var configuration = builder.Build();
var settings = new AppSettings();
configuration.Bind(settings);
container.RegisterInstance(settings);
container.Register(() => () => RestClient.For<IGitlabApi>("https://gitlab.com/api/v4"));
container.Register(() => () => RestClient.For<IGithubApi>("https://api.github.com"));
container.Register(() => () => RestClient.For<IBitbucketApi>("https://api.bitbucket.org/2.0"));
container.Register(typeof(IHeartbeat), () =>
!string.IsNullOrEmpty(settings.DeadmanssnitchUrl)
? new DeadmansSnitchBeat(settings.DeadmanssnitchUrl, container.GetInstance<ILogger>())
: (IHeartbeat)new NullBeat());
container.Register(typeof(IBackupStrategy),
settings.Backup.Local.Strategy == AppSettings.LocalSettings.BackupStrategy.Full
? typeof(FullBackupStrategy)
: typeof(IncrementalBackupStrategy));
container.Register<App>();
container.Verify();
return container;
}
}