192 lines
6.4 KiB
C#
192 lines
6.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System.Text.Json.Serialization;
|
|
using BlueWest.Data.Auth;
|
|
using BlueWest.Tools;
|
|
using BlueWest.WebApi.Interceptors;
|
|
using BlueWest.WebApi.Interfaces;
|
|
using BlueWest.WebApi.Tools;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
namespace BlueWest.WebApi
|
|
{
|
|
/// <summary>
|
|
/// Startup class for the API.
|
|
/// </summary>
|
|
public class Startup
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IWebHostEnvironment _environment;
|
|
|
|
private readonly string MyAllowSpecificOrigins = Constants.CorsPolicyName;
|
|
|
|
/// <summary>
|
|
/// Startup configuration of the API
|
|
/// </summary>
|
|
public Startup(IConfiguration configuration, IWebHostEnvironment hostEnvironment)
|
|
{
|
|
_configuration = configuration;
|
|
_environment = hostEnvironment;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Configure Services
|
|
/// </summary>
|
|
/// <param name="services">Dependency injection</param>
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddCors(options =>
|
|
{
|
|
options.AddPolicy(name: MyAllowSpecificOrigins,
|
|
builder =>
|
|
{
|
|
builder.WithOrigins("http://localhost:5173/", "http://localhost:5173", "http://127.0.0.1:5173", "localhost:5173")
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
services.AddDistributedMemoryCache();
|
|
|
|
services.AddSession(options =>
|
|
{
|
|
options.Cookie.Name = ".BlueWest.Session";
|
|
options.Cookie.Domain = SessionConstants.CookieDomain;
|
|
options.Cookie.HttpOnly = true;
|
|
});
|
|
services
|
|
.AddResponseCaching()
|
|
.AddControllers(options =>
|
|
{
|
|
options.CacheProfiles.Add("Default30",
|
|
new CacheProfile()
|
|
{
|
|
Duration = 30
|
|
});
|
|
})
|
|
.AddJsonOptions(options => options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);
|
|
|
|
services.AddLogging(builder =>
|
|
{
|
|
builder.AddSimpleConsole();
|
|
});
|
|
|
|
|
|
services
|
|
.AddSwaggerGen(options =>
|
|
{
|
|
options.SchemaFilter<SwaggerEnumSchemaFilter>();
|
|
options.SwaggerDoc("v1", new OpenApiInfo
|
|
{
|
|
Title = "BlueWest.Api.App",
|
|
Version = "v1"
|
|
});
|
|
|
|
// Set the comments path for the Swagger JSON and UI.
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
|
|
options.IncludeXmlComments(xmlPath);
|
|
|
|
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>()
|
|
}
|
|
});
|
|
|
|
});
|
|
|
|
|
|
services.AddGrpc(options =>
|
|
{
|
|
options.Interceptors.Add<ServerLoggerInterceptor>();
|
|
});
|
|
|
|
services.AddSingleton<ServerLoggerInterceptor>();
|
|
/*
|
|
services.AddSingleton<IFileProvider>(
|
|
new PhysicalFileProvider(
|
|
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/ImageFiles")
|
|
)
|
|
);
|
|
*/
|
|
|
|
|
|
services
|
|
.AddSingleton<EventManager>();
|
|
|
|
|
|
services.AddAuthServerServices( _configuration, _environment);
|
|
services.AddScoped<ExchangeInterface>();
|
|
|
|
services.PrepareMySqlDatabasePool(_configuration, _environment);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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>
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
|
|
//app.UseStaticFiles();
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI(c =>
|
|
{
|
|
c.RoutePrefix = "swagger";
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlueWest.Api v1");
|
|
});
|
|
|
|
app.UseStaticFiles();
|
|
//app.UseHttpsRedirection();
|
|
|
|
app.UseRouting();
|
|
app.UseCors(MyAllowSpecificOrigins);
|
|
app.UseSession();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
// endpoints.MapGrpcService<GreeterService>();
|
|
});
|
|
}
|
|
}
|
|
}
|