CodeLiturgy.Dashboard/BlueWest.Api/Startup.cs

142 lines
4.7 KiB
C#
Raw Normal View History

2021-12-06 02:49:27 +03:00
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;
2022-08-13 20:15:43 +03:00
using System.IO;
2021-12-06 02:49:27 +03:00
using System.Linq;
2022-08-13 20:15:43 +03:00
using System.Reflection;
2022-08-19 02:02:57 +03:00
using System.Text.Json.Serialization;
2021-12-06 02:49:27 +03:00
using System.Threading.Tasks;
2022-08-19 19:47:35 +03:00
using BlueWest.Tools;
using BlueWest.WebApi.Interfaces;
2022-08-04 03:59:04 +03:00
using BlueWest.WebApi.MySQL;
2021-12-06 02:49:27 +03:00
using BlueWest.WebApi.Tools;
2022-08-04 03:59:04 +03:00
using Microsoft.EntityFrameworkCore;
2021-12-06 02:49:27 +03:00
using Microsoft.OpenApi.Models;
namespace BlueWest.WebApi
{
public class Startup
{
2022-08-19 19:47:35 +03:00
public IConfiguration Configuration { get; }
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
{
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-19 02:02:57 +03:00
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
2021-12-06 02:49:27 +03:00
/*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
{
2022-08-13 20:15:43 +03:00
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"),
}
2021-12-06 02:49:27 +03:00
});
2022-08-13 20:15:43 +03:00
// 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);
2021-12-06 02:49:27 +03:00
});
2022-08-19 06:18:50 +03:00
services
2022-08-19 19:47:35 +03:00
.AddSingleton<EventManager>()
.PrepareDatabasePool(Configuration, _environment)
.AddSingleton<ExchangeInterface>();
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
2021-12-06 02:49:27 +03:00
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
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");
});
2021-12-06 02:49:27 +03:00
//app.UseHttpsRedirection();
2021-12-06 02:49:27 +03:00
app.UseRouting();
app.UseCors(MyAllowSpecificOrigins);
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
2022-08-13 08:34:20 +03:00
// endpoints.MapGrpcService<GreeterService>();
2021-12-10 03:04:48 +03:00
2021-12-06 02:49:27 +03:00
});
}
}
}