init
This commit is contained in:
+83
@@ -0,0 +1,83 @@
|
||||
|
||||
using example_app.Database.Connector;
|
||||
using example_csharp_app;
|
||||
using example_csharp_app.Aggregators;
|
||||
using example_csharp_app.Interfaces;
|
||||
using example_csharp_app.Repositories;
|
||||
using example_csharp_app.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
|
||||
namespace Program
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
private static void SetupDi(IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddCors();
|
||||
|
||||
services.AddScoped<IBookAggregator, BookAggregator>();
|
||||
services.AddScoped<IBookService, BookService>();
|
||||
services.AddScoped<IBookRepository, BookRepository>();
|
||||
services.AddDbContext<AppDbContext>(
|
||||
(provider, builder) =>
|
||||
{
|
||||
//var settings = provider.GetRequiredService<IOptions<DbSettings>>(); // эта версия лучше
|
||||
var connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnection");
|
||||
builder.UseNpgsql(connectionString);
|
||||
},
|
||||
ServiceLifetime.Transient
|
||||
);
|
||||
}
|
||||
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
var configurationBuilder = (new ConfigurationBuilder())
|
||||
.SetBasePath(Environment.CurrentDirectory)
|
||||
.AddJsonFile("appsettings.json", false, true)
|
||||
.AddJsonFile("appsettings.{environmentName}", true, true)
|
||||
.AddEnvironmentVariables()
|
||||
.AddCommandLine(args);
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
var configuration = configurationBuilder.Build();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
SetupDi(builder.Services, configuration);
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
//if(app.Environment.IsDevelopment())
|
||||
//{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
//}
|
||||
app.MapControllers();
|
||||
app.UseMiddleware<ErrorLogMiddleware>();
|
||||
//app.UseHttpsRedirection();
|
||||
|
||||
app.Run();
|
||||
|
||||
} catch (Exception)
|
||||
{
|
||||
// log
|
||||
}
|
||||
finally
|
||||
{
|
||||
// flush log
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user