forked from AcaMate/AcaMate_API
101 lines
3.2 KiB
C#
101 lines
3.2 KiB
C#
|
|
using Pomelo.EntityFrameworkCore;
|
|
|
|
using System.Text;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
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();
|
|
|
|
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.Run(); |