CodeLiturgy.Dashboard/BlueWest.Api/Startup.cs

142 lines
4.7 KiB
C#

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 System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using BlueWest.Tools;
using BlueWest.WebApi.Interfaces;
using BlueWest.WebApi.MySQL;
using BlueWest.WebApi.Tools;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
namespace BlueWest.WebApi
{
public class Startup
{
public IConfiguration Configuration { get; }
private readonly IWebHostEnvironment _environment;
private readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
/// <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", "http://127.0.0.1", "http://localhost:3000",
"http://127.0.0.1:3000", "localhost:3000", "127.0.0.1:3000")
.AllowAnyHeader().AllowAnyMethod();
});*/
});
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
/*services
.AddNewtonsoftJson(options =>
options.SerializerSettings.Converters.Add(new StringEnumConverter()));
// order is vital, this *must* be called *after* AddNewtonsoftJson()
services.AddSwaggerGenNewtonsoftSupport();*/
services.AddSwaggerGen(options =>
{
options.SchemaFilter<SwaggerEnumSchemaFilter>();
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "BlueWest.Api Documentation",
Version = "v1",
Description = "A simple example ASP.NET Core Web API",
TermsOfService = new Uri("https://example.com/terms"),
Contact = new OpenApiContact
{
Name = "Benny",
Email = string.Empty,
Url = new Uri("https://git.codeliturgy.com"),
},
License = new OpenApiLicense
{
Name = "Use under LICX",
Url = new Uri("https://git.codeliturgy.com/license"),
}
});
// 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);
});
services
.AddSingleton<EventManager>()
.PrepareDatabasePool(Configuration, _environment)
.AddSingleton<ExchangeInterface>();
// services.AddGrpc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//app.UseStaticFiles();
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "swagger";
c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlueWest.Api v1");
});
//app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(MyAllowSpecificOrigins);
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
// endpoints.MapGrpcService<GreeterService>();
});
}
}
}