110 lines
4.1 KiB
C#

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using PetCompanion.Data;
using PetCompanion.Repositories;
using PetCompanion.Services;
using System.Text.Json.Serialization;
namespace PetCompanion
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Read Firebase configuration from environment variables
var firebaseAppId = Environment.GetEnvironmentVariable("FIREBASE_APP_ID") ??
throw new InvalidOperationException("FIREBASE_APP_ID environment variable is not set");
// Add services to the container.
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Configure Entity Framework Core
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlite(connectionString);
options.EnableSensitiveDataLogging();
});
builder.Services.AddScoped<PetClassRepository>();
builder.Services.AddScoped<PetClassService>();
builder.Services.AddScoped<PetActionService>();
builder.Services.AddScoped<SkillService>();
builder.Services.AddScoped<GameItemsRepository>();
builder.Services.AddScoped<GameItemService>();
builder.Services.AddScoped<PetRepository>();
builder.Services.AddScoped<PetService>();
builder.Services.AddScoped<PetSkillRepository>();
builder.Services.AddScoped<PetInventoryRepository>();
builder.Services.AddScoped<PetSkillService>();
builder.Services.AddScoped<PetInventoryService>();
builder.Services.AddScoped<ActionGatheredRepository>();
// Add the background service
builder.Services.AddHostedService<PetActionBackgroundService>();
// Add CORS policy
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = $"https://securetoken.google.com/{firebaseAppId}";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = $"https://securetoken.google.com/{firebaseAppId}",
ValidateAudience = true,
ValidAudience = firebaseAppId,
ValidateLifetime = true
};
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
// After app builder is created
using (var scope = app.Services.CreateScope())
{
var itemService = scope.ServiceProvider.GetRequiredService<GameItemService>();
itemService.LoadItemsFromCsv("game-data/GameItemsData.csv");
}
// Use CORS policy
app.UseCors("AllowAll");
app.MapControllers();
app.Run();
}
}
}