Skip to content

Commit

Permalink
fix: update service for smalltalk api
Browse files Browse the repository at this point in the history
  • Loading branch information
live-dev999 committed Apr 10, 2022
1 parent 15a1906 commit 75b0b81
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public class GlobalSetting
{
public string GatewayChatEndpoint { get; set; } = "https://api-smalltalk.o2bus.com";
public static GlobalSetting Instance { get; } = new GlobalSetting();
public string HubConnectionURL { get; set; } = "http://localhost:5000/chathub";
public string HubConnectionURL { get; set; } = "http://localhost:5000/chathub"; //"https://signalr.o2bus.com/chathub";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ public string Message
#region Methods
public override async Task InitializeAsync(IDictionary<string, string> query)
{


await hubConnection.StartAsync();
await hubConnection.InvokeAsync("NewUserAsync","Denis");

await RelaodData();
}

private async Task SendMsgAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "6.0.3",
"commands": [
"dotnet-ef"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging;
using O2NextGen.Sdk.NetCore.Models.smalltalk;
using O2NextGen.SmallTalk.Api.Mappings;
using O2NextGen.SmallTalk.Api.Services;
using O2NextGen.SmallTalk.Business.Models;
using O2NextGen.SmallTalk.Business.Services;
using System.Threading;
Expand All @@ -18,18 +19,20 @@ public class ChatController : ControllerBase

private readonly IHostingEnvironment _environment;
private readonly ILogger<VersionController> _logger;
private readonly ISignalRService _signalRService;
private readonly IChatManager _chatManager;

#endregion


#region Ctors

public ChatController(IHostingEnvironment environment, ILogger<VersionController> logger,
public ChatController(IHostingEnvironment environment, ILogger<VersionController> logger, ISignalRService signalRService,
IChatManager chatManager)
{
_environment = environment;
_logger = logger;
_signalRService = signalRService;
_chatManager = chatManager;
}

Expand Down Expand Up @@ -71,7 +74,7 @@ public async Task<IActionResult> AddAsync(long sessionId, ChatMessage chatMessag
throw new System.ArgumentNullException(nameof(chatMessage));

ChatMessageModel resultSession = await _chatManager.AddMessage(sessionId, chatMessage.ToModel(), ct);

await _signalRService.GetAsync(ct);
return Ok(resultSession.ToViewModel());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<UserSecretsId>3e239bd5-b8d6-455f-8e46-ea4782668914</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..\..</DockerfileContext>
<DockerComposeProjectPath>..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<UserSecretsId>3e239bd5-b8d6-455f-8e46-ea4782668914</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..\..</DockerfileContext>
<DockerComposeProjectPath>..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly">
<Version>2.2.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\SDKs\O2NG.Sdk.NetCore\O2NG.Sdk.NetCore.Models\O2NextGen.Sdk.NetCore.Models.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Business\O2NextGen.SmallTalk.Business.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Data\O2NextGen.SmallTalk.Data.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Impl\O2NextGen.SmallTalk.Impl.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\SDKs\O2NG.Sdk.NetCore\O2NG.Sdk.NetCore.Models\O2NextGen.Sdk.NetCore.Models.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Business\O2NextGen.SmallTalk.Business.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Data\O2NextGen.SmallTalk.Data.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Impl\O2NextGen.SmallTalk.Impl.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace O2NextGen.SmallTalk.Api.Services
{
public class SignalRService: ISignalRService
{
private readonly HttpClient _httpClient;
public SignalRService(HttpClient httpClient)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}

public async Task GetAsync(CancellationToken ct)
{
await _httpClient.GetAsync($"api/chat", ct);
}
}

public interface ISignalRService
{
Task GetAsync(CancellationToken ct);
}
}
27 changes: 26 additions & 1 deletion src/Services/smalltalk/O2NextGen.SmallTalk.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using O2NextGen.SmallTalk.Api.Helpers;
using O2NextGen.SmallTalk.Api.Services;
using Polly;
using System;
using System.Threading.Tasks;

namespace O2NextGen.SmallTalk.Api
Expand All @@ -20,6 +24,7 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddRequiredMvcComponents();
services.AddBusiness();
services.AddApplicationServices(Configuration);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
Expand Down Expand Up @@ -49,5 +54,25 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseMvc();
}
}
}
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services, IConfiguration configuration)
{
//register delegating handlers
// services.AddTransient<HttpClientAuthorizationDelegatingHandler>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

//register http services
services
.AddHttpClient<ISignalRService, SignalRService>("Signal-R", client =>
{
client.BaseAddress = new Uri(configuration.GetValue<string>("urls:SignalRUrl"));
})
.AddTransientHttpErrorPolicy(builder => builder.WaitAndRetryAsync(5, arrempt => TimeSpan.FromSeconds(arrempt * 2)
));

return services;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"Default": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"urls": {
"SignalRUrl": "http://localhost:5000"
}
}

Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using O2NextGen.SmallTalk.SignalrHub.Hubs;
using System.Threading.Tasks;

