CodeLiturgy.Dashboard/BlueWest.Api/Startup.cs

206 lines
7.0 KiB
C#
Raw Normal View History

2022-08-22 02:51:45 +03:00
using System;
using System.IO;
using System.Reflection;
2021-12-06 02:49:27 +03:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2022-08-19 02:02:57 +03:00
using System.Text.Json.Serialization;
2022-08-19 19:47:35 +03:00
using BlueWest.Tools;
2022-09-17 22:13:35 +03:00
using BlueWest.WebApi.Interceptors;
2022-08-19 19:47:35 +03:00
using BlueWest.WebApi.Interfaces;
2022-08-22 02:51:45 +03:00
using BlueWest.WebApi.Tools;
2022-09-17 22:13:35 +03:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
2022-08-22 02:51:45 +03:00
using Microsoft.OpenApi.Models;
2021-12-06 02:49:27 +03:00
namespace BlueWest.WebApi
{
2022-08-20 05:47:32 +03:00
/// <summary>
/// Startup class for the API.
/// </summary>
2021-12-06 02:49:27 +03:00
public class Startup
{
2022-08-20 05:47:32 +03:00
private readonly IConfiguration _configuration;
2022-08-19 19:47:35 +03:00
private readonly IWebHostEnvironment _environment;
2022-09-17 22:13:35 +03:00
2022-09-12 17:57:37 +03:00
private readonly string MyAllowSpecificOrigins = Constants.CorsPolicyName;
2022-08-19 19:47:35 +03:00
2022-08-19 02:20:06 +03:00
/// <summary>
/// Startup configuration of the API
/// </summary>
2022-08-19 19:47:35 +03:00
public Startup(IConfiguration configuration, IWebHostEnvironment hostEnvironment)
2021-12-06 02:49:27 +03:00
{
2022-08-20 05:47:32 +03:00
_configuration = configuration;
2022-08-19 19:47:35 +03:00
_environment = hostEnvironment;
2021-12-06 02:49:27 +03:00
}
2022-08-19 19:47:35 +03:00
2021-12-06 02:49:27 +03:00
2022-08-19 19:47:35 +03:00
/// <summary>
/// Configure Services
/// </summary>
/// <param name="services">Dependency injection</param>
2021-12-06 02:49:27 +03:00
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
2022-09-11 06:22:01 +03:00
options.AddPolicy(name: MyAllowSpecificOrigins,
2021-12-06 02:49:27 +03:00
builder =>
{
2022-09-17 22:13:35 +03:00
builder.WithOrigins("http://localhost:5173/", "http://localhost:5173", "http://127.0.0.1:5173", "localhost:5173")
2022-09-12 17:57:37 +03:00
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
2022-09-11 06:22:01 +03:00
});
2021-12-06 02:49:27 +03:00
});
2022-08-20 05:47:32 +03:00
services
2022-09-17 22:13:35 +03:00
.AddResponseCaching()
.AddControllers(options =>
{
options.CacheProfiles.Add("Default30",
new CacheProfile()
{
Duration = 30
});
})
2022-08-20 05:47:32 +03:00
.AddJsonOptions(options => options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);
2022-08-19 06:18:50 +03:00
2022-09-17 22:13:35 +03:00
services.AddLogging(builder =>
{
builder.AddSimpleConsole();
});
services.AddSession(options =>
{
2022-09-19 05:50:15 +03:00
options.Cookie.Domain = SessionConstants.CookieDomain;
2022-09-17 22:13:35 +03:00
options.Cookie.HttpOnly = true;
});
2022-08-20 05:47:32 +03:00
services
2022-08-22 02:51:45 +03:00
.AddSwaggerGen(options =>
{
options.SchemaFilter<SwaggerEnumSchemaFilter>();
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "BlueWest.Api.App",
2022-09-07 20:26:28 +03:00
Version = "v1"
2022-08-22 02:51:45 +03:00
});
2022-09-10 07:12:03 +03:00
2022-08-22 02:51:45 +03:00
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
2022-09-10 08:05:24 +03:00
2022-08-22 02:51:45 +03:00
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
2022-09-10 08:05:24 +03:00
2022-08-22 02:51:45 +03:00
options.IncludeXmlComments(xmlPath);
2022-09-10 07:12:03 +03:00
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description =
"JWT Authorization header using the Bearer scheme (Example: 'Bearer 12345abcdef')",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
2022-08-22 02:51:45 +03:00
});
2022-09-17 22:13:35 +03:00
services.AddGrpc(options =>
{
options.Interceptors.Add<ServerLoggerInterceptor>();
});
services.AddSingleton<ServerLoggerInterceptor>();
2022-09-10 07:12:03 +03:00
/*
2022-09-10 00:33:17 +03:00
services.AddSingleton<IFileProvider>(
new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/ImageFiles")
)
);
2022-09-10 07:12:03 +03:00
*/
2022-09-10 00:33:17 +03:00
2022-09-06 07:54:48 +03:00
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile("config.json")
.Build();
var allowedDatabase = configuration["database"];
2022-08-19 06:18:50 +03:00
services
2022-09-06 07:54:48 +03:00
.AddSingleton<EventManager>();
2022-09-17 22:13:35 +03:00
2022-09-06 07:54:48 +03:00
2022-09-18 04:17:37 +03:00
services.AddAuthServerServices( _configuration, _environment, configuration);
2022-09-10 08:05:24 +03:00
services.AddScoped<ExchangeInterface>();
2022-09-10 07:12:03 +03:00
2022-09-17 22:13:35 +03:00
2022-09-07 20:26:28 +03:00
switch (allowedDatabase)
2022-09-06 07:54:48 +03:00
{
2022-09-07 20:26:28 +03:00
case "mysql":
2022-09-18 04:00:24 +03:00
services.PrepareMySqlDatabasePool(_configuration, _environment, configuration);
2022-09-07 20:26:28 +03:00
break;
case "sqlite":
services.PrepareSqlLiteDatabasePool(_configuration, _environment);
break;
default:
2022-09-19 05:50:15 +03:00
services.PrepareMySqlDatabasePool(_configuration, _environment, configuration);
break;
2022-09-06 07:54:48 +03:00
}
2022-09-10 07:12:03 +03:00
2021-12-06 02:49:27 +03:00
}
2022-08-13 06:35:36 +03:00
2022-09-07 20:26:28 +03:00
2022-08-20 05:47:32 +03:00
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app">Object with the necessary data to configure an application's request pipeline</param>
/// <param name="env">Provides information about the web hosting environment an application is running in.</param>
2021-12-06 02:49:27 +03:00
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
2022-08-13 20:15:43 +03:00
//app.UseStaticFiles();
app.UseSwagger();
2022-09-17 22:13:35 +03:00
2022-08-13 20:15:43 +03:00
app.UseSwaggerUI(c =>
2021-12-06 02:49:27 +03:00
{
2022-08-13 20:15:43 +03:00
c.RoutePrefix = "swagger";
c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlueWest.Api v1");
});
2022-09-17 22:13:35 +03:00
2022-09-10 07:28:41 +03:00
app.UseStaticFiles();
//app.UseHttpsRedirection();
2022-09-10 07:12:03 +03:00
2021-12-06 02:49:27 +03:00
app.UseRouting();
app.UseCors(MyAllowSpecificOrigins);
2022-09-10 00:33:17 +03:00
app.UseAuthentication();
2021-12-06 02:49:27 +03:00
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
2022-08-13 08:34:20 +03:00
// endpoints.MapGrpcService<GreeterService>();
2021-12-06 02:49:27 +03:00
});
}
}
}