2026-07-23 13:54:06 -07:00

99 lines
3.2 KiB
C#

global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.SignalR;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.IdentityModel.Tokens;
global using qtc_api.Data;
global using qtc_api.Dtos.Room;
global using qtc_api.Dtos.User;
global using qtc_api.Hubs;
global using qtc_api.Models;
global using qtc_api.Schema;
global using qtc_api.Services.TokenService;
global using qtc_api.Services.UserService;
global using System.Text;
using qtc_api.Services.BucketService;
using qtc_api.Services.ContactService;
using qtc_api.Services.CurrencyGamesService;
using qtc_api.Services.EmailService;
using qtc_api.Services.GameRoomService;
using qtc_api.Services.RoomService;
using qtc_api.Services.StoreService;
using System.Text.Json;
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;
});
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();
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.AddScoped<IBucketService, BucketService>();
builder.Services.AddScoped(provider => GetServerConfig());
ServerConfig GetServerConfig()
{
if (!File.Exists("./Resources/ServerConfig.json"))
throw new Exception("Server Config Doesn't Exist. Please Refer To The Repo For Examples.");
else
{
try
{
var _jsonText = File.ReadAllText("./Resources/ServerConfig.json");
ServerConfig? _config = JsonSerializer.Deserialize<ServerConfig>(_jsonText);
if (_config != null)
return _config;
else throw new Exception("Invalid Server Config. Please Refer To The Repo For Examples.");
}
catch (JsonException)
{
throw new Exception("Invalid Server Config. Please Refer To The Repo For Examples.");
}
}
}
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.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapHub<ChatHub>("/chat");
app.MapHub<TicTacToeHub>("/tttgame");
app.Run();