using Microsoft.Extensions.Options; using SQLInsight.Agent; using SQLInsight.Agent.Monitoring; using SQLInsight.Agent.Updates; using SQLInsight.Connectivity; using SQLInsight.Notifications; using SQLInsight.Repository; using SQLInsight.Rules; using SQLInsight.Rules.Engine; if (UpdateCommands.Handles(args)) { return await UpdateCommands.RunAsync(args); } var builder = Host.CreateApplicationBuilder(args); builder.Services.AddWindowsService(o => o.ServiceName = "SQLInsight Agent"); builder.Services.AddSystemd(); builder.Services.Configure(builder.Configuration.GetSection(AgentOptions.SectionName)); builder.Services.AddSingleton(TimeProvider.System); builder.Services.AddSingleton(); builder.Services.Configure(builder.Configuration.GetSection(UpdateOptions.SectionName)); builder.Services.AddSingleton(sp => UpdateService.LoadKnowledge( sp.GetRequiredService>().Value, sp.GetRequiredService().CreateLogger())); builder.Services.AddSingleton(sp => new RuleEngine(BuiltInRules.Create(sp.GetRequiredService()))); builder.Services.AddSingleton(sp => { var connectionString = sp.GetRequiredService>().Value.Repository.ConnectionString; if (string.IsNullOrWhiteSpace(connectionString)) { throw new InvalidOperationException( "SQLInsight:Repository:ConnectionString mangler. Angiv forbindelsen til repository-databasen (SQL Server 2016+, ikke en af de overvÄgede instanser)."); } return new InsightRepository(connectionString); }); builder.Services.Configure(builder.Configuration.GetSection(NotificationOptions.SectionName)); builder.Services.AddHttpClient("notifications", c => c.Timeout = TimeSpan.FromSeconds(15)); builder.Services.AddSingleton(sp => { var options = sp.GetRequiredService>().Value; var http = sp.GetRequiredService(); var channels = new List(); if (options.Email.Enabled) { channels.Add(new EmailChannel(options.Email)); } if (options.Teams.Enabled) { channels.Add(new TeamsChannel(options.Teams, http.CreateClient("notifications"))); } if (options.Webhook.Enabled) { channels.Add(new WebhookChannel(options.Webhook, http.CreateClient("notifications"))); } return new NotificationDispatcher(channels, options, sp.GetRequiredService(), sp.GetRequiredService>()); }); builder.Services.Configure(builder.Configuration.GetSection(SQLInsight.Agent.Hub.HubOptions.SectionName)); builder.Services.AddHttpClient("hub", c => c.Timeout = TimeSpan.FromSeconds(30)); builder.Services.AddSingleton(); builder.Services.AddHostedService(); builder.Services.AddHttpClient("updates", c => c.Timeout = TimeSpan.FromMinutes(10)); builder.Services.AddHostedService(); builder.Services.AddHostedService(); var host = builder.Build(); await host.RunAsync(); return 0;