104 lines
3.5 KiB
C#
104 lines
3.5 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using EonaCat.gRPC.Core.Interfaces.Services;
|
|
using EonaCat.gRPC.Repository;
|
|
using EonaCat.gRPC.Repository.Base;
|
|
using EonaCat.gRPC.Repository.DatabaseContext;
|
|
using EonaCat.gRPC.Service;
|
|
using System.Text;
|
|
using EonaCat.LogStack.Extensions;
|
|
using EonaCat.Mapper;
|
|
|
|
namespace EonaCat.gRPC.Api.Helpers;
|
|
|
|
public static class Extension
|
|
{
|
|
public static void AddInfrastructureServices(this WebApplicationBuilder builder)
|
|
{
|
|
RegisterSwagger(builder);
|
|
RegisterLogger(builder);
|
|
RegisterDatabaseContext(builder);
|
|
RegisterAuthentication(builder);
|
|
}
|
|
public static void AddBusinessServices(this WebApplicationBuilder builder)
|
|
{
|
|
RegisterRepositoryDependencies(builder.Services);
|
|
RegisterServiceDependencies(builder);
|
|
}
|
|
|
|
public static void RegisterServiceDependencies(WebApplicationBuilder builder)
|
|
{
|
|
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("Jwt"));
|
|
builder.Services.AddTransient<IUserService, UserService>();
|
|
}
|
|
|
|
private static void RegisterRepositoryDependencies(IServiceCollection services)
|
|
{
|
|
services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
|
|
services.AddScoped<IUserRepository, UserRepository>();
|
|
}
|
|
|
|
private static void RegisterAuthentication(WebApplicationBuilder builder)
|
|
{
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
}).AddJwtBearer(options =>
|
|
{
|
|
options.RequireHttpsMetadata = true;
|
|
options.SaveToken = true;
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(builder.Configuration.GetSection("Jwt").GetSection("Secret").Value)),
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
};
|
|
});
|
|
}
|
|
|
|
private static void RegisterDatabaseContext(WebApplicationBuilder builder)
|
|
{
|
|
builder.Services.AddDbContext<AppDbContext>((provider, options) =>
|
|
{
|
|
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
|
|
});
|
|
}
|
|
|
|
private static void RegisterLogger(WebApplicationBuilder builder)
|
|
{
|
|
builder.AddEonaCatLogging((x) => x
|
|
.AddFlow(new LogStack.Flows.ConsoleFlow())
|
|
.AddFlow(new LogStack.Flows.FileFlow("./logs")));
|
|
}
|
|
|
|
private static void RegisterSwagger(WebApplicationBuilder builder)
|
|
{
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
c.SwaggerDoc("v1", new OpenApiInfo
|
|
{
|
|
Title = "EonaCat gRPC API",
|
|
Version = "v1",
|
|
Description = "EonaCat gRPC API"
|
|
});
|
|
});
|
|
}
|
|
|
|
public static void AppUseSwagger(this WebApplication app)
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(options =>
|
|
{
|
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "EonaCat gRPC API");
|
|
options.RoutePrefix = string.Empty;
|
|
});
|
|
}
|
|
|
|
public static void MapGrpcServices(this WebApplication app)
|
|
{
|
|
app.MapGrpcService<UserHandler>();
|
|
app.MapGrpcService<AuthenticationHandler>();
|
|
}
|
|
} |