Refactor code structure for improved readability and maintainability

This commit is contained in:
Din
2025-08-08 12:26:54 +08:00
parent eceec1b72d
commit bb7c4c3d0e
11 changed files with 77 additions and 40 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ namespace ResourceMonitorService
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder =>
{ {
webBuilder.UseStartup<Startup>(); webBuilder.UseStartup<Startup>();
webBuilder.UseUrls("http://localhost:5000", "https://localhost:5001"); // URLs are now configured via appsettings.{Environment}.json files
}) })
.ConfigureServices((hostContext, services) => .ConfigureServices((hostContext, services) =>
{ {
-1
View File
@@ -4,7 +4,6 @@
"ResourceMonitorService": { "ResourceMonitorService": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"applicationUrl": "http://localhost:5000;https://localhost:5001",
"environmentVariables": { "environmentVariables": {
"DOTNET_ENVIRONMENT": "Development" "DOTNET_ENVIRONMENT": "Development"
} }
+34 -12
View File
@@ -4,7 +4,11 @@ A comprehensive system monitoring service with a modern web dashboard for real-t
## 🌟 New: Web Dashboard ## 🌟 New: Web Dashboard
Access the interactive web dashboard at `http://localhost:5000` featuring: Access the interactive web dashboard:
- **Development**: `http://localhost:5000`
- **Release/Production**: `http://localhost:24142`
Features:
- **Real-time Monitoring**: Live updates every 15 seconds via SignalR - **Real-time Monitoring**: Live updates every 15 seconds via SignalR
- **Responsive Design**: Mobile-friendly interface built with Tailwind CSS - **Responsive Design**: Mobile-friendly interface built with Tailwind CSS
@@ -90,14 +94,22 @@ For security reasons, the system control feature is hidden by default. To access
## 📡 API Endpoints ## 📡 API Endpoints
The service runs a web server on `http://localhost:5000` providing: The service runs a web server providing:
- **Development**: `http://localhost:5000`
- **Release/Production**: `http://localhost:24142`
Both environments provide:
### Web Interface ### Web Interface
- `GET /` - **Main Dashboard** - Interactive web interface for monitoring - `GET /` - **Main Dashboard** - Interactive web interface for monitoring
- `GET /swagger` - **API Documentation** - Interactive API explorer and documentation - `GET /swagger` - **API Documentation** - Interactive API explorer and documentation
### REST API ### REST API
All API endpoints are available at `http://localhost:5000/api/[endpoint]`: All API endpoints are available at:
- **Development**: `http://localhost:5000/api/[endpoint]`
- **Release/Production**: `http://localhost:24142/api/[endpoint]`
Endpoints:
### System Information ### System Information
- `GET /api/resource/usage` - **Complete resource overview** - All monitoring data in one call - `GET /api/resource/usage` - **Complete resource overview** - All monitoring data in one call
@@ -149,7 +161,11 @@ All API endpoints are available at `http://localhost:5000/api/[endpoint]`:
cd C:\Work\DEV\ResourceUsageAPI cd C:\Work\DEV\ResourceUsageAPI
dotnet run --configuration Release dotnet run --configuration Release
``` ```
Then open your browser to `http://localhost:5000` for the interactive dashboard. Then open your browser to:
- Development: `http://localhost:5000`
- Release/Production: `http://localhost:24142`
for the interactive dashboard.
### Option 2: Windows Service (Production) ### Option 2: Windows Service (Production)
```powershell ```powershell
@@ -221,8 +237,10 @@ For development and testing:
# Run in development mode with hot reload # Run in development mode with hot reload
dotnet run --environment Development dotnet run --environment Development
# Access the dashboard at http://localhost:5000 # Access the dashboard at:
# Access Swagger API documentation at http://localhost:5000/swagger # - Development: http://localhost:5000
# - Release/Production: http://localhost:24142
# Access Swagger API documentation at the same URL + /swagger
``` ```
### Troubleshooting ### Troubleshooting
@@ -230,7 +248,7 @@ dotnet run --environment Development
- **Service won't start**: Check the logs in the `logs/` directory - **Service won't start**: Check the logs in the `logs/` directory
- **No GPU data**: Make sure you have an NVIDIA GPU and drivers installed - **No GPU data**: Make sure you have an NVIDIA GPU and drivers installed
- **High CPU usage**: Adjust monitoring intervals in `appsettings.json` - **High CPU usage**: Adjust monitoring intervals in `appsettings.json`
- **Web dashboard not accessible**: Verify firewall settings and ensure port 5000 is available - **Web dashboard not accessible**: Verify firewall settings and ensure the appropriate port is available (5000 for development, 24142 for release/production)
- **Game detection issues**: Check if games are running from standard installation directories - **Game detection issues**: Check if games are running from standard installation directories
- **API errors**: Verify endpoints using Swagger documentation at `/swagger` - **API errors**: Verify endpoints using Swagger documentation at `/swagger`
- **Performance issues**: Consider increasing `UpdateIntervalMs` in configuration - **Performance issues**: Consider increasing `UpdateIntervalMs` in configuration
@@ -468,23 +486,27 @@ The service supports real-time alert notifications via Telegram bot. To set up T
## 🔧 PowerShell Usage Examples ## 🔧 PowerShell Usage Examples
```powershell ```powershell
# Access the web dashboard # Access the web dashboard (adjust port based on environment)
# Development:
Start-Process "http://localhost:5000" Start-Process "http://localhost:5000"
# Release/Production:
Start-Process "http://localhost:24142"
# Get complete resource overview # Get complete resource overview (adjust URL as needed)
$resources = Invoke-RestMethod -Uri "http://localhost:5000/api/resource/usage" $baseUrl = "http://localhost:24142" # Use 5000 for development
$resources = Invoke-RestMethod -Uri "$baseUrl/api/resource/usage"
Write-Host "CPU: $($resources.cpu.usage.ToString('F1'))%" Write-Host "CPU: $($resources.cpu.usage.ToString('F1'))%"
Write-Host "Memory: $($resources.memory.usagePercentage.ToString('F1'))%" Write-Host "Memory: $($resources.memory.usagePercentage.ToString('F1'))%"
Write-Host "GPU: $($resources.gpu.usage)%" Write-Host "GPU: $($resources.gpu.usage)%"
# Get system information # Get system information
$systemInfo = Invoke-RestMethod -Uri "http://localhost:5000/api/resource/system-info" $systemInfo = Invoke-RestMethod -Uri "$baseUrl/api/resource/system-info"
Write-Host "Machine: $($systemInfo.machineName)" Write-Host "Machine: $($systemInfo.machineName)"
Write-Host "OS: $($systemInfo.osVersion)" Write-Host "OS: $($systemInfo.osVersion)"
Write-Host "CPU: $($systemInfo.cpuName)" Write-Host "CPU: $($systemInfo.cpuName)"
# Get disk usage # Get disk usage
$disks = Invoke-RestMethod -Uri "http://localhost:5000/api/resource/disks" $disks = Invoke-RestMethod -Uri "$baseUrl/api/resource/disks"
foreach ($disk in $disks) { foreach ($disk in $disks) {
$freeGB = [math]::Round($disk.freeSpace / 1GB, 1) $freeGB = [math]::Round($disk.freeSpace / 1GB, 1)
$totalGB = [math]::Round($disk.totalSize / 1GB, 1) $totalGB = [math]::Round($disk.totalSize / 1GB, 1)
+10
View File
@@ -4,5 +4,15 @@
"Default": "Information", "Default": "Information",
"Microsoft.Hosting.Lifetime": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }
},
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://*:5000"
},
"Https": {
"Url": "https://*:5001"
}
}
} }
} }
+15
View File
@@ -0,0 +1,15 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://*:24142"
}
}
}
}
-10
View File
@@ -9,16 +9,6 @@
} }
}, },
"RunAsWindowsService": true, "RunAsWindowsService": true,
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://*:5000"
},
"Https": {
"Url": "https://*:5001"
}
}
},
"ApiSettings": { "ApiSettings": {
"ApiKey": "b7f3e8a1-4c2d-4d9f-9a6e-2a1c5d7f8e9a", "ApiKey": "b7f3e8a1-4c2d-4d9f-9a6e-2a1c5d7f8e9a",
"RequireApiKey": false, "RequireApiKey": false,
+3 -2
View File
@@ -23,6 +23,7 @@ Start-Sleep -Seconds 2
Copy-Item "install-service.ps1" $TEMP_PATH Copy-Item "install-service.ps1" $TEMP_PATH
Copy-Item "start-service.bat" $TEMP_PATH Copy-Item "start-service.bat" $TEMP_PATH
Copy-Item "appsettings.json" $TEMP_PATH Copy-Item "appsettings.json" $TEMP_PATH
Copy-Item "appsettings.Production.json" $TEMP_PATH -ErrorAction SilentlyContinue
Copy-Item "README.md" $TEMP_PATH -ErrorAction SilentlyContinue Copy-Item "README.md" $TEMP_PATH -ErrorAction SilentlyContinue
# Create deployment readme # Create deployment readme
@@ -35,8 +36,8 @@ Copy-Item "README.md" $TEMP_PATH -ErrorAction SilentlyContinue
3. Execute: .\install-service.ps1 3. Execute: .\install-service.ps1
## Access ## Access
- Web Dashboard: http://localhost:5000 - Web Dashboard: http://localhost:24142
- API Health: http://localhost:5000/api/health - API Health: http://localhost:24142/api/health
Generated: $(Get-Date) Generated: $(Get-Date)
"@ | Out-File "$TEMP_PATH\DEPLOYMENT.txt" -Encoding UTF8 "@ | Out-File "$TEMP_PATH\DEPLOYMENT.txt" -Encoding UTF8
+11 -11
View File
@@ -141,12 +141,12 @@ try {
# Remove old rule if it exists # Remove old rule if it exists
Remove-NetFirewallRule -DisplayName "Resource Monitor Service" -ErrorAction SilentlyContinue Remove-NetFirewallRule -DisplayName "Resource Monitor Service" -ErrorAction SilentlyContinue
# Create new rule for port 5000 (web dashboard) # Create new rule for port 24142 (web dashboard)
New-NetFirewallRule -DisplayName "Resource Monitor Service" -Direction Inbound -Protocol TCP -LocalPort 5000 -Action Allow -Profile Any -ErrorAction Stop New-NetFirewallRule -DisplayName "Resource Monitor Service" -Direction Inbound -Protocol TCP -LocalPort 24142 -Action Allow -Profile Any -ErrorAction Stop
Write-Host "Firewall rule created for web dashboard (port 5000)" -ForegroundColor Green Write-Host "Firewall rule created for web dashboard (port 24142)" -ForegroundColor Green
} catch { } catch {
Write-Host "WARNING: Failed to create firewall rule. You may need to configure manually." -ForegroundColor Yellow Write-Host "WARNING: Failed to create firewall rule. You may need to configure manually." -ForegroundColor Yellow
Write-Host "Manual command: New-NetFirewallRule -DisplayName 'Resource Monitor Service' -Direction Inbound -Protocol TCP -LocalPort 5000 -Action Allow" -ForegroundColor Gray Write-Host "Manual command: New-NetFirewallRule -DisplayName 'Resource Monitor Service' -Direction Inbound -Protocol TCP -LocalPort 24142 -Action Allow" -ForegroundColor Gray
} }
# Start the service # Start the service
@@ -169,9 +169,9 @@ Write-Host
Write-Host "=== Installation Complete ===" -ForegroundColor Cyan Write-Host "=== Installation Complete ===" -ForegroundColor Cyan
Write-Host "Service Name: $SERVICE_NAME" -ForegroundColor White Write-Host "Service Name: $SERVICE_NAME" -ForegroundColor White
Write-Host "Installation Path: $INSTALL_PATH" -ForegroundColor White Write-Host "Installation Path: $INSTALL_PATH" -ForegroundColor White
Write-Host "Web Dashboard: http://localhost:5000" -ForegroundColor Yellow Write-Host "Web Dashboard: http://localhost:24142" -ForegroundColor Yellow
Write-Host "API Documentation: http://localhost:5000/swagger" -ForegroundColor Yellow Write-Host "API Documentation: http://localhost:24142/swagger" -ForegroundColor Yellow
Write-Host "API Health Check: http://localhost:5000/api/health" -ForegroundColor White Write-Host "API Health Check: http://localhost:24142/api/health" -ForegroundColor White
Write-Host Write-Host
Write-Host "The service is now running and will start automatically with Windows." -ForegroundColor Green Write-Host "The service is now running and will start automatically with Windows." -ForegroundColor Green
Write-Host "You can manage it through Services.msc or using PowerShell commands:" -ForegroundColor White Write-Host "You can manage it through Services.msc or using PowerShell commands:" -ForegroundColor White
@@ -187,15 +187,15 @@ Write-Host
Write-Host "Testing web dashboard..." -ForegroundColor Yellow Write-Host "Testing web dashboard..." -ForegroundColor Yellow
Start-Sleep -Seconds 5 Start-Sleep -Seconds 5
try { try {
$response = Invoke-RestMethod -Uri "http://localhost:5000/api/health" -TimeoutSec 10 $response = Invoke-RestMethod -Uri "http://localhost:24142/api/health" -TimeoutSec 10
Write-Host "Web Dashboard Test: SUCCESS" -ForegroundColor Green Write-Host "Web Dashboard Test: SUCCESS" -ForegroundColor Green
Write-Host "Service Status: $($response.status)" -ForegroundColor White Write-Host "Service Status: $($response.status)" -ForegroundColor White
Write-Host "Service Uptime: $($response.uptime)" -ForegroundColor White Write-Host "Service Uptime: $($response.uptime)" -ForegroundColor White
Write-Host Write-Host
Write-Host "🎉 Web Dashboard is ready at: http://localhost:5000" -ForegroundColor Green Write-Host "Web Dashboard is ready at: http://localhost:24142" -ForegroundColor Green
Write-Host "📖 API Documentation at: http://localhost:5000/swagger" -ForegroundColor Green Write-Host "API Documentation at: http://localhost:24142/swagger" -ForegroundColor Green
} catch { } catch {
Write-Host "Web Dashboard Test: FAILED" -ForegroundColor Red Write-Host "Web Dashboard Test: FAILED" -ForegroundColor Red
Write-Host "The service may still be starting up. Wait a few minutes and try accessing:" -ForegroundColor Yellow Write-Host "The service may still be starting up. Wait a few minutes and try accessing:" -ForegroundColor Yellow
Write-Host "http://localhost:5000" -ForegroundColor White Write-Host "http://localhost:24142" -ForegroundColor White
} }
+3 -3
View File
@@ -6,8 +6,8 @@ echo.
echo This service will monitor your VM's resources and provide a REST API echo This service will monitor your VM's resources and provide a REST API
echo for remote monitoring from your Unraid server. echo for remote monitoring from your Unraid server.
echo. echo.
echo Service will be available at: http://localhost:5000 echo Service will be available at: http://localhost:24142
echo API Documentation: http://localhost:5000/api/health echo API Documentation: http://localhost:24142/api/health
echo. echo.
echo Press Ctrl+C to stop the service echo Press Ctrl+C to stop the service
echo. echo.
@@ -34,7 +34,7 @@ if errorlevel 1 (
) )
echo. echo.
echo Starting service on http://localhost:5000 echo Starting service on http://localhost:24142
echo. echo.
dotnet run --configuration Release dotnet run --configuration Release