using System;
using BlueWest.WebApi.EF;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
namespace BlueWest.WebApi
{
///
/// Startup Extensions
///
public static class StartupExtensions
{
private static MySqlServerVersion GetMySqlServerVersion(int major, int minor, int build) => new (new Version(major, minor, build));
///
/// Get MYSQL Connection String
///
///
///
///
private static DbContextOptionsBuilder GetMySqlSettings(
this DbContextOptionsBuilder optionsBuilder,
IConfiguration configuration,
IWebHostEnvironment environment)
{
optionsBuilder
.UseMySql(configuration.GetConnectionString("DockerMySQL"), GetMySqlServerVersion(8, 0, 11))
.UseMySql(GetMySqlServerVersion(8, 0, 11),
builder =>
{
builder.EnableRetryOnFailure(6, TimeSpan.FromSeconds(3), null);
});
// The following three options help with debugging, but should
// be changed or removed for production.
if (environment.IsDevelopment())
{
optionsBuilder
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
return optionsBuilder;
}
///
/// Setup database Contexts
///
///
///
///
///
public static IServiceCollection PrepareMySQLDatabasePool(this IServiceCollection serviceCollection,
IConfiguration configuration, IWebHostEnvironment environment)
{
return serviceCollection
.AddDbContextPool(options => options.GetMySqlSettings(configuration, environment))
.AddDbContextPool(options => options.GetMySqlSettings(configuration, environment))
.AddDbContextPool(options => options.GetMySqlSettings(configuration, environment))
.AddDbContextPool(options => options.GetMySqlSettings(configuration, environment));
}
///
/// Setup database Contexts
///
///
///
///
///
public static IServiceCollection PrepareSQLLiteDatabasePool(this IServiceCollection serviceCollection,
IConfiguration configuration, IWebHostEnvironment environment)
{
return serviceCollection
.AddDbContextPool(options => options.UseSqlite("Data Source=BlueWest.Api.db"))
.AddDbContextPool(options => options.UseSqlite("Data Source=BlueWest.Api.db"))
.AddDbContextPool(options => options.UseSqlite("Data Source=BlueWest.Api.db"))
.AddDbContextPool(options => options.UseSqlite("Data Source=BlueWest.Api.db"));
}
}
}