forked from AcaMate/AcaMate_API
126 lines
3.9 KiB
C#
126 lines
3.9 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 설정부 시작
|
|
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
|
|
// .AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials()
|
|
.SetIsOriginAllowed(host => true);
|
|
});
|
|
});
|
|
|
|
|
|
///// ===== 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.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.UseCors("CorsPolicy");
|
|
app.UseRouting();
|
|
|
|
app.UseWebSockets();
|
|
app.UseEndpoints(end =>
|
|
{
|
|
end.MapControllers();
|
|
end.MapHub<ChatHub>("/chatHub");
|
|
});
|
|
|
|
|
|
app.Run(); |