Files
SystemX.Web/Projects/VPKI/VPKI/VPKI.Web.Api/Program.cs
2025-04-30 14:13:58 +09:00

140 lines
3.8 KiB
C#

using DB.VPKI_AccountDB;
using DB.VPKI_DataDB;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Identity.Client;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using System.Security.Cryptography.Xml;
using System.Text;
using VPKI.Library.Config;
using VPKI.Library.Services;
using VPKI.Web.Api.Services;
string configDir = @"../Config";
//raed log4net config
if (Log4net.IsConfigLoad == true)
{
Log4net.WriteLine("Log4net Init Success");
Log4net.AutoRemoveLog();
}
else
{
Log4net.WriteLine("Log4net Init Failed", LogType.Error);
}
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpContextAccessor();
//singleton
builder.Services.AddSingleton<ConfigService<WebApiConfig>>();
builder.Services.AddSingleton<CertificateService>();
//scoped
builder.Services.AddScoped<VpkiBaseService>();
builder.Services.AddScoped<ISO15118_02Service>();
builder.Services.AddScoped<ISO15118_20Service>();
builder.Services.AddScoped<AuthService>();
builder.Services.AddScoped<UserService>();
//dbContext
builder.Services.AddDbContext<VpkiAccountDbContext>();
builder.Services.AddDbContext<VpkiDataDbContext>();
//config preload
ConfigService<WebApiConfig> preloadConfig = new ConfigService<WebApiConfig>();
if (preloadConfig.OpenConfig($@"{configDir}/VPKI.WebApiConfig.json") == true)
{
var config = preloadConfig.GetConfig();
//auth
builder.Services
.AddAuthentication(option =>
{
option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ClockSkew = TimeSpan.Zero,
ValidIssuer = $"{config?.Auth?.issuer}",
ValidAudience = $"{config?.Auth?.audience}",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes($"{config?.Auth?.accessTokenSecret}"))
};
});
}
else
{
Console.WriteLine("Config Preload Load Error.");
return;
}
var app = builder.Build();
//read api config and set
string serverUrl = string.Empty;
var configService = app.Services.GetService<ConfigService<WebApiConfig>>();
bool isIIS = false;
if (configService?.OpenConfig($@"{configDir}/VPKI.WebApiConfig.json") == true)
{
Log4net.WriteLine("WebApi Config Success.");
var apiConfig = ConfigService<WebApiConfig>.Config;
if(apiConfig != null)
{
serverUrl = $"{apiConfig?.Server?.Address}:{apiConfig?.Server?.Port}";
isIIS = apiConfig!.Server.IIS;
}
}
else
{
Log4net.WriteLine("WebApi Config Error.");
return;
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
Log4net.WriteLine($"IsDevelopment:{app.Environment.IsDevelopment()}");
Log4net.WriteLine($"Swagger Url: {serverUrl}/swagger");
app.UseSwagger();
app.UseSwaggerUI(option => {
option.DefaultModelsExpandDepth(-1);
});
}
app.UseAuthentication();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
if (isIIS == true)
{
app.Run();
}
else
{
Log4net.WriteLine($"Operation Url: {serverUrl}");
app.Run($"{serverUrl}");
}