namespace O2NextGen.SmallTalk.SignalrHub.Controllers
{
[Route("api/chat")]
[ApiController]
public class ChatController : ControllerBase
{
private readonly IChatHub chatHub;
private IHubContext<ChatHub> _hubContext;

public ChatController(IChatHub chatHub)
public ChatController(IHubContext<ChatHub> chatHub)
{
this.chatHub = chatHub;
this._hubContext = chatHub;
}
[HttpGet]
public void Test()
public async Task<IActionResult> Test()
{
chatHub.UpdateMessages();
//await chatHub.UpdateMessages();
await _hubContext.Clients
.Group("Denis")
.SendAsync("OnUpdateMessage");
return Ok("Ok");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;

namespace O2NextGen.SmallTalk.SignalrHub.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}

// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}

// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ public interface IChatHub {
}
public class ChatHub : Hub, IChatHub
{
string username;
public async Task NewUserAsync(string username)
{
await Groups.AddToGroupAsync(Context.ConnectionId, username);
this.username = username;
await base.OnConnectedAsync();
}

public override Task OnConnectedAsync()
{

return base.OnConnectedAsync();
}

public async Task UpdateMessages()
{
await Clients.All.SendAsync("OnUpdateMessage");
await Clients.Group(username).SendAsync("OnUpdateMessage");
//await Groups..All.SendAsync("OnUpdateMessage");
}

//public override async Task OnConnectedAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
<UserSecretsId>bc1a7d3c-820d-4431-a0c7-d51b4a253b6d</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
Expand All @@ -14,7 +14,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using O2NextGen.SmallTalk.SignalrHub.Hubs;

Expand All @@ -11,6 +12,11 @@ public class Startup
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
var mvcBuilder = services.AddMvcCore(options => {
//options.Filters.Add<ApiExceptionFilter>();
});
mvcBuilder.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
mvcBuilder.AddJsonFormatters();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
Expand Down
5 changes: 4 additions & 1 deletion src/Services/smalltalk/O2NextGen.SmallTalk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{64D081A5-7679-4BA4-B1D9-9577379FF16F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "O2NextGen.SmallTalk.Api", "O2NextGen.SmallTalk.Api\O2NextGen.SmallTalk.Api.csproj", "{4D3AF6CE-4262-4F0E-9DAC-86A4781C7D96}"
ProjectSection(ProjectDependencies) = postProject
{03A09C6B-B835-4748-839E-33E395D8197C} = {03A09C6B-B835-4748-839E-33E395D8197C}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntegrationTests.O2NextGen.SmallTalk.Api", "Tests\IntegrationTests.O2NextGen.SmallTalk.Api\IntegrationTests.O2NextGen.SmallTalk.Api.csproj", "{9BBF5376-E08B-4181-9187-2BE4A8787CF4}"
EndProject
Expand All @@ -27,7 +30,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SDKs", "SDKs", "{B03A4DA5-C
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "O2NextGen.Sdk.NetCore.Models", "..\..\SDKs\O2NG.Sdk.NetCore\O2NG.Sdk.NetCore.Models\O2NextGen.Sdk.NetCore.Models.csproj", "{011B24B8-B1CA-4585-9862-916BE9A090AF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "O2NextGen.SmallTalk.SignalrHub", "O2NextGen.SmallTalk.SignalrHub\O2NextGen.SmallTalk.SignalrHub.csproj", "{03A09C6B-B835-4748-839E-33E395D8197C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "O2NextGen.SmallTalk.SignalrHub", "O2NextGen.SmallTalk.SignalrHub\O2NextGen.SmallTalk.SignalrHub.csproj", "{03A09C6B-B835-4748-839E-33E395D8197C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down

0 comments on commit 75b0b81

Please sign in to comment.