-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
157 lines (134 loc) · 5.68 KB
/
Startup.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kryxivia.AuthLoaderAPI.Settings;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using MongoDB.Driver;
using Kryxivia.Domain.Extensions;
using Serilog;
using Kryxivia.AuthLoaderAPI.Middlewares;
using Kryxivia.Contracts.Options;
using Microsoft.DotNet.PlatformAbstractions;
using Kryxivia.Shared.Settings;
using System.Reflection;
using System.IO;
using Kryxivia.AuthLoaderAPI.Middlewares.Attributes;
using Kryxivia.AuthLoaderAPI.HealthChecks;
using Kryxivia.AuthLoaderAPI.Services.LoginQueue;
using Kryxivia.AuthLoaderAPI.Utilities;
using Kryxivia.AuthLoaderAPI.Filters;
using Kryxivia.AuthLoaderAPI.Services.TemporaryToken;
namespace Kryxivia.AuthLoaderAPI
{
public class Startup
{
private readonly IWebHostEnvironment WebHostEnvironment;
public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
{
Configuration = configuration;
WebHostEnvironment = webHostEnvironment;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Configuration...
var web3Section = Configuration.GetSection("Web3");
services.Configure<Web3Settings>(web3Section);
services.Configure<JwtSettings>(Configuration.GetSection("Jwt"));
services.Configure<SwaggerSettings>(Configuration.GetSection("Swagger"));
services.Configure<LoginQueueSettings>(Configuration.GetSection("LoginQueue"));
services.Configure<PlayerStateSettings>(Configuration.GetSection("PlayerState"));
// Kryxivia MongoDB...
services.AddKryxMongoDBWithRepositories(Configuration.GetConnectionString("KryxiviaDatabase"));
// Application Insights...
services.AddApplicationInsightsTelemetry();
// Kryxivia Contracts...
var web3Settings = web3Section.Get<Web3Settings>();
services.AddKryxContracts(options =>
{
if (!string.IsNullOrWhiteSpace(web3Settings.Testnet?.NftContractAddr))
{
options.TestnetWeb3 = web3Settings.TestnetWeb3();
options.TestnetNftContractAddress = web3Settings.Testnet.NftContractAddr;
}
if (!string.IsNullOrWhiteSpace(web3Settings.Mainnet?.NftContractAddr))
{
options.MainnetWeb3 = web3Settings.MainnetWeb3();
options.MainnetNftContractAddress = web3Settings.Mainnet.NftContractAddr;
}
});
// Services...
services.AddSingleton<LoginQueueService>();
services.AddSingleton<TemporaryTokenService>();
services.AddSingleton<PlayerStateService>();
// Health Checks...
services.AddHealthChecks()
.AddCheck<MongoDBHealthCheck>("MongoDB");
// Swagger...
services.AddSwaggerGen(s =>
{
s.SwaggerDoc("v1", new OpenApiInfo() { Title = "Kryxivia.AuthLoaderAPI", Version = "v1" });
s.OperationFilter<AuthorizeCheckOperationFilter>();
s.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header. \r\n\r\n Enter your token in the text input below.\r\n\r\nExample: \" 12345abcdef\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
s.IncludeXmlComments(xmlPath);
});
// Controllers...
services.AddControllers();
// Cors...
services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials();
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Serilog...
app.UseSerilogRequestLogging();
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
// Middlewares...
app.UseMiddleware<JwtMiddleware>();
app.UseMiddleware<SwaggerBasicAuthMiddleware>();
// Swagger...
app.UseSwagger();
app.UseSwaggerUI();
// Service initialization
app.ApplicationServices.GetService<PlayerStateService>();
app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/health");
endpoints.MapControllers();
});
}
}
}