forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
web api
This commit is contained in:
12
MagicCarpetProject/MagicCarpetWebApi/AuthOptions.cs
Normal file
12
MagicCarpetProject/MagicCarpetWebApi/AuthOptions.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
|
||||
namespace MagicCarpetWebApi;
|
||||
|
||||
public class AuthOptions
|
||||
{
|
||||
public const string ISSUER = "MagicCarpet_AuthServer"; // издатель токена
|
||||
public const string AUDIENCE = "MagicCarpetl_AuthClient"; // потребитель токена
|
||||
const string KEY = "catsecret_secretsecretsecretkey!123"; // ключ для шифрации
|
||||
public static SymmetricSecurityKey GetSymmetricSecurityKey() => new(Encoding.UTF8.GetBytes(KEY));
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace MagicCarpetWebApi.Infrastructure;
|
||||
|
||||
public class ConfigurationDatabase(IConfiguration configuration) : IConfigurationDatabase
|
||||
{
|
||||
private readonly Lazy<DataBaseSettings> _dataBaseSettings = new(() =>
|
||||
{
|
||||
return configuration.GetValue<DataBaseSettings>("DataBaseSettings") ?? throw new InvalidDataException(nameof(DataBaseSettings));
|
||||
});
|
||||
|
||||
public string ConnectionString => _dataBaseSettings.Value.ConnectionString;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace MagicCarpetWebApi.Infrastructure;
|
||||
|
||||
public class DataBaseSettings
|
||||
{
|
||||
public required string ConnectionString { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="9.0.3" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.7.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MagicCarpetBusinessLogic\MagicCarpetBusinessLogic.csproj" />
|
||||
<ProjectReference Include="..\MagicCarpetContracts\MagicCarpetContracts.csproj" />
|
||||
<ProjectReference Include="..\MagicCarpetDatabase\MagicCarpetDatabase.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="MagicCarpetTests" />
|
||||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,6 @@
|
||||
@MagicCarpetWebApi_HostAddress = http://localhost:5235
|
||||
|
||||
GET {{MagicCarpetWebApi_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
111
MagicCarpetProject/MagicCarpetWebApi/Program.cs
Normal file
111
MagicCarpetProject/MagicCarpetWebApi/Program.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using MagicCarpetBusinessLogic.Implementations;
|
||||
using MagicCarpetContracts.AdapterContracts;
|
||||
using MagicCarpetContracts.BuisnessLogicContracts;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase;
|
||||
using MagicCarpetDatabase.Implementations;
|
||||
using MagicCarpetWebApi;
|
||||
using MagicCarpetWebApi.Adapters;
|
||||
using MagicCarpetWebApi.Infrastructure;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Serilog;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
using var loggerFactory = new LoggerFactory();
|
||||
loggerFactory.AddSerilog(new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger());
|
||||
builder.Services.AddSingleton(loggerFactory.CreateLogger("Any"));
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
|
||||
ValidateIssuer = true,
|
||||
|
||||
ValidIssuer = AuthOptions.ISSUER,
|
||||
|
||||
ValidateAudience = true,
|
||||
|
||||
ValidAudience = AuthOptions.AUDIENCE,
|
||||
|
||||
ValidateLifetime = true,
|
||||
|
||||
IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(),
|
||||
|
||||
ValidateIssuerSigningKey = true,
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddSingleton<IConfigurationDatabase, ConfigurationDatabase>();
|
||||
|
||||
builder.Services.AddTransient<IClientBusinessLogicContract, ClientBusinessLogicContract>();
|
||||
builder.Services.AddTransient<IPostBusinessLogicContract, PostBusinessLogicContract>();
|
||||
builder.Services.AddTransient<ITourBusinessLogicContract, TourBusinessLogicContract>();
|
||||
builder.Services.AddTransient<ISalaryBusinessLogicContract, SalaryBusinessLogicContract>();
|
||||
builder.Services.AddTransient<ISaleBusinessLogicContract, SaleBusinessLogicContract>();
|
||||
builder.Services.AddTransient<IEmployeeBusinessLogicContract, EmployeeBusinessLogicContract>();
|
||||
|
||||
builder.Services.AddTransient<MagicCarpetDbContext>();
|
||||
builder.Services.AddTransient<IClientStorageContract, ClientStorageContract>();
|
||||
builder.Services.AddTransient<IPostStorageContract, PostStorageContract>();
|
||||
builder.Services.AddTransient<ITourStorageContract, TourStorageContract>();
|
||||
builder.Services.AddTransient<ISalaryStorageContract, SalaryStorageContract>();
|
||||
builder.Services.AddTransient<ISaleStorageContract, SaleStorageContract>();
|
||||
builder.Services.AddTransient<IEmployeeStorageContract, EmployeeStorageContract>();
|
||||
|
||||
builder.Services.AddTransient<IClientAdapter, ClientAdapter>();
|
||||
builder.Services.AddTransient<IPostAdapter, PostAdapter>();
|
||||
builder.Services.AddTransient<ITourAdapter, TourAdapter>();
|
||||
builder.Services.AddTransient<ISaleAdapter, SaleAdapter>();
|
||||
builder.Services.AddTransient<IEmployeeAdapter, EmployeeAdapter>();
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
if (app.Environment.IsProduction())
|
||||
{
|
||||
var dbContext = app.Services.GetRequiredService<MagicCarpetDbContext>();
|
||||
if (dbContext.Database.CanConnect())
|
||||
{
|
||||
dbContext.Database.EnsureCreated();
|
||||
dbContext.Database.Migrate();
|
||||
}
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.Map("/login/{username}", (string username) =>
|
||||
{
|
||||
return new JwtSecurityTokenHandler().WriteToken(new JwtSecurityToken(
|
||||
issuer: AuthOptions.ISSUER,
|
||||
audience: AuthOptions.AUDIENCE,
|
||||
claims: [new(ClaimTypes.Name, username)],
|
||||
expires: DateTime.UtcNow.Add(TimeSpan.FromMinutes(2)),
|
||||
signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256)));
|
||||
});
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:1754",
|
||||
"sslPort": 44390
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5235",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7030;http://localhost:5235",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
MagicCarpetProject/MagicCarpetWebApi/WeatherForecast.cs
Normal file
13
MagicCarpetProject/MagicCarpetWebApi/WeatherForecast.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace MagicCarpetWebApi
|
||||
{
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
28
MagicCarpetProject/MagicCarpetWebApi/appsettings.json
Normal file
28
MagicCarpetProject/MagicCarpetWebApi/appsettings.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": {
|
||||
"Default": "Information"
|
||||
},
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "../logs/magiccarpet-.log",
|
||||
"rollingInterval": "Day",
|
||||
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level:u3} {Username} {Message:lj}{Exception}{NewLine}"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
//"DataBaseSettings": {
|
||||
// "ConnectionString": "Host=127.0.0.1;Port=5432;Database=MagicCarpetTest;Username=postgres;Password=postgres;"
|
||||
//}
|
||||
}
|
||||
Reference in New Issue
Block a user