This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
forked from abrl91/dot-net8-api-sample
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
82 lines (67 loc) · 2.09 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
using System.Reflection;
using GraphQL;
using Microsoft.EntityFrameworkCore;
using MyService;
using MyService.APIs;
using MyService.Infrastructure;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
builder.Services.RegisterServices();
builder.Services.RegisterGraphQL();
builder.Services.AddApiAuthentication();
// Add a DbContext to the container
builder.Services.AddDbContext<MyServiceContext>(opt =>
// opt.UseInMemoryDatabase("TodoList")
opt.UseNpgsql(builder.Configuration.GetConnectionString("DbContext"))
);
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.UseOpenApiAuthentication();
// using System.Reflection;
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
builder.Services.AddCors(builder =>
{
builder.AddPolicy(
"MyCorsPolicy",
policy =>
{
policy
.AllowAnyHeader()
.AllowAnyMethod()
.WithOrigins(["localhost", "https://studio.apollographql.com"])
.AllowCredentials();
}
);
});
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
await RolesManager.SyncRoles(services, app.Configuration);
}
app.UseApiAuthentication();
app.UseCors();
app.MapGraphQLEndpoints();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseStaticFiles();
app.UseSwaggerUI(options =>
{
options.InjectStylesheet("/swagger-ui/swagger.css");
});
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
await SeedDevelopmentData.SeedDevUser(services, app.Configuration);
}
}
app.UseHttpsRedirection();
app.MapControllers();
app.Run();