CodeLiturgy.Dashboard/BlueWest.Api/Extensions/StartupExtensions.cs

88 lines
3.7 KiB
C#
Raw Normal View History

2022-08-13 06:35:36 +03:00
using System;
2022-08-22 00:14:50 +03:00
using BlueWest.WebApi.EF;
2022-08-19 19:47:35 +03:00
using Microsoft.AspNetCore.Hosting;
2022-08-13 06:35:36 +03:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
2022-08-19 19:47:35 +03:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2022-08-19 06:18:50 +03:00
using Microsoft.Extensions.Logging;
2022-09-06 07:54:48 +03:00
using Microsoft.Extensions.Configuration;
2022-08-13 06:35:36 +03:00
2022-08-19 19:47:35 +03:00
namespace BlueWest.WebApi
2022-08-13 06:35:36 +03:00
{
2022-08-19 19:47:35 +03:00
/// <summary>
/// Startup Extensions
/// </summary>
public static class StartupExtensions
2022-08-13 06:35:36 +03:00
{
2022-09-06 07:54:48 +03:00
private static MySqlServerVersion GetMySqlServerVersion(int major, int minor, int build) => new (new Version(major, minor, build));
2022-08-19 19:47:35 +03:00
/// <summary>
/// Get MYSQL Connection String
/// </summary>
/// <param name="optionsBuilder"></param>
/// <param name="configuration"></param>
2022-08-20 05:47:32 +03:00
/// <param name="environment"></param>
private static DbContextOptionsBuilder GetMySqlSettings(
2022-08-19 19:47:35 +03:00
this DbContextOptionsBuilder optionsBuilder,
IConfiguration configuration,
IWebHostEnvironment environment)
{
2022-09-06 07:54:48 +03:00
optionsBuilder
.UseMySql(configuration.GetConnectionString("DockerMySQL"), GetMySqlServerVersion(8, 0, 11))
.UseMySql(GetMySqlServerVersion(8, 0, 11),
builder =>
{
builder.EnableRetryOnFailure(6, TimeSpan.FromSeconds(3), null);
});
2022-08-20 05:47:32 +03:00
2022-08-19 06:18:50 +03:00
// The following three options help with debugging, but should
// be changed or removed for production.
2022-08-19 19:47:35 +03:00
if (environment.IsDevelopment())
{
optionsBuilder
2022-08-19 19:47:35 +03:00
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
2022-08-20 05:47:32 +03:00
return optionsBuilder;
2022-08-19 19:47:35 +03:00
}
/// <summary>
/// Setup database Contexts
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="configuration"></param>
2022-08-20 05:47:32 +03:00
/// <param name="environment"></param>
2022-08-19 19:47:35 +03:00
/// <returns></returns>
2022-09-06 07:54:48 +03:00
public static IServiceCollection PrepareMySQLDatabasePool(this IServiceCollection serviceCollection,
2022-08-19 19:47:35 +03:00
IConfiguration configuration, IWebHostEnvironment environment)
{
return serviceCollection
.AddDbContextPool<UserDbContext>(options => options.GetMySqlSettings(configuration, environment))
2022-08-20 05:47:32 +03:00
.AddDbContextPool<CountryDbContext>(options => options.GetMySqlSettings(configuration, environment))
2022-08-22 00:14:50 +03:00
.AddDbContextPool<FinanceDbContext>(options => options.GetMySqlSettings(configuration, environment))
.AddDbContextPool<CompanyDbContext>(options => options.GetMySqlSettings(configuration, environment));
2022-08-19 19:47:35 +03:00
}
2022-09-06 07:54:48 +03:00
/// <summary>
/// Setup database Contexts
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="configuration"></param>
/// <param name="environment"></param>
/// <returns></returns>
public static IServiceCollection PrepareSQLLiteDatabasePool(this IServiceCollection serviceCollection,
IConfiguration configuration, IWebHostEnvironment environment)
{
return serviceCollection
.AddDbContextPool<UserDbContext>(options => options.UseSqlite("Data Source=BlueWest.Api.db"))
.AddDbContextPool<CountryDbContext>(options => options.UseSqlite("Data Source=BlueWest.Api.db"))
.AddDbContextPool<FinanceDbContext>(options => options.UseSqlite("Data Source=BlueWest.Api.db"))
.AddDbContextPool<CompanyDbContext>(options => options.UseSqlite("Data Source=BlueWest.Api.db"));
}
2022-08-13 06:35:36 +03:00
}
}