64 lines
2.2 KiB
C#
64 lines
2.2 KiB
C#
global using Microsoft.AspNetCore.Mvc;
|
|
global using Microsoft.EntityFrameworkCore;
|
|
global using Microsoft.AspNetCore.SignalR;
|
|
global using Microsoft.AspNetCore.Authorization;
|
|
global using qtc_api.Models;
|
|
global using qtc_api.Data;
|
|
global using qtc_api.Dtos.User;
|
|
global using qtc_api.Dtos.Room;
|
|
global using qtc_api.Services.UserService;
|
|
global using Microsoft.IdentityModel.Tokens;
|
|
global using System.Text;
|
|
global using qtc_api.Services.TokenService;
|
|
global using qtc_gateway.Hubs;
|
|
using qtc_api.Services.RoomService;
|
|
using qtc_api.Services.ContactService;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddControllers();
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddDbContext<DataContext>(options =>
|
|
{
|
|
if (builder.Environment.IsProduction()) options.UseMySQL(builder.Configuration.GetConnectionString("DefaultConnection"));
|
|
else options.UseSqlite(builder.Configuration.GetConnectionString("DevelopmentConnection"));
|
|
|
|
// ignore pending model changes warning
|
|
options.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning));
|
|
});
|
|
builder.Services.AddSignalR();
|
|
|
|
builder.Services.AddAuthentication().AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
|
ValidAudience = builder.Configuration["Jwt:Audience"],
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!))
|
|
};
|
|
});
|
|
builder.Services.AddScoped<IUserService, UserService>();
|
|
builder.Services.AddScoped<ITokenService, TokenService>();
|
|
builder.Services.AddScoped<IRoomService, RoomService>();
|
|
builder.Services.AddScoped<IContactService, ContactService>();
|
|
|
|
var app = builder.Build();
|
|
|
|
using var scope = app.Services.CreateScope();
|
|
|
|
await scope.ServiceProvider.GetRequiredService<DataContext>().Database.EnsureCreatedAsync();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.MapHub<ChatHub>("/chat");
|
|
|
|
app.Run();
|