patbef-ServiceOutside/ServiceOutside/Program.cs

152 lines
6.5 KiB
C#
Raw Normal View History

2024-01-29 16:27:34 +01:00
using AspNetCoreRateLimit;
using ServiceOutside.Controllers;
using ServiceShared.Database;
using ServiceShared.Database.Migrations;
using ServiceOutside.Filter;
using ServiceOutside.Service;
using ServiceShared;
using ServiceShared.Crypto;
using ServiceOutside.Models;
using System.Reflection;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
IConfigurationSection logSection = builder.Configuration.GetSection("Log");
IConfigurationSection PushNotifications = builder.Configuration.GetSection("PushNotifications");
if (logSection == null)
{
Log.Error(new Exception("Log configuration was not found in appsettings.json"), "OutsideService", "Programm");
}
else if(PushNotifications == null)
{
Log.Error(new Exception("PushNotifications configuration was not found in appsettings.json"), "OutsideService", "Programm");
}
else
{
string type = logSection.GetValue<string>("Type");
string logPath = logSection.GetValue<string>("path");
string SmtpServer = logSection.GetValue<string>("SmtpServer");
int SmptPort = logSection.GetValue<int>("SmtpPort");
bool SmtpSSL = logSection.GetValue<bool>("SmtpSSL");
string SmtpUsername = logSection.GetValue<string>("SmtpUsername");
string SmtpPassword = logSection.GetValue<string>("SmtpPassword");
string SmtpSenderEmail = logSection.GetValue<string>("SmtpSenderEmail");
string SmtpSenderName = logSection.GetValue<string>("SmtpSenderName");
string LogsRecipients = logSection.GetValue<string>("LogsRecipients");
string CustomerRequestsRecipients = logSection.GetValue<string>("CustomerRequestsRecipients");
Log.Types logType = Log.Types.INFO;
Enum.TryParse(type, out logType);
Log.SetLogPath(logPath);
Log.SetLog(logType);
/** check if smtp username string was stored as encrypted string in config **/
if (!string.IsNullOrEmpty(SmtpUsername))
{
string decryptedSmtpUsername = AES.Decrypt(SmtpUsername);
if (!string.IsNullOrEmpty(decryptedSmtpUsername))
{
SmtpUsername = decryptedSmtpUsername;
}
}
/** check if smtp password string was stored as encrypted string in config **/
if (!string.IsNullOrEmpty(SmtpPassword))
{
string decryptedSmtpPassword = AES.Decrypt(SmtpPassword);
if (!string.IsNullOrEmpty(decryptedSmtpPassword))
{
SmtpPassword = decryptedSmtpPassword;
}
}
Mail.SetSMTPData(SmtpServer, SmptPort, SmtpSSL, SmtpSenderEmail, SmtpSenderName, LogsRecipients, CustomerRequestsRecipients, SmtpUsername, SmtpPassword);
string defaultConnection = builder.Configuration.GetConnectionString("default");
string serviceInsideIP = builder.Configuration.GetSection("ServiceInsideIP").Value;
string serviceInsideURL = builder.Configuration.GetSection("ServiceInsideURL").Value;
if (string.IsNullOrEmpty(serviceInsideIP))
{
Log.Error(new Exception("ServiceInsideIP was not found in the appsettings.json"), "OutsideService", "Programm");
}
else if (string.IsNullOrEmpty(serviceInsideURL))
{
Log.Error(new Exception("ServiceInsideURL was not found in the appsettings.json"), "OutsideService", "Programm");
}
else if (string.IsNullOrEmpty(defaultConnection))
{
Log.Error(new Exception("Default ConnectionString has been not defined in the appsettings.json"), "OutsideService", "Programm");
}
else
{
string connectionString = builder.Configuration.GetConnectionString(defaultConnection);
if (string.IsNullOrEmpty(connectionString))
{
Log.Error(new Exception("ConnectionString(" + connectionString + ") was not found in the appsettings.json"), "OutsideService", "Programm");
}
else
{
PushNotificationsConfiguration pushNotificationsConfiguration = new PushNotificationsConfiguration(PushNotifications);
if (!pushNotificationsConfiguration.Valid)
{
Log.Error(new Exception("Some of push notification configurations missing. Please check the configuration in the appsettings.json"), "OutsideService", "Programm");
}
else
{
/** check if connection string was stored as encrypted string in config **/
string decryptedConnectionString = AES.Decrypt(connectionString);
if (!string.IsNullOrEmpty(decryptedConnectionString))
{
connectionString = decryptedConnectionString;
}
string appVersion = Assembly.GetEntryAssembly().GetName().Version.ToString();
Log.Info("ServiceOutside(" + appVersion + ") starting...");
DbContext dbContext = new DbContext(connectionString);
dbContext.RunMigration(new Version_2(), appVersion);
builder.Services.AddMemoryCache();
builder.Services.Configure<IpRateLimitOptions>(builder.Configuration.GetSection("IpRateLimiting"));
builder.Services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
builder.Services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
builder.Services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
builder.Services.AddHttpContextAccessor();
builder.Services.Add(new ServiceDescriptor(typeof(DbContext), dbContext));
builder.Services.Add(new ServiceDescriptor(typeof(KeyPair), Curve25519.GenerateKeyPair()));
builder.Services.AddControllers();
WebApplication app = builder.Build();
app.UseIpRateLimiting();
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseRouting();
app.MapControllers();
Log.SetTraceDbController(new ServiceShared.Database.Controllers.Traces(dbContext));
Log.Info("OutsideService successfully started");
Notification.SetConfigurations(pushNotificationsConfiguration);
ServiceInsideRequest.SetServiceInsideIP(serviceInsideIP);
ServiceInside.SetServiceInsideURL(serviceInsideURL);
ServiceInsideRequest.SetLog(logType);
TrustedHeader.SetLog(logType);
TrustedHeader.SetDbContext(dbContext);
BaseController.SetLog(logType);
app.Run();
}
}
}
}