-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
115 lines (94 loc) · 3.02 KB
/
Copy pathProgram.cs
File metadata and controls
115 lines (94 loc) · 3.02 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Reflection;
using Azure.Identity;
using Core;
using Forge.Helpers;
using Forge.Interface;
using Infrastructure;
using Microsoft.OpenApi.Models;
using Scalar.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddScoped<ICommonHelpers, CommonHelpers>();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
// Let's add support for Entity Framework Core and connect to a database
// --- Add Azure Key Vault configuration ---
var keyVaultUri = builder.Configuration["EcWin24KeyVaultUri"];
if (
!string.IsNullOrEmpty(keyVaultUri)
&& Uri.TryCreate(keyVaultUri, UriKind.Absolute, out var validUri)
)
{
builder.Configuration.AddAzureKeyVault(validUri, new DefaultAzureCredential());
}
else
{
Console.WriteLine("Key Vault URI is not valid");
}
// --- End of Azure Key Vault configuration ---
var connectionString = builder.Configuration.GetConnectionString("SQLServerAzure");
// Time to register our Core Services and Infrastructure
builder.Services.AddCoreServices().AddInfrastructure(connectionString!);
// https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-8.0&tabs=visual-studio
builder.Services.AddSwaggerGen(c =>
{
// Adding a new Swagger document
c.SwaggerDoc(
"v1",
new OpenApiInfo
{
Title = "Forge API",
Version = "v1",
Description = "A simple API for managing events and packages.",
Contact = new OpenApiContact()
{
Name = "Georgi Sundberg",
Email = "georgi.sundberg@outlook.com",
Url = new Uri("https://www.google.se"),
},
}
);
// Adding XML comments to Swagger
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
// Adding support for CORS
builder.Services.AddCors(options =>
{
// Adding a new policy
options.AddPolicy(
"AllowAllNoSecurity",
policy =>
{
// Minimal without security...duh
policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
}
);
});
// Registering the CORS policy
var app = builder.Build();
// Configure the HTTP request pipeline.
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapScalarApiReference();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
// Adding a new Swagger endpoint
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Forge API v1");
// Adding OAuth support
c.OAuthClientId("swagger-ui");
// Adding OAuth scopes
c.OAuthScopes("api1", "api2");
});
app.MapOpenApi();
}
app.UseHttpsRedirection();
// Enable CORS after the HTTPS redirection
app.UseCors("AllowAllNoSecurity");
app.UseAuthorization();
app.MapControllers();
app.Run();