diff --git a/src/SpamGuard/Program.cs b/src/SpamGuard/Program.cs index 3751555..dd9eced 100644 --- a/src/SpamGuard/Program.cs +++ b/src/SpamGuard/Program.cs @@ -1,2 +1,78 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +// src/SpamGuard/Program.cs +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Serilog; +using SpamGuard.Configuration; +using SpamGuard.Services; +using SpamGuard.State; +using SpamGuard.Tray; + +namespace SpamGuard; + +static class Program +{ + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.SetHighDpiMode(HighDpiMode.SystemAware); + + var dataDir = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + "SpamGuard"); + Directory.CreateDirectory(dataDir); + + Log.Logger = new LoggerConfiguration() + .WriteTo.File( + Path.Combine(dataDir, "logs", "spamguard-.log"), + rollingInterval: RollingInterval.Day, + retainedFileCountLimit: 7) + .CreateLogger(); + + try + { + var host = Host.CreateDefaultBuilder() + .UseSerilog() + .ConfigureAppConfiguration((context, config) => + { + config.AddJsonFile("appsettings.json", optional: false); + config.AddEnvironmentVariables("SPAMGUARD_"); + }) + .ConfigureServices((context, services) => + { + services.Configure( + context.Configuration.GetSection(SpamGuardOptions.SectionName)); + + // State stores + services.AddSingleton(new ProcessedUidStore(dataDir)); + services.AddSingleton(new TrustedSenderStore(dataDir)); + + // Services + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddHttpClient(); + + // Background services + services.AddSingleton(); + services.AddHostedService(sp => sp.GetRequiredService()); + services.AddHostedService(); + }) + .Build(); + + host.Start(); + + Application.Run(new TrayApplicationContext(host)); + } + catch (Exception ex) + { + Log.Fatal(ex, "Application failed to start"); + } + finally + { + Log.CloseAndFlush(); + } + } +} diff --git a/src/SpamGuard/SpamGuard.csproj b/src/SpamGuard/SpamGuard.csproj index e221b19..e4797c3 100644 --- a/src/SpamGuard/SpamGuard.csproj +++ b/src/SpamGuard/SpamGuard.csproj @@ -12,6 +12,9 @@ + + +