Ejemplo n.º 1
0
        void BuildSiloHost()
        {
            var instrumentationKey           = _configuration.GetValue <string>("ApplicationInsights:InstrumentationKey");
            var hostname                     = _configuration.GetValue <string>("HOSTNAME");
            var azureStorageConnectionString = _configuration.GetValue <string>("Storage:AzureStorageConnectionString");
            var redisClusteringUrl           = _configuration.GetValue <string>("REDIS_URL");

            var builder = new SiloHostBuilder()
                          .ConfigureLogging(loggingBuilder =>
            {
                if (!string.IsNullOrEmpty(instrumentationKey))
                {
                    loggingBuilder.AddApplicationInsights(instrumentationKey);
                }
                loggingBuilder.AddConsole();
            })
                          // Configure ClusterId and ServiceId
                          .Configure <ClusterOptions>(options =>
            {
                options.ClusterId = "dev";
                options.ServiceId = "TwitchServices";
            })
                          .AddStartupTask <LoadConfigurationStartupTask>()
                          .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(ChannelGrain).Assembly).WithReferences())
                          // Configure connectivity
                          .ConfigureEndpoints(hostname: hostname, siloPort: 11111, gatewayPort: 30000);

            if (!string.IsNullOrEmpty(azureStorageConnectionString))
            {
                builder.AddAzureTableGrainStorage("profileStore", (AzureTableStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "profiles";
                    options.UseJson          = true;
                    options.IndentJson       = false;
                });
                builder.AddAzureTableGrainStorage("channelStore", (AzureTableStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "channels";
                    options.UseJson          = true;
                    options.IndentJson       = false;
                });
                builder.AddAzureTableGrainStorage("botSettingsStore", (AzureTableStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "botsettings";
                    options.UseJson          = true;
                    options.IndentJson       = false;
                });
                builder.AddCustomCategoriesStorage("customCategoriesStore", (CustomCategoriesStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "customcategories";
                });
            }
            else
            {
                builder.AddMemoryGrainStorage("profileStore");
                builder.AddMemoryGrainStorage("channelStore");
                builder.AddMemoryGrainStorage("botSettingsStore");
                builder.AddMemoryGrainStorage("customCategoriesStore");
            }

            if (!string.IsNullOrEmpty(redisClusteringUrl))
            {
                // Use redis clustering when available
                builder.UseRedisClustering(redisClusteringUrl);
            }
            else
            {
                // Use localhost clustering for a single local silo
                builder.UseLocalhostClustering(11111, 30000, null, "TwitchServices", "dev");
            }

            // Temp

            builder.ConfigureServices((context, services) =>
            {
                // Load channels and command configuration from static json file, and inject
                var channelsConfig = new ConfigurationBuilder().AddJsonFile("channels.json").Build();
                IEnumerable <ChannelOptions> channelOptions = new List <ChannelOptions>();
                channelsConfig.GetSection("channels").Bind(channelOptions);
                services.AddTransient <IEnumerable <ChannelOptions> >((_) => channelOptions);

                // Configure services
                services.AddHttpClient();
                services.Configure <TwitchApplicationOptions>(_configuration.GetSection("twitch"));
                services.Configure <TwitchChatClientOptions>(_configuration.GetSection("twitch").GetSection("IrcOptions"));
                services.Configure <AzureGameLocalizationStoreOptions>(_configuration.GetSection("loc:azure"));
                services.AddSingleton <IMessageProcessor, TracingMessageProcessor>();
                services.AddTransient <TwitchChatClient>();
                services.AddTransient <TwitchAPIClient>();
                services.AddTransient <IGDBClient>();
                services.AddSingleton <IMemoryCache, MemoryCache>();
                services.AddSingleton <SteamStoreClient>();
                services.AddSingleton <IAuthenticated>(s =>
                                                       Twitch.Authenticate()
                                                       .FromAppCredentials(
                                                           s.GetService <IOptions <TwitchApplicationOptions> >().Value.ClientId,
                                                           s.GetService <IOptions <TwitchApplicationOptions> >().Value.ClientSecret)
                                                       .Build()
                                                       );
                services.AddTransient <ITwitchCategoryProvider, GrainTwitchCategoryProvider>();
                services.AddSingleton <IGameLocalizationStore, AzureStorageGameLocalizationStore>();
                services.PostConfigure <TwitchChatClientOptions>(options =>
                {
                    var oauth = Twitch.Authenticate()
                                .FromOAuthToken(options.OAuthToken)
                                .Build();
                    var loggerFactory = new LoggerFactory();
                    using (var httpClient = new HttpClient())
                        using (var apiClient = TwitchAPIClient.Create(oauth))
                        {
                            options.TokenInfo = apiClient.ValidateToken().Result;
                        }
                });

                // Configure commands
                services.AddCommand <GameSynopsisCommand>("GameSynopsis");
                services.AddCommand <TracingMessageProcessor>("Logger");
                services.AddCommand <ResponseCommandProcessor>("Response");
            });
            _siloHost = builder.Build();
        }