AcaMate_API/Program.cs

135 lines
4.2 KiB
C#

using Pomelo.EntityFrameworkCore;
using System.Text;
using AcaMate.Common.Chat;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.SignalR;
using AcaMate.Common.Models;
using AcaMate.V1.Services;
using AcaMate.Common.Data;
using AcaMate.V1.Controllers;
var builder = WebApplication.CreateBuilder(args);
// DB 설정부 시작
builder.Configuration.AddJsonFile("private/dbSetting.json", optional: true, reloadOnChange: true);
// var connectionString = builder.Configuration.GetConnectionString("MariaDbConnection");
// builder.Services.AddDbContext<AppDbContext>(options => options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
builder.Services.AddDbContext<AppDbContext>(optionsAction: (serviceProvider, options) =>
{
var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
var dbName = httpContextAccessor.HttpContext?.Request.Query["aca_code"].ToString();
var baseConnectionString = builder.Configuration.GetConnectionString("MariaDbConnection");
if (!string.IsNullOrEmpty(dbName))
{
baseConnectionString = baseConnectionString.Replace("database=AcaMate", $"database={dbName}");
}
options.UseMySql(baseConnectionString, ServerVersion.AutoDetect(baseConnectionString));
});
builder.Services.AddHttpContextAccessor();
// DB 설정부 끝
var dbString = builder.Configuration.GetConnectionString("MariaDbConnection");
var userString = builder.Configuration.GetConnectionString("DBAccount");
// JWT 설정부 시작
if (builder.Environment.IsDevelopment())
{
builder.Configuration.AddJsonFile("private/jwtSetting.Development.json", optional: true, reloadOnChange: true);
}
else
{
builder.Configuration.AddJsonFile("private/jwtSetting.json", optional: true, reloadOnChange: true);
}
builder.Services.Configure<JwtSettings>(builder.Configuration.GetSection("JwtSettings"));
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
var jwtSettings = builder.Configuration.GetSection("JwtSettings").Get<JwtSettings>();
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtSettings.Issuer,
ValidAudience = jwtSettings.Audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.SecretKey))
};
});
// JWT 설정부 끝
builder.Services.AddControllers();
// 여기다가 API 있는 컨트롤러들 AddScoped 하면 되는건가?
// builder.Services.AddScoped<UserService>(); //
// builder.Services.AddScoped<UserController>();
builder.Services.AddEndpointsApiExplorer();
// 스웨거 설정 추가 부분
// builder.Services.AddSwaggerGen();
builder.Services.AddCustomSwagger();
// SignalR 설정 추가 부분
builder.Services.AddSignalR();
builder.Services.AddCors(option =>
{
option.AddPolicy("CorsPolicy", builder =>
{
builder
.WithOrigins("https://devacamate.ipstein.myds.me", "https://acamate.ipstein.myds.me") // 특정 도메인만 허용
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
// 로컬 테스트 위한 부분
builder.WebHost.UseUrls("http://0.0.0.0:5144");
///// ===== builder 설정 부 ===== /////
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
// app.UseSwagger();
// app.UseSwaggerUI();
app.UseCustomSwaggerUI();
app.UseDeveloperExceptionPage(); // 좀더 자세한 예외 정보 제공
}
else
{
app.UseExceptionHandler("/error");
app.UseHsts();
}
// 로컬 테스트 위한 부분
// app.UseHttpsRedirection();
app.UseRouting();
// app.MapControllers();
app.UseCors("CorsPolicy");
app.UseAuthorization();
app.UseWebSockets();
app.UseEndpoints(end =>
{
end.MapControllers();
end.MapHub<ChatHub>("/chatHub");
});
app.Run();