You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
8.5 KiB
225 lines
8.5 KiB
using System.Reflection;
|
|
using DbContextLib;
|
|
using DbContextLib.Identity;
|
|
using Entities;
|
|
using HeartTrackAPI.Utils;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
using Microsoft.AspNetCore.Mvc.Versioning;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.OpenApi.Models;
|
|
using Model.Manager;
|
|
using Model2Entities;
|
|
using StubAPI;
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
|
|
namespace HeartTrackAPI;
|
|
|
|
public class AppBootstrap(IConfiguration configuration)
|
|
{
|
|
private IConfiguration Configuration { get; } = configuration;
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddControllers();
|
|
services.AddEndpointsApiExplorer();
|
|
AddSwagger(services);
|
|
// include Xml comment
|
|
// addsecurityRequiment
|
|
// securityDef
|
|
AddHeartTrackContextServices(services);
|
|
AddModelService(services);
|
|
AddIdentityServices(services);
|
|
AddApiVersioning(services);
|
|
|
|
services.AddHealthChecks();
|
|
|
|
}
|
|
|
|
private void AddHeartTrackContextServices(IServiceCollection services)
|
|
{
|
|
string connectionString;
|
|
|
|
switch (Environment.GetEnvironmentVariable("TYPE"))
|
|
{
|
|
case "BDD":
|
|
var HOST = System.Environment.GetEnvironmentVariable("HOST");
|
|
var PORT = System.Environment.GetEnvironmentVariable("PORTDB");
|
|
var DATABASE = System.Environment.GetEnvironmentVariable("DATABASE");
|
|
var USERNAME = System.Environment.GetEnvironmentVariable("USERNAME");
|
|
var PASSWORD = System.Environment.GetEnvironmentVariable("PASSWORD");
|
|
|
|
connectionString = $"Server={HOST};port={PORT};database={DATABASE};user={USERNAME};password={PASSWORD}";
|
|
Console.WriteLine(connectionString);
|
|
Console.WriteLine("======================");
|
|
Console.WriteLine($"server={HOST};port={PORT};database={DATABASE};user={USERNAME};password={PASSWORD}");
|
|
Console.WriteLine(connectionString);
|
|
services.AddDbContext<HeartTrackContext>(options =>
|
|
options.UseMySql($"{connectionString}", new MySqlServerVersion(new Version(10, 11, 1)))
|
|
, ServiceLifetime.Singleton);
|
|
break;
|
|
default:
|
|
connectionString = Configuration.GetConnectionString("HeartTrackAuthConnection");
|
|
if (!string.IsNullOrWhiteSpace(connectionString))
|
|
{
|
|
services.AddDbContext<AuthDbContext>(options => options.UseInMemoryDatabase("AuthDb"));
|
|
Console.WriteLine(connectionString);
|
|
Console.WriteLine("======================");
|
|
//options => options.UseSqlite(connectionString)
|
|
//services.AddDbContext<HeartTrackContext>();
|
|
services.AddDbContext<HeartTrackContext>(options =>
|
|
options.UseSqlite(connectionString), ServiceLifetime.Singleton);
|
|
}
|
|
else
|
|
{
|
|
services.AddDbContext<AuthDbContext>(options => options.UseInMemoryDatabase("AuthDb"));
|
|
services.AddDbContext<HeartTrackContext>(options => options.UseInMemoryDatabase("HeartTrackDb"));
|
|
}
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
services.AddSingleton<DbContextOptions<HeartTrackContext>>(provider =>
|
|
{
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
connection.Open();
|
|
|
|
var options = new DbContextOptionsBuilder<HeartTrackContext>()
|
|
.UseSqlite(connection)
|
|
.Options;
|
|
|
|
return options;
|
|
});*/
|
|
}
|
|
|
|
private void AddModelService(IServiceCollection services)
|
|
{
|
|
//services.AddSingleton<IDataManager>(provider => new DbDataManager(provider.GetService<HeartTrackContext>()));
|
|
//services.AddSingleton<IDataManager, StubData>();
|
|
services.AddSingleton<IDataManager>(provider => new DbDataManager(provider.GetRequiredService<HeartTrackContext>()));
|
|
|
|
//services.AddTransient<IActivityManager, ActivityManager>();
|
|
}
|
|
|
|
private void AddIdentityServices(IServiceCollection services)
|
|
{
|
|
// services.AddTransient<IEmailSender<AthleteEntity>, EmailSender>();
|
|
services.AddAuthorization();
|
|
|
|
services.AddIdentityApiEndpoints<IdentityUser>()
|
|
.AddEntityFrameworkStores<AuthDbContext>();
|
|
//services.AddIdentity<AthleteEntity, IdentityRole>()
|
|
// .AddEntityFrameworkStores<AuthDbContext>().AddDefaultTokenProviders();
|
|
}
|
|
|
|
private void AddApiVersioning(IServiceCollection services)
|
|
{
|
|
|
|
services.AddApiVersioning(opt =>
|
|
{
|
|
opt.ReportApiVersions = true;
|
|
opt.AssumeDefaultVersionWhenUnspecified = true;
|
|
opt.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
|
|
// options.ApiVersionReader = new HeaderApiVersionReader("api-version");
|
|
|
|
opt.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(),
|
|
new HeaderApiVersionReader("x-api-version"),
|
|
new MediaTypeApiVersionReader("x-api-version"));
|
|
});
|
|
|
|
}
|
|
private void AddSwagger(IServiceCollection services)
|
|
{
|
|
services.AddSwaggerGen(options =>
|
|
{
|
|
options.OperationFilter<SwaggerDefaultValues>();
|
|
|
|
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: \"Authorization: Bearer {token}\"",
|
|
Name = "Authorization",
|
|
In = ParameterLocation.Header,
|
|
Type = SecuritySchemeType.ApiKey
|
|
});
|
|
var scheme = new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = "Bearer"
|
|
},
|
|
Scheme = "oauth2",
|
|
Name = "Bearer",
|
|
In = ParameterLocation.Header,
|
|
},
|
|
new List<string>()
|
|
}
|
|
};
|
|
options.AddSecurityRequirement(scheme);
|
|
});
|
|
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, SwaggerOptions>();
|
|
services.AddSwaggerGen(options =>
|
|
{
|
|
options.OperationFilter<SwaggerDefaultValues>();
|
|
});
|
|
/* services.AddSwaggerGen(options =>
|
|
{
|
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "HeartTrackAPI", Version = "v1" });
|
|
options.SwaggerDoc("v2", new OpenApiInfo { Title = "HeartTrackAPI", Version = "v2" });
|
|
});*/
|
|
|
|
services.AddVersionedApiExplorer(setup =>
|
|
{
|
|
setup.GroupNameFormat = "'v'VVV";
|
|
setup.SubstituteApiVersionInUrl = true;
|
|
});
|
|
|
|
}
|
|
|
|
public void Configure(WebApplication app, IWebHostEnvironment env)
|
|
{
|
|
app.UseHttpsRedirection();
|
|
app.MapIdentityApi<IdentityUser>();
|
|
|
|
app.MapControllers();
|
|
app.UseAuthorization();
|
|
|
|
app.MapHealthChecks("/health");
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (true)
|
|
{
|
|
var apiVersionDescriptionProvider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
app.MapSwagger();
|
|
app.UseSwaggerUI(options =>
|
|
{
|
|
foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
|
|
|
|
//foreach (var description in apiVersionDescriptionProvider)
|
|
{
|
|
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json",
|
|
description.GroupName.ToUpperInvariant());
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|