1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
+ using System . Linq ;
5
+ using System . Threading . Tasks ;
4
6
using EventStore . ClientAPI ;
5
7
using EventStore . ClientAPI . SystemData ;
6
8
using Linker ;
@@ -11,37 +13,51 @@ namespace LinkerConsoleApp
11
13
{
12
14
class Program
13
15
{
14
- private static Logger Log = LogManager . GetCurrentClassLogger ( ) ;
16
+ private static readonly Logger Log = LogManager . GetCurrentClassLogger ( ) ;
15
17
static void Main ( string [ ] args )
16
18
{
17
19
Log . Info ( "Building services..." ) ;
20
+ var config = BuildConfig ( ) ;
21
+ var links = config . GetSection ( "links" ) . Get < IEnumerable < Link > > ( ) ;
22
+ var services = new List < LinkerService > ( ) ;
23
+ foreach ( var link in links )
24
+ {
25
+ var filters = link . Filters . Select ( linkFilter => new Filter
26
+ {
27
+ FilterOperation = linkFilter . FilterOperation , FilterType = linkFilter . FilterType ,
28
+ Value = linkFilter . Value
29
+ } ) . ToList ( ) ;
30
+ var filterService = new FilterService ( filters ) ;
31
+ var service = new LinkerService ( new LinkerConnectionBuilder ( new Uri ( link . Origin . ConnectionString ) ,
32
+ ConnectionSettings . Create ( ) . SetDefaultUserCredentials ( new UserCredentials ( link . Origin . User , link . Origin . Pass ) ) ,
33
+ link . Origin . ConnectionName ) , new LinkerConnectionBuilder ( new Uri ( link . Destination . ConnectionString ) ,
34
+ ConnectionSettings . Create ( ) . SetDefaultUserCredentials ( new UserCredentials ( link . Destination . User , link . Destination . Pass ) ) ,
35
+ link . Destination . ConnectionName ) , filterService , Settings . Default ( ) , new NLogger ( ) ) ;
36
+ services . Add ( service ) ;
37
+ }
38
+ StartServices ( services ) ;
39
+ Log . Info ( "Press enter to exit the program" ) ;
40
+ Console . ReadLine ( ) ;
41
+ }
42
+
43
+ private static async Task StartServices ( IEnumerable < LinkerService > services )
44
+ {
45
+ foreach ( var linkerService in services )
46
+ {
47
+ Log . Info ( $ "Starting { linkerService . Name } ") ;
48
+ await linkerService . Start ( ) ;
49
+ }
50
+ }
18
51
19
- var env = Environment . GetEnvironmentVariable ( "CORE_ENVIRONMENT" ) ;
52
+ private static IConfigurationRoot BuildConfig ( )
53
+ {
54
+ var env = Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ;
20
55
var builder = new ConfigurationBuilder ( )
21
56
. SetBasePath ( Directory . GetCurrentDirectory ( ) )
22
57
. AddJsonFile ( "appsettings.json" , optional : true , reloadOnChange : false )
23
58
. AddJsonFile ( $ "appsettings.{ env } .json", optional : true , reloadOnChange : false )
24
59
. AddEnvironmentVariables ( ) ;
25
- var config = builder . Build ( ) ;
26
-
27
- var origin = config . GetSection ( "origin" ) . Get < Origin > ( ) ;
28
- var destination = config . GetSection ( "destination" ) . Get < Destination > ( ) ;
29
- var filterService = new FilterService ( new List < Filter >
30
- {
31
- new Filter { FilterOperation = FilterOperation . Exclude , FilterType = FilterType . Stream , Value = "diary-*" } ,
32
- new Filter { FilterOperation = FilterOperation . Include , FilterType = FilterType . Stream , Value = "*" }
33
- } ) ;
34
-
35
- var service = new LinkerService ( new LinkerConnectionBuilder ( new Uri ( origin . ConnectionString ) ,
36
- ConnectionSettings . Create ( ) . SetDefaultUserCredentials ( new UserCredentials ( origin . User , origin . Pass ) ) ,
37
- origin . ConnectionName ) , new LinkerConnectionBuilder ( new Uri ( destination . ConnectionString ) ,
38
- ConnectionSettings . Create ( ) . SetDefaultUserCredentials ( new UserCredentials ( destination . User , destination . Pass ) ) ,
39
- destination . ConnectionName ) , filterService , Settings . Default ( ) , new NLogger ( ) ) ;
40
- service . Start ( ) . Wait ( ) ;
41
-
42
- Log . Info ( "Replica Service started" ) ;
43
- Log . Info ( "Press enter to exit the program" ) ;
44
- Console . ReadLine ( ) ;
60
+ return builder . Build ( ) ;
45
61
}
46
62
}
47
63
}
0 commit comments