Refactor NVML wrapper and update CORS policy; add appsettings configuration files
This commit is contained in:
@@ -25,7 +25,8 @@ namespace ResourceMonitorService
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowAllOrigins",
|
||||
builder => builder.AllowAnyOrigin()
|
||||
builder => builder
|
||||
.WithOrigins("http://localhost:4200","http://192.168.50.52:4200","http://vmwin11:4200")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod());
|
||||
});
|
||||
@@ -42,17 +43,17 @@ namespace ResourceMonitorService
|
||||
// and compares it with the expected API key from appsettings.json.
|
||||
// If the API key is missing or invalid, it returns a 401 Unauthorized response.
|
||||
//
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
if (!context.Request.Headers.TryGetValue("X-API-KEY", out var extractedApiKey) || extractedApiKey != apiKey)
|
||||
{
|
||||
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||||
await context.Response.WriteAsync("Unauthorized: Invalid API Key");
|
||||
return;
|
||||
}
|
||||
/* app.Use(async (context, next) =>
|
||||
{
|
||||
if (!context.Request.Headers.TryGetValue("X-API-KEY", out var extractedApiKey) || extractedApiKey != apiKey)
|
||||
{
|
||||
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||||
await context.Response.WriteAsync("Unauthorized: Invalid API Key");
|
||||
return;
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
await next();
|
||||
}); */
|
||||
// Apply CORS policy to allow all origins
|
||||
app.UseCors("AllowAllOrigins");
|
||||
|
||||
@@ -230,7 +231,26 @@ namespace ResourceMonitorService
|
||||
if (usage > 80)
|
||||
{
|
||||
// Get the current processes and sort them by CPU usage in descending order
|
||||
var processes = Process.GetProcesses().OrderByDescending(p => p.TotalProcessorTime);
|
||||
var processes = Process.GetProcesses()
|
||||
.Select(p =>
|
||||
{
|
||||
try
|
||||
{
|
||||
return new
|
||||
{
|
||||
Process = p,
|
||||
TotalProcessorTime = p.TotalProcessorTime
|
||||
};
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null; // Skip processes that throw exceptions
|
||||
}
|
||||
})
|
||||
.Where(p => p != null)
|
||||
.OrderByDescending(p => p.TotalProcessorTime)
|
||||
.Select(p => p.Process)
|
||||
.ToList();
|
||||
|
||||
// Create a new anonymous type containing the CPU usage, RAM usage, and the top 3 highest CPU-using processes
|
||||
return new
|
||||
@@ -327,7 +347,9 @@ namespace ResourceMonitorService
|
||||
{
|
||||
Usage = utilization.Gpu,
|
||||
Temperature = temperature,
|
||||
FanSpeed = fanSpeed
|
||||
FanSpeed = fanSpeed,
|
||||
IsAvailable = false,
|
||||
Error = ""
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -343,7 +365,7 @@ namespace ResourceMonitorService
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsNvidiaGpuPresent()
|
||||
/* private bool IsNvidiaGpuPresent()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -371,7 +393,7 @@ namespace ResourceMonitorService
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
} */
|
||||
|
||||
private object GetCurrentlyRunningGame()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user