Na most működig SWAGGER és 5000 porton publishban is !

This commit is contained in:
2025-01-15 17:26:09 +01:00
parent a2906cc113
commit 6a46975cfd
7 changed files with 55 additions and 28 deletions
+5 -1
View File
@@ -4,16 +4,19 @@
{ {
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
private const string ApiKeyHeaderName = "X-Api-Key"; private const string ApiKeyHeaderName = "X-Api-Key";
private readonly ILogger<ApiKeyMiddleware> _logger;
public ApiKeyMiddleware(RequestDelegate next) public ApiKeyMiddleware(RequestDelegate next, ILogger<ApiKeyMiddleware> logger)
{ {
_next = next; _next = next;
_logger = logger;
} }
public async Task InvokeAsync(HttpContext context) public async Task InvokeAsync(HttpContext context)
{ {
if (!context.Request.Headers.TryGetValue(ApiKeyHeaderName, out var extractedApiKey)) if (!context.Request.Headers.TryGetValue(ApiKeyHeaderName, out var extractedApiKey))
{ {
_logger.LogWarning("API Key is missing.");
context.Response.StatusCode = 401; // Unauthorized context.Response.StatusCode = 401; // Unauthorized
context.Response.ContentType = "text/html; charset=utf-8"; context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.WriteAsync("API Key hiányzik."); await context.Response.WriteAsync("API Key hiányzik.");
@@ -25,6 +28,7 @@
if (!apiKey.Equals(extractedApiKey)) if (!apiKey.Equals(extractedApiKey))
{ {
_logger.LogWarning("Invalid API Key.");
context.Response.StatusCode = 403; // Forbidden context.Response.StatusCode = 403; // Forbidden
context.Response.ContentType = "text/html; charset=utf-8"; context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.WriteAsync("Helytelen API Key."); await context.Response.WriteAsync("Helytelen API Key.");
@@ -10,20 +10,41 @@ namespace WorkFlowCheck.API.Controllers
public class UserController : ControllerBase public class UserController : ControllerBase
{ {
private IUserService _userService; private IUserService _userService;
public UserController(IUserService userService) public UserController(IUserService userService)
{ {
_userService = userService; _userService = userService;
} }
[HttpGet]
public async Task<ApiResponseDTO<UserDTO>> Get() [HttpGet("{id}")]
public async Task<ApiResponseDTO<UserDTO>> Get(int id)
{ {
var userDTO = await _userService.GetUser(1);
var retVal = new ApiResponseDTO<UserDTO>() var retVal = new ApiResponseDTO<UserDTO>()
{ {
IsSuccess = true, IsSuccess = true,
Data = userDTO
}; };
var userDTO = await _userService.GetUser(id);
if (userDTO != null)
{
if (userDTO.Id == 0)
{
retVal.IsSuccess = false;
retVal.Errors.Add("No match!");
retVal.Data = userDTO;
}
else
{
retVal.IsSuccess = true;
retVal.Data = userDTO;
}
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal; return retVal;
} }
} }
+10 -13
View File
@@ -11,18 +11,13 @@ using WorkFlowCheck.BL.Infra;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
Log.Logger = new LoggerConfiguration() Log.Logger = new LoggerConfiguration()
.WriteTo.Console() // Logok írása a konzolra .ReadFrom.Configuration(builder.Configuration)
.WriteTo.File("logs/WorkFlowCheck.log", rollingInterval: RollingInterval.Day) // Logok fájlba
.CreateLogger(); .CreateLogger();
//builder.WebHost.ConfigureKestrel(options =>
//{
// options.ListenAnyIP(5069, listenOptions =>
// {
// listenOptions.UseHttps(Path.Combine(AppContext.BaseDirectory, "Certs", "SwaggerCert.pfx"), "Noispot135!Xy()zz654321");
// });
//});
builder.Services.AddAutoMapper(typeof(MapperProfile)); builder.Services.AddAutoMapper(typeof(MapperProfile));
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
@@ -78,7 +73,10 @@ builder.Services.AddControllers()
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
}); });
var app = builder.Build(); var app = builder.Build();
// Swagger middleware // Swagger middleware
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
@@ -91,11 +89,10 @@ if (app.Environment.IsDevelopment())
// Routing és endpointok // Routing és endpointok
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseAuthorization(); app.UseRouting();
app.MapControllers();
app.UseMiddleware<ApiKeyMiddleware>(); app.UseMiddleware<ApiKeyMiddleware>();
app.MapControllers();
app.UseAuthorization();
app.Run(); app.Run();
@@ -3,7 +3,7 @@
"http": { "http": {
"commandName": "Project", "commandName": "Project",
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "todos", "launchUrl": "swagger",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
}, },
@@ -13,7 +13,7 @@
"IIS Express": { "IIS Express": {
"commandName": "IISExpress", "commandName": "IISExpress",
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "todos", "launchUrl": "swagger",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }
@@ -9,6 +9,7 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>false</InvariantGlobalization> <InvariantGlobalization>false</InvariantGlobalization>
<PublishAot>false</PublishAot> <PublishAot>false</PublishAot>
<IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\WorkFlowCheck.BL\WorkFlowCheck.BL.csproj" /> <ProjectReference Include="..\WorkFlowCheck.BL\WorkFlowCheck.BL.csproj" />
+3 -3
View File
@@ -6,11 +6,11 @@
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Verbose" "Microsoft.AspNetCore": "Information"
} }
}, },
"Serilog": { "Serilog": {
"MinimumLevel": "Verbose", "MinimumLevel": "Information",
"WriteTo": [ "WriteTo": [
{ {
"Name": "Console" "Name": "Console"
@@ -18,7 +18,7 @@
{ {
"Name": "File", "Name": "File",
"Args": { "Args": {
"path": "logs/log-.txt", "path": "logs/log-.log",
"rollingInterval": "Day" "rollingInterval": "Day"
} }
} }
+8 -4
View File
@@ -4,8 +4,12 @@ namespace WorkFlowCheck.BL.DTO
{ {
public class ApiResponseBaseDTO public class ApiResponseBaseDTO
{ {
public ApiResponseBaseDTO()
{
Errors = new List<string>();
}
public bool IsSuccess { get; set; } public bool IsSuccess { get; set; }
public IEnumerable<string> Errors { get; set; } public List<string> Errors { get; set; }
public bool HandleError { get; set; } public bool HandleError { get; set; }
public string Error public string Error
{ {
@@ -16,17 +20,17 @@ namespace WorkFlowCheck.BL.DTO
} }
} }
} }
public class ApiResponseDTO : ApiResponseBaseDTO public class ApiResponseDTO : ApiResponseBaseDTO
{ {
public object Data { get; set; } public object Data { get; set; }
} }
public interface IApiResponseDTO<out T> public interface IApiResponseDTO<out T>
{ {
} }
public class ApiResponseDTO<T> : ApiResponseBaseDTO, IApiResponseDTO<T> public class ApiResponseDTO<T> : ApiResponseBaseDTO, IApiResponseDTO<T>
{ {
public T Data { get; set; } public T Data { get; set; }