-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
81 lines (76 loc) · 3.05 KB
/
Program.cs
File metadata and controls
81 lines (76 loc) · 3.05 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
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.OpenApi.Models;
using RomRepo.api.Auth;
using RomRepo.api.DataAccess;
using RomRepo.api.Services;
using System.Reflection;
namespace RomRepo.api
{
/// <summary>Application entry point</summary>
public class Program
{
/// <summary>Application entry point</summary>
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddDbContext<ApiContext>();
builder.Services.AddScoped<IApiRepository, ApiRepository>();
builder.Services.AddScoped<IFileService, FileService>();
builder.Services.AddScoped<IRomService, RomService>();
builder.Services.AddScoped<IApiKeyService, ApiKeyService>();
builder.Services.AddScoped<AdminFilter>();
builder.Services.Configure<KestrelServerOptions>(options => options.Limits.MaxRequestBodySize = int.MaxValue);
//Remember to enable "GenerateDocumentationFile" in project settings
builder.Services.AddSwaggerGen(opt =>
{
opt.SwaggerDoc("v1", new OpenApiInfo
{
Title = "RomRepo.api",
Version = "v1"
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
opt.IncludeXmlComments(xmlPath);
opt.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "API Key is needed for most operations",
Name = "x-api-key",
Type = SecuritySchemeType.ApiKey
});
opt.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "ApiKey"
}
},
new List<string>()
}
});
});
builder.Services.AddAuthentication()
.AddScheme<KeyAuthSchemeOptions, KeyAuthSchemeHandler>(
"ApiKey",
opts => { }
);
var app = builder.Build();
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers().RequireAuthorization();
app.Run();
}
}
}