CodeLiturgy.Dashboard/BlueWest.Api/Startup.cs

183 lines
6.3 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-10 00:33:17 +03:00
using BlueWest.WebApi.Context.Users;
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-10 00:33:17 +03:00
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.FileProviders;
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;
private readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
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-08-13 08:34:20 +03:00
/*options.AddPolicy(name: MyAllowSpecificOrigins,
2021-12-06 02:49:27 +03:00
builder =>
{
builder.WithOrigins("http://localhost", "http://127.0.0.1", "http://localhost:3000",
"http://127.0.0.1:3000", "localhost:3000", "127.0.0.1:3000")
.AllowAnyHeader().AllowAnyMethod();
2022-08-13 08:34:20 +03:00
});*/
2021-12-06 02:49:27 +03:00
});
2022-08-20 05:47:32 +03:00
services
.AddControllers()
.AddJsonOptions(options => options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);
2022-08-19 06:18:50 +03:00
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-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-10 08:05:24 +03:00
services.AddAuthServerServices(MyAllowSpecificOrigins, _configuration, _environment);
services.AddScoped<ExchangeInterface>();
2022-09-10 07:12:03 +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":
services.PrepareMySqlDatabasePool(_configuration, _environment);
break;
case "sqlite":
services.PrepareSqlLiteDatabasePool(_configuration, _environment);
break;
default:
throw new InvalidOperationException("config.json doesn't specify a valid database. Use mysql or sqlite.");
2022-09-06 07:54:48 +03:00
}
2022-09-10 07:12:03 +03:00
2022-09-06 07:54:48 +03:00
2022-08-19 19:47:35 +03:00
2022-08-13 08:34:20 +03:00
// services.AddGrpc();
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.UseDeveloperExceptionPage();
app.UseSwagger();
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-10 07:28:41 +03:00
app.UseStaticFiles();
//app.UseHttpsRedirection();
2022-09-10 07:12:03 +03:00
2022-09-10 08:05:24 +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
});
}
}
}