2025-11-05 20:50:25 +01:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
2025-11-09 19:39:52 +01:00
|
|
|
using DiunaBI.API.Services;
|
2025-11-05 20:50:25 +01:00
|
|
|
using DiunaBI.Infrastructure.Data;
|
|
|
|
|
using DiunaBI.Infrastructure.Services;
|
|
|
|
|
using Google.Apis.Sheets.v4;
|
|
|
|
|
using Serilog;
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
if (builder.Environment.IsProduction())
|
|
|
|
|
{
|
|
|
|
|
builder.Host.UseSerilog((context, configuration) =>
|
|
|
|
|
{
|
|
|
|
|
var instanceName = context.Configuration["InstanceName"] ?? "unknown";
|
|
|
|
|
configuration
|
|
|
|
|
.ReadFrom.Configuration(context.Configuration)
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
.Enrich.WithProperty("Application", $"DiunaBI-{instanceName}")
|
|
|
|
|
.Enrich.WithProperty("Version", Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown")
|
|
|
|
|
.Enrich.WithEnvironmentName()
|
|
|
|
|
.Enrich.WithMachineName();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var connectionString = builder.Configuration.GetConnectionString("SQLDatabase");
|
|
|
|
|
|
|
|
|
|
builder.Services.AddDbContext<AppDbContext>(x =>
|
|
|
|
|
{
|
|
|
|
|
x.UseSqlServer(connectionString);
|
|
|
|
|
x.EnableSensitiveDataLogging();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("CORSPolicy", corsPolicyBuilder =>
|
|
|
|
|
{
|
|
|
|
|
corsPolicyBuilder.WithOrigins("http://localhost:4200")
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials();
|
|
|
|
|
|
|
|
|
|
corsPolicyBuilder.WithOrigins("https://diuna.bim-it.pl")
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials();
|
|
|
|
|
|
|
|
|
|
corsPolicyBuilder.WithOrigins("https://morska.diunabi.com")
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
|
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
}).AddJwtBearer(options =>
|
|
|
|
|
{
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuer = false,
|
|
|
|
|
ValidateAudience = false,
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
2025-11-12 11:59:11 +01:00
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtSettings:SecurityKey"]!))
|
2025-11-05 20:50:25 +01:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-09 19:39:52 +01:00
|
|
|
builder.Services.AddScoped<GoogleAuthService>();
|
|
|
|
|
builder.Services.AddScoped<JwtTokenService>();
|
|
|
|
|
|
2025-11-05 20:50:25 +01:00
|
|
|
// Google Sheets dependencies
|
|
|
|
|
Console.WriteLine("Adding Google Sheets dependencies...");
|
|
|
|
|
builder.Services.AddSingleton<GoogleSheetsHelper>();
|
|
|
|
|
builder.Services.AddSingleton<GoogleDriveHelper>();
|
|
|
|
|
builder.Services.AddSingleton<SpreadsheetsResource.ValuesResource>(provider =>
|
|
|
|
|
{
|
|
|
|
|
var googleSheetsHelper = provider.GetRequiredService<GoogleSheetsHelper>();
|
|
|
|
|
var valuesResource = googleSheetsHelper.Service?.Spreadsheets.Values;
|
|
|
|
|
|
|
|
|
|
if (valuesResource == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Google Sheets Service is not initialized properly");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return valuesResource;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSingleton<PluginManager>();
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
if (app.Environment.IsProduction())
|
|
|
|
|
{
|
|
|
|
|
app.UseSerilogRequestLogging(options =>
|
|
|
|
|
{
|
|
|
|
|
options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms";
|
|
|
|
|
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
|
|
|
|
|
{
|
|
|
|
|
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
|
|
|
|
|
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
|
|
|
|
|
|
|
|
|
|
var userAgent = httpContext.Request.Headers.UserAgent.FirstOrDefault();
|
|
|
|
|
if (!string.IsNullOrEmpty(userAgent))
|
|
|
|
|
{
|
|
|
|
|
diagnosticContext.Set("UserAgent", userAgent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
diagnosticContext.Set("RemoteIP", httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown");
|
|
|
|
|
diagnosticContext.Set("RequestContentType", httpContext.Request.ContentType ?? "none");
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Plugin initialization
|
|
|
|
|
var pluginManager = app.Services.GetRequiredService<PluginManager>();
|
|
|
|
|
var executablePath = Assembly.GetExecutingAssembly().Location;
|
|
|
|
|
var executableDir = Path.GetDirectoryName(executablePath)!;
|
|
|
|
|
var pluginsPath = Path.Combine(executableDir, "Plugins");
|
|
|
|
|
|
|
|
|
|
if (app.Environment.IsProduction())
|
|
|
|
|
{
|
|
|
|
|
Log.Information("Starting DiunaBI application");
|
|
|
|
|
Log.Information("Loading plugins from: {PluginsPath}", pluginsPath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var logger = app.Services.GetRequiredService<ILogger<Program>>();
|
|
|
|
|
logger.LogInformation("Starting DiunaBI application (Development)");
|
|
|
|
|
logger.LogInformation("Loading plugins from: {PluginsPath}", pluginsPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pluginManager.LoadPluginsFromDirectory(pluginsPath);
|
|
|
|
|
|
|
|
|
|
app.Use(async (context, next) =>
|
|
|
|
|
{
|
|
|
|
|
var token = context.Request.Headers.Authorization.ToString();
|
|
|
|
|
if (token.Length > 0
|
|
|
|
|
&& !context.Request.Path.ToString().Contains("getForPowerBI")
|
|
|
|
|
&& !context.Request.Path.ToString().Contains("getConfiguration")
|
|
|
|
|
&& !context.Request.Path.ToString().Contains("DataInbox/Add"))
|
|
|
|
|
{
|
|
|
|
|
var handler = new JwtSecurityTokenHandler();
|
|
|
|
|
var data = handler.ReadJwtToken(token.Split(' ')[1]);
|
|
|
|
|
context.Request.Headers.Append("UserId", new Microsoft.Extensions.Primitives.StringValues(data.Subject));
|
|
|
|
|
}
|
|
|
|
|
await next(context);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.UseCors("CORSPolicy");
|
|
|
|
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
2025-11-09 19:39:52 +01:00
|
|
|
app.MapGet("/health", () => Results.Ok(new { status = "OK", timestamp = DateTime.UtcNow }))
|
|
|
|
|
.AllowAnonymous();
|
|
|
|
|
|
2025-11-05 20:50:25 +01:00
|
|
|
app.Run();
|
|
|
|
|
|
|
|
|
|
if (app.Environment.IsProduction())
|
|
|
|
|
{
|
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
|
}
|
|
|
|
|
// for testing purposes
|
2025-06-08 14:21:45 +02:00
|
|
|
public partial class Program { }
|