119 lines
4.1 KiB
C#
119 lines
4.1 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 = false;
|
|
});
|
|
|
|
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.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());
|
|
|
|
builder.Services.AddAuthentication().AddJwtBearer(options =>
|
|
{
|
|
var jwtIssuer = builder.Configuration["Jwt:Issuer"];
|
|
var jwtAudience = builder.Configuration["Jwt:Audience"];
|
|
var jwtKey = builder.Configuration["Jwt:Key"] ?? Environment.GetEnvironmentVariable("JWT_KEY");
|
|
|
|
// check configuration for invalid jwt settings
|
|
if (jwtIssuer is null || jwtAudience is null || jwtKey is null)
|
|
throw new Exception("JWT Validation Parameters Are Not Set In appsettings.json. Please Set These Parameters.");
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = jwtIssuer,
|
|
ValidAudience = jwtAudience,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
|
|
};
|
|
});
|
|
|
|
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();
|