Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Web
diff options
context:
space:
mode:
Commit6fb05a2a6f94e008dc689291d0522f39f87a6622 (patch)
Author
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-24 14:45:23 +0200
Committer
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-24 14:45:23 +0200
Files845087b1419336f526b68678d92d090c1076a132 /src/Web
pusg-api-6fb05a2a6f94e008dc689291d0522f39f87a6622.tar
pusg-api-6fb05a2a6f94e008dc689291d0522f39f87a6622.tar.gz
pusg-api-6fb05a2a6f94e008dc689291d0522f39f87a6622.zip
Parentd62fcf4e8af0705756bcf159588c0e5eeab1a56d (diff)
Initial Commit - Add all project files
diff --git a/src/Web/PUSG.Web/Controllers/HomeController.cs b/src/Web/PUSG.Web/Controllers/HomeController.cs
new file mode 100644
index 0000000..60cfe2f
--- /dev/null
+++ b/src/Web/PUSG.Web/Controllers/HomeController.cs
@@ -0,0 +1,15 @@
+using Microsoft.AspNetCore.Mvc;
+
+namespace PUSG.Web.Controllers
+{
+ [Route("api/[controller]")]
+ [ApiController]
+ public class HomeController : ControllerBase
+ {
+ [Route("/")]
+ public IActionResult Index()
+ {
+ return Ok();
+ }
+ }
+}
diff --git a/src/Web/PUSG.Web/Hubs/HomeHub.cs b/src/Web/PUSG.Web/Hubs/HomeHub.cs
new file mode 100644
index 0000000..b762635
--- /dev/null
+++ b/src/Web/PUSG.Web/Hubs/HomeHub.cs
@@ -0,0 +1,62 @@
+using Microsoft.AspNetCore.SignalR;
+using PUSG.Web.Static;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace PUSG.Web.Hubs
+{
+ public class HomeHub : Hub
+ {
+ private ISessionService sessionService;
+
+ public HomeHub(ISessionService sessionService)
+ {
+ this.sessionService = sessionService;
+ }
+
+ private async Task BroadcastToLoggedIn(string message, params object[] contents)
+ {
+ List<string> excludedConnections = this.sessionService
+ .LiveConnections
+ .Where(liveConnection => !this.sessionService.IsLogged(liveConnection))
+ .ToList();
+
+ await this.Clients.AllExcept(excludedConnections).SendAsync(message, contents);
+ }
+
+ public async Task SendMessage(string username, string messageContent)
+ {
+ if (this.sessionService.IsLogged(username))
+ {
+ await this.BroadcastToLoggedIn("ReceiveMessage", username, messageContent);
+ }
+ }
+
+ public async Task Login(string username)
+ {
+ if(!this.sessionService.IsLogged(username))
+ {
+ this.sessionService.AddUser(this.Context.ConnectionId, username);
+ }
+ }
+
+ public override async Task OnConnectedAsync()
+ {
+ this.sessionService.LiveConnections.Add(this.Context.ConnectionId);
+
+ await this.BroadcastToLoggedIn("OnConnected", "A new user has connected!");
+
+ await base.OnConnectedAsync();
+ }
+
+ public override async Task OnDisconnectedAsync(Exception exception)
+ {
+ this.sessionService.LiveConnections.Remove(this.Context.ConnectionId);
+ this.sessionService.RemoveUser(this.Context.ConnectionId);
+
+ await base.OnDisconnectedAsync(exception);
+ }
+ }
+}
diff --git a/src/Web/PUSG.Web/PUSG.Web.csproj b/src/Web/PUSG.Web/PUSG.Web.csproj
new file mode 100644
index 0000000..d12c450
--- /dev/null
+++ b/src/Web/PUSG.Web/PUSG.Web.csproj
@@ -0,0 +1,8 @@
+<Project Sdk="Microsoft.NET.Sdk.Web">
+
+ <PropertyGroup>
+ <TargetFramework>netcoreapp3.1</TargetFramework>
+ </PropertyGroup>
+
+
+</Project>
diff --git a/src/Web/PUSG.Web/Program.cs b/src/Web/PUSG.Web/Program.cs
new file mode 100644
index 0000000..0bf82e6
--- /dev/null
+++ b/src/Web/PUSG.Web/Program.cs
@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+
+namespace PUSG.Web
+{
+ public class Program
+ {
+ public static void Main(string[] args)
+ {
+ CreateHostBuilder(args).Build().Run();
+ }
+
+ public static IHostBuilder CreateHostBuilder(string[] args) =>
+ Host.CreateDefaultBuilder(args)
+ .ConfigureWebHostDefaults(webBuilder =>
+ {
+ webBuilder.UseStartup<Startup>();
+ });
+ }
+}
diff --git a/src/Web/PUSG.Web/Properties/launchSettings.json b/src/Web/PUSG.Web/Properties/launchSettings.json
new file mode 100644
index 0000000..ea77118
--- /dev/null
+++ b/src/Web/PUSG.Web/Properties/launchSettings.json
@@ -0,0 +1,30 @@
+{
+ "$schema": "http://json.schemastore.org/launchsettings.json",
+ "iisSettings": {
+ "windowsAuthentication": false,
+ "anonymousAuthentication": true,
+ "iisExpress": {
+ "applicationUrl": "http://localhost:60787",
+ "sslPort": 44385
+ }
+ },
+ "profiles": {
+ "IIS Express": {
+ "commandName": "IISExpress",
+ "launchBrowser": true,
+ "launchUrl": "weatherforecast",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ },
+ "PUSG.Web": {
+ "commandName": "Project",
+ "launchBrowser": true,
+ "launchUrl": "weatherforecast",
+ "applicationUrl": "https://localhost:5001;http://localhost:5000",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ }
+ }
+}
diff --git a/src/Web/PUSG.Web/Startup.cs b/src/Web/PUSG.Web/Startup.cs
new file mode 100644
index 0000000..82ae7e4
--- /dev/null
+++ b/src/Web/PUSG.Web/Startup.cs
@@ -0,0 +1,62 @@
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using PUSG.Web.Hubs;
+using PUSG.Web.Static;
+
+namespace PUSG.Web
+{
+ public class Startup
+ {
+ public Startup(IConfiguration configuration)
+ {
+ Configuration = configuration;
+ }
+
+ public IConfiguration Configuration { get; }
+
+ // This method gets called by the runtime. Use this method to add services to the container.
+ public void ConfigureServices(IServiceCollection services)
+ {
+ services.AddSingleton<ISessionService, SessionService>();
+
+ services.AddControllers();
+ services.AddSignalR();
+
+ services.AddCors(options =>
+ {
+ options.AddPolicy("CustomPolicy", builder =>
+ {
+ builder
+ .WithOrigins("http://localhost:8001")
+ .AllowCredentials()
+ .AllowAnyHeader()
+ .AllowAnyMethod();
+ });
+ });
+ }
+
+ // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
+ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
+ {
+ if (env.IsDevelopment())
+ {
+ app.UseDeveloperExceptionPage();
+ }
+
+ app.UseHttpsRedirection();
+
+ app.UseCors("CustomPolicy");
+
+ app.UseRouting();
+
+ app.UseEndpoints(endpoints =>
+ {
+ endpoints.MapHub<HomeHub>("/home");
+ endpoints.MapControllers();
+ });
+ }
+ }
+}
diff --git a/src/Web/PUSG.Web/Static/ISessionService.cs b/src/Web/PUSG.Web/Static/ISessionService.cs
new file mode 100644
index 0000000..05eae53
--- /dev/null
+++ b/src/Web/PUSG.Web/Static/ISessionService.cs
@@ -0,0 +1,19 @@
+using System.Collections.Generic;
+
+namespace PUSG.Web.Static
+{
+ public interface ISessionService
+ {
+ bool IsLogged(string username);
+
+ void AddUser(string connectionId, string username);
+
+ void RemoveUser(string connectionId);
+
+ Dictionary<string, string> Users { get; }
+
+ List<string> LiveConnections { get; }
+
+ KeyValuePair<string, string> GetUser(string connectionId);
+ }
+}
diff --git a/src/Web/PUSG.Web/Static/SessionService.cs b/src/Web/PUSG.Web/Static/SessionService.cs
new file mode 100644
index 0000000..0e8e61a
--- /dev/null
+++ b/src/Web/PUSG.Web/Static/SessionService.cs
@@ -0,0 +1,40 @@
+using System.Collections.Generic;
+using System.Linq;
+
+namespace PUSG.Web.Static
+{
+ public class SessionService : ISessionService
+ {
+ private Dictionary<string, string> users;
+
+ public SessionService()
+ {
+ this.users = new Dictionary<string, string>();
+ this.LiveConnections = new List<string>();
+ }
+
+ public Dictionary<string, string> Users => this.users;
+
+ public List<string> LiveConnections { get; }
+
+ public void AddUser(string connectionId, string username)
+ {
+ this.users[connectionId] = username;
+ }
+
+ public KeyValuePair<string, string> GetUser(string connectionId)
+ {
+ return this.users.SingleOrDefault(user => user.Key == connectionId);
+ }
+
+ public bool IsLogged(string credential)
+ {
+ return this.users.ContainsKey(credential) || this.users.ContainsValue(credential);
+ }
+
+ public void RemoveUser(string connectionId)
+ {
+ this.users.Remove(connectionId);
+ }
+ }
+}
diff --git a/src/Web/PUSG.Web/appsettings.Development.json b/src/Web/PUSG.Web/appsettings.Development.json
new file mode 100644
index 0000000..8983e0f
--- /dev/null
+++ b/src/Web/PUSG.Web/appsettings.Development.json
@@ -0,0 +1,9 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft": "Warning",
+ "Microsoft.Hosting.Lifetime": "Information"
+ }
+ }
+}
diff --git a/src/Web/PUSG.Web/appsettings.json b/src/Web/PUSG.Web/appsettings.json
new file mode 100644
index 0000000..d9d9a9b
--- /dev/null
+++ b/src/Web/PUSG.Web/appsettings.json
@@ -0,0 +1,10 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft": "Warning",
+ "Microsoft.Hosting.Lifetime": "Information"
+ }
+ },
+ "AllowedHosts": "*"
+}