Add new Backend structure with proper .NET 8 projects
This commit is contained in:
100
src/Backend/DiunaBI.WebAPI/Program.cs
Normal file
100
src/Backend/DiunaBI.WebAPI/Program.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using FirebaseAdmin;
|
||||
using Google.Apis.Auth.OAuth2;
|
||||
using Google.Cloud.Firestore;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Text;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
using DiunaBI.Core.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
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,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Secret"]!))
|
||||
};
|
||||
|
||||
});
|
||||
builder.Services.AddAuthentication();
|
||||
|
||||
builder.Services.AddSingleton(typeof(GoogleSheetsHelper));
|
||||
builder.Services.AddSingleton(typeof(GoogleDriveHelper));
|
||||
|
||||
var fileName = "diunabi-admin-firebase.json";
|
||||
#if DEBUG
|
||||
fileName = "diunabi-admin-firebase-Development.json";
|
||||
#endif
|
||||
var credentialPath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
|
||||
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credentialPath);
|
||||
FirebaseAdmin.FirebaseApp.Create(new AppOptions()
|
||||
{
|
||||
Credential = GoogleCredential.GetApplicationDefault()
|
||||
});
|
||||
builder.Services.AddSingleton(FirestoreDb.Create("diunabi-admin"));
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
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();
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user