2025-07-31 14:54:32 -07:00

94 lines
3.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.Schema;
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_api.Hubs;
using qtc_api.Services.RoomService;
using qtc_api.Services.ContactService;
using qtc_api.Services.CurrencyGamesService;
using qtc_api.Services.GameRoomService;
using qtc_api.Services.StoreService;
using qtc_api.Services.EmailService;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddDbContext<DataContext>();
builder.Services.AddSignalR().AddHubOptions<ChatHub>(options =>
{
options.EnableDetailedErrors = true;
options.KeepAliveInterval = TimeSpan.FromSeconds(15);
});
builder.Services.AddStackExchangeRedisCache(options =>
{
var redisConnectionString = Environment.GetEnvironmentVariable("REDIS_CONNECTIONSTRING");
if (redisConnectionString != null)
options.Configuration = redisConnectionString;
if (!builder.Environment.IsProduction())
options.InstanceName = "QtCNetServerDev_";
else
options.InstanceName = "QtCNetServer_";
});
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"]
};
var jwtKey = Environment.GetEnvironmentVariable("JWT_KEY");
if (jwtKey != null)
options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey));
else throw new Exception("Cannot Find Environment Variables 'JWT_KEY'. Please Check Environment.");
});
builder.Services.AddTransient<IEmailService, EmailService>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<ITokenService, TokenService>();
builder.Services.AddScoped<IRoomService, RoomService>();
builder.Services.AddScoped<IContactService, ContactService>();
builder.Services.AddScoped<StoreService>();
builder.Services.AddSingleton<CurrencyGamesService>();
builder.Services.AddSingleton<GameRoomService>();
builder.Services.AddHostedService(provider => provider.GetService<CurrencyGamesService>()!);
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", options =>
{
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets |
Microsoft.AspNetCore.Http.Connections.HttpTransportType.LongPolling;
});
app.MapHub<TicTacToeHub>("/tttgame");
app.Run();