AlanMoonbase 41e1ad4bf0 Implement First Currency Game - Stock Market
Added `StockAmount` To `User` Model (Database Update Required)
Reworked `ChatHub` Command Names
Removed Cache On User Info Endpoints
2025-06-26 16:12:50 -07:00

77 lines
2.7 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_api.Hubs;
using qtc_api.Services.RoomService;
using qtc_api.Services.ContactService;
using qtc_api.Services.CurrencyGamesService;
using Microsoft.Extensions.DependencyInjection;
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.AddEndpointsApiExplorer();
builder.Services.AddSignalR();
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.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<ITokenService, TokenService>();
builder.Services.AddScoped<IRoomService, RoomService>();
builder.Services.AddScoped<IContactService, ContactService>();
builder.Services.AddSingleton<CurrencyGamesService>();
builder.Services.AddHostedService<CurrencyGamesService>(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");
app.Run();