Skip to content

Commit

Permalink
begin signalr integration
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonRun3s committed Jan 10, 2024
1 parent 26f957c commit fb59dda
Show file tree
Hide file tree
Showing 20 changed files with 3,337 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CWASP Razor Edition/CWASP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.QuickGrid.EntityFrameworkAdapter" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.1" />
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.21.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
13 changes: 13 additions & 0 deletions CWASP Razor Edition/Hubs/indexHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.SignalR;

namespace CWASP_Razor_Edition.Hubs
{
public class IndexHub : Hub
{
public async Task UpdateDatabaseView()
{
// Like a repeater, tell all clients to update their database view.
await Clients.All.SendAsync("UpdateDatabaseView");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)

b.HasKey("Id");

b.ToTable("Ticket");
b.ToTable("Ticket", (string)null);
});
#pragma warning restore 612, 618
}
Expand Down
3 changes: 2 additions & 1 deletion CWASP Razor Edition/Models/SeedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public static void Initialize(IServiceProvider serviceProvider)
DbContextOptions<CWASP_Razor_EditionContext>>());
if (context == null || context.Ticket == null)
{
throw new ArgumentNullException("Null CWASP_Razor_EditionContext");
var paramName = "Null CWASP_Razor_EditionContext";
throw new ArgumentNullException(paramName);
}

// Look for any tickets.
Expand Down
4 changes: 3 additions & 1 deletion CWASP Razor Edition/Pages/Create.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
<h6>@ViewData["Message"]</h6>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" asp-page-handler="Submit" />
<input type="submit" id="createButton" value="Create" class="btn btn-primary" asp-page-handler="Submit" />
<input type="submit" value="Check Duplicates" class="btn btn-primary" asp-page-handler="CheckDuplicates" />
</div>
</form>
<script src="~/js/signalr/dist/browser/signalr.js"></script>
<script src="~/js/Create.js"></script>
</div>
</div>

Expand Down
9 changes: 2 additions & 7 deletions CWASP Razor Edition/Pages/Create.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CWASP_Razor_Edition.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using CWASP_Razor_Edition.Data;
using CWASP_Razor_Edition.Models;
using Microsoft.AspNetCore.SignalR.Client;

namespace CWASP_Razor_Edition.Pages
{
Expand Down
2 changes: 1 addition & 1 deletion CWASP Razor Edition/Pages/Error.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h1 class="text-danger">Error</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
Expand Down
11 changes: 4 additions & 7 deletions CWASP Razor Edition/Pages/Error.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ namespace CWASP_Razor_Edition.Pages
{
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
public class ErrorModel(ILogger<ErrorModel> logger) : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}
#pragma warning disable IDE0052 // Remove unread private members
private readonly ILogger<ErrorModel> _logger = logger;
#pragma warning restore IDE0052 // Remove unread private members

public void OnGet()
{
Expand Down
4 changes: 3 additions & 1 deletion CWASP Razor Edition/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</p>
</form>

<table class="table">
<table class="table" id="DatabaseList">
<thead>
<tr>
<th>
Expand Down Expand Up @@ -70,4 +70,6 @@
</tr>
}
</tbody>
<script src="~/js/signalr/dist/browser/signalr.js"></script>
<script src="~/js/Index.js"></script>
</table>
3 changes: 3 additions & 0 deletions CWASP Razor Edition/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using Microsoft.Extensions.DependencyInjection;
using CWASP_Razor_Edition.Data;
using CWASP_Razor_Edition.Models;
using CWASP_Razor_Edition.Hubs;
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddDbContext<CWASP_Razor_EditionContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("CWASPdbconnection") ?? throw new InvalidOperationException("Connection string 'CWASPdbconnection' not found.")));
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddSignalR().AddAzureSignalR();

var app = builder.Build();

Expand Down Expand Up @@ -37,5 +39,6 @@
app.UseAuthorization();

