-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
84 lines (66 loc) · 2.21 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Book_Management.DBContext;
using Book_Management.GraphQL;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
ConfigureServices(builder.Services, builder.Configuration);
var app = builder.Build();
// Configure the HTTP request pipeline.
ConfigurePipeline(app, app.Environment);
app.Run();
static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
// Database Configuration
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
// GraphQL Configuration
services
.AddGraphQLServer()
.AddQueryType<Query>()
.AddMutationType<Mutation>()
.AddType<AuthorType>()
.AddType<BookType>()
.AddFiltering()
.AddSorting()
.AddProjections();
// CORS Configuration
services.AddCors(options =>
{
options.AddPolicy("Book_Management_Frontend",
builder => builder
.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
// Controllers (if needed for REST endpoints)
services.AddControllers();
// Swagger/OpenAPI (optional, but recommended)
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
}
static void ConfigurePipeline(WebApplication app, IWebHostEnvironment env)
{
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// Enable CORS
app.UseCors("Book_Management_Frontend");
// Ensure database is created and migrated
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.Migrate(); // Applies any pending migrations
}
// Enable HTTPS redirection
app.UseHttpsRedirection();
// Enable authorization (if using authentication)
app.UseAuthorization();
// Map controllers
app.MapControllers();
// Map GraphQL endpoint
app.MapGraphQLHttp("/graphql");
}