DocSpace-buildtools/common/services/ASC.Webhooks.Service/Program.cs

105 lines
3.1 KiB
C#
Raw Normal View History

var options = new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};
var builder = WebApplication.CreateBuilder(options);
builder.Host.UseWindowsService();
builder.Host.UseSystemd();
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.WebHost.ConfigureKestrel((hostingContext, serverOptions) =>
2021-08-26 18:43:41 +00:00
{
var kestrelConfig = hostingContext.Configuration.GetSection("Kestrel");
if (!kestrelConfig.Exists()) return;
var unixSocket = kestrelConfig.GetValue<string>("ListenUnixSocket");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
2021-08-26 18:43:41 +00:00
{
if (!string.IsNullOrWhiteSpace(unixSocket))
{
unixSocket = string.Format(unixSocket, hostingContext.HostingEnvironment.ApplicationName.Replace("ASC.", "").Replace(".", ""));
serverOptions.ListenUnixSocket(unixSocket);
}
}
});
builder.Host.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)
.AddJsonFile("redis.json")
.AddJsonFile($"redis.{env}.json", true)
.AddEnvironmentVariables()
.AddCommandLine(args);
});
2021-08-26 18:43:41 +00:00
builder.WebHost.ConfigureServices((hostContext, services) =>
{
services.AddMemoryCache();
2021-08-26 18:43:41 +00:00
var diHelper = new DIHelper(services);
var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get<RedisConfiguration>();
var kafkaConfiguration = hostContext.Configuration.GetSection("kafka").Get<KafkaSettings>();
2022-01-14 07:42:43 +00:00
if (kafkaConfiguration != null)
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
}
else if (redisConfiguration != null)
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>));
services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConfiguration);
}
else
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>));
}
2021-08-27 14:50:49 +00:00
services.AddHttpClient();
diHelper.TryAdd<DbWorker>();
2021-08-27 15:33:21 +00:00
services.AddHostedService<BuildQueueService>();
diHelper.TryAdd<BuildQueueService>();
2022-02-02 14:23:15 +00:00
services.AddHostedService<WorkerService>();
diHelper.TryAdd<WorkerService>();
});
builder.Host.ConfigureNLogLogging();
var startup = new BaseWorkerStartup(builder.Configuration);
startup.ConfigureServices(builder.Services);
var app = builder.Build();
startup.Configure(app);
2022-02-11 14:33:00 +00:00
app.Run();