2021-08-26 18:43:41 +00:00
|
|
|
namespace ASC.Webhooks.Service
|
|
|
|
{
|
2022-01-21 10:47:56 +00:00
|
|
|
public static class Program
|
2021-08-26 18:43:41 +00:00
|
|
|
{
|
2021-08-27 14:50:49 +00:00
|
|
|
public async static Task Main(string[] args)
|
2021-08-26 18:43:41 +00:00
|
|
|
{
|
2021-08-27 14:50:49 +00:00
|
|
|
var host = CreateHostBuilder(args).Build();
|
|
|
|
|
|
|
|
await host.RunAsync();
|
2021-08-26 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
|
|
|
Host.CreateDefaultBuilder(args)
|
|
|
|
.UseSystemd()
|
|
|
|
.UseWindowsService()
|
|
|
|
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
|
|
|
|
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<BaseWorkerStartup>())
|
|
|
|
.ConfigureAppConfiguration((hostContext, config) =>
|
|
|
|
{
|
|
|
|
var buided = config.Build();
|
|
|
|
var path = buided["pathToConf"];
|
|
|
|
if (!Path.IsPathRooted(path))
|
|
|
|
{
|
|
|
|
path = Path.GetFullPath(CrossPlatform.PathCombine(hostContext.HostingEnvironment.ContentRootPath, path));
|
|
|
|
}
|
|
|
|
config.SetBasePath(path);
|
|
|
|
var env = hostContext.Configuration.GetValue("ENVIRONMENT", "Production");
|
|
|
|
config
|
|
|
|
.AddInMemoryCollection(new Dictionary<string, string>
|
|
|
|
{
|
|
|
|
{"pathToConf", path }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.AddJsonFile("appsettings.json")
|
|
|
|
.AddJsonFile($"appsettings.{env}.json", true)
|
|
|
|
.AddJsonFile($"appsettings.services.json", true)
|
|
|
|
.AddJsonFile("storage.json")
|
|
|
|
.AddJsonFile("kafka.json")
|
|
|
|
.AddJsonFile($"kafka.{env}.json", true)
|
2021-12-21 16:16:34 +00:00
|
|
|
.AddJsonFile("redis.json")
|
|
|
|
.AddJsonFile($"redis.{env}.json", true)
|
2021-08-26 18:43:41 +00:00
|
|
|
.AddEnvironmentVariables()
|
|
|
|
.AddCommandLine(args);
|
|
|
|
})
|
|
|
|
.ConfigureServices((hostContext, services) =>
|
|
|
|
{
|
|
|
|
services.AddMemoryCache();
|
|
|
|
|
|
|
|
var diHelper = new DIHelper(services);
|
|
|
|
|
2022-01-13 11:24:14 +00:00
|
|
|
var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get<RedisConfiguration>();
|
2022-01-27 12:20:51 +00:00
|
|
|
var kafkaConfiguration = hostContext.Configuration.GetSection("kafka").Get<KafkaSettings>();
|
2022-01-13 11:24:14 +00:00
|
|
|
|
2022-01-27 12:20:51 +00:00
|
|
|
if (kafkaConfiguration != null)
|
|
|
|
{
|
2022-02-01 14:51:29 +00:00
|
|
|
diHelper.TryAdd(typeof(IEventBus<>), typeof(EventBusKafka<>));
|
2022-01-27 12:20:51 +00:00
|
|
|
}
|
|
|
|
else if (redisConfiguration != null)
|
2022-01-13 11:24:14 +00:00
|
|
|
{
|
2022-02-01 14:51:29 +00:00
|
|
|
diHelper.TryAdd(typeof(IEventBus<>), typeof(EventBusRedis<>));
|
2022-01-14 07:42:43 +00:00
|
|
|
|
|
|
|
services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConfiguration);
|
2022-01-13 11:24:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-01 14:51:29 +00:00
|
|
|
diHelper.TryAdd(typeof(IEventBus<>), typeof(EventBusMemoryCache<>));
|
2022-01-13 11:24:14 +00:00
|
|
|
}
|
2021-08-27 14:50:49 +00:00
|
|
|
|
2021-08-27 15:33:21 +00:00
|
|
|
diHelper.TryAdd<DbWorker>();
|
|
|
|
|
2021-08-26 18:43:41 +00:00
|
|
|
services.AddHostedService<WebhookHostedService>();
|
|
|
|
diHelper.TryAdd<WebhookHostedService>();
|
2021-12-21 16:16:34 +00:00
|
|
|
|
2021-08-26 18:43:41 +00:00
|
|
|
})
|
|
|
|
.ConfigureContainer<ContainerBuilder>((context, builder) =>
|
|
|
|
{
|
2021-08-27 14:50:49 +00:00
|
|
|
builder.Register(context.Configuration, false, false);
|
2021-08-26 18:43:41 +00:00
|
|
|
})
|
|
|
|
.ConfigureNLogLogging();
|
|
|
|
}
|
|
|
|
}
|