app.MapRazorPages();
app.MapHub<IndexHub>("/indexHub");

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"resourceGroupName": {
"type": "string",
"defaultValue": "CWASP-AppService",
"metadata": {
"_parameterType": "resourceGroup",
"description": "Name of the resource group for the resource. It is recommended to put resources under same resource group for better tracking."
}
},
"resourceGroupLocation": {
"type": "string",
"defaultValue": "eastus",
"metadata": {
"_parameterType": "location",
"description": "Location of the resource group. Resource groups could have different location than resources."
}
},
"resourceLocation": {
"type": "string",
"defaultValue": "[parameters('resourceGroupLocation')]",
"metadata": {
"_parameterType": "location",
"description": "Location of the resource. By default use resource group's location, unless the resource provider is not supported there."
}
}
},
"resources": [
{
"type": "Microsoft.Resources/resourceGroups",
"name": "[parameters('resourceGroupName')]",
"location": "[parameters('resourceGroupLocation')]",
"apiVersion": "2019-10-01"
},
{
"type": "Microsoft.Resources/deployments",
"name": "[concat(parameters('resourceGroupName'), 'Deployment', uniqueString(concat('CWASP-SignalR-Service', subscription().subscriptionId)))]",
"resourceGroup": "[parameters('resourceGroupName')]",
"apiVersion": "2019-10-01",
"dependsOn": [
"[parameters('resourceGroupName')]"
],
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"sku": {
"name": "Free_F1",
"tier": "Free",
"size": "F1",
"capacity": 1
},
"location": "[parameters('resourceLocation')]",
"name": "CWASP-SignalR-Service",
"type": "Microsoft.SignalRService/SignalR",
"apiVersion": "2018-10-01"
}
]
}
}
}
],
"metadata": {
"_dependencyType": "signalr.azure"
}
}
11 changes: 7 additions & 4 deletions CWASP Razor Edition/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"launchBrowser": true,
"applicationUrl": "http://localhost:5095",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.Azure.SignalR"
}
},
"https": {
Expand All @@ -24,15 +25,17 @@
"launchBrowser": true,
"applicationUrl": "https://localhost:7229;http://localhost:5095",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.Azure.SignalR"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.Azure.SignalR"
}
}
}
}
}
5 changes: 5 additions & 0 deletions CWASP Razor Edition/Properties/serviceDependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"type": "appInsights",
"connectionId": "APPLICATIONINSIGHTS_CONNECTION_STRING",
"dynamicId": null
},
"signalr1": {
"type": "signalr",
"connectionId": "Azure:SignalR:ConnectionString",
"dynamicId": null
}
}
}
8 changes: 8 additions & 0 deletions CWASP Razor Edition/Properties/serviceDependencies.local.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
"type": "appInsights.azure",
"connectionId": "APPLICATIONINSIGHTS_CONNECTION_STRING",
"dynamicId": null
},
"signalr1": {
"serviceConnectorResourceId": "/subscriptions/[parameters('subscriptionId')]/resourceGroups/[parameters('resourceGroupName')]/providers/Microsoft.ServiceLinker/locations/eastus/connectors/AzureSignalRConnectionString_B403A73EB6",
"secretStore": "LocalSecretsFile",
"resourceId": "/subscriptions/[parameters('subscriptionId')]/resourceGroups/[parameters('resourceGroupName')]/providers/Microsoft.SignalRService/SignalR/CWASP-SignalR-Service",
"type": "signalr.azure",
"connectionId": "Azure:SignalR:ConnectionString",
"dynamicId": null
}
}
}
8 changes: 8 additions & 0 deletions CWASP Razor Edition/libman.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
"dist/browser/signalr.js",
"dist/browser/signalr.min.js"
]
},
{
"library": "signalr@2.4.3",
"destination": "wwwroot/lib/signalr/",
"files": [
"jquery.signalR.js",
"jquery.signalR.min.js"
]
}
]
}
12 changes: 12 additions & 0 deletions CWASP Razor Edition/wwwroot/js/Create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/indexHub").build();

document.getElementById("createButton").addEventListener("click", function (event) {
// Tell the SignalR hub to invoke the "RefreshDatabaseView" function.
connection.invoke("RefreshDatabaseView").catch(function (err) {
return console.error(err.toString());
});

console.log("SignalR detected an event.");
});
9 changes: 9 additions & 0 deletions CWASP Razor Edition/wwwroot/js/Index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/indexHub").build();

// Run function when told to by the SignalR hub.
connection.on("RefreshDatabaseView", function () {
// Refresh the page to display updated data.
window.location.reload();
});
4 changes: 0 additions & 4 deletions CWASP Razor Edition/wwwroot/js/site.js

This file was deleted.

Loading

0 comments on commit fb59dda

Please sign in to comment.