diff --git a/Hub/Service/Controllers/ValuesController.cs b/Hub/Service/Controllers/ValuesController.cs deleted file mode 100644 index 2696a48..0000000 --- a/Hub/Service/Controllers/ValuesController.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using System.Collections.Generic; - -namespace ChrisKaczor.HomeMonitor.Hub.Service.Controllers -{ - [Route("[controller]")] - [ApiController] - public class ValuesController : ControllerBase - { - // GET values - [HttpGet] - public ActionResult> Get() - { - return new[] { "value1", "value2" }; - } - } -} diff --git a/Hub/Service/Dockerfile b/Hub/Service/Dockerfile index 1b83a58..ff141d2 100644 --- a/Hub/Service/Dockerfile +++ b/Hub/Service/Dockerfile @@ -1,8 +1,8 @@ -FROM mcr.microsoft.com/dotnet/aspnet:5.0-alpine AS base +FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS base WORKDIR /app -EXPOSE 80 +EXPOSE 8080 -FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build +FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build WORKDIR /src COPY ["./Service.csproj", "./"] RUN dotnet restore "Service.csproj" diff --git a/Hub/Service/Hubs/DeviceStatusHub.cs b/Hub/Service/Hubs/DeviceStatusHub.cs index 0e0f7d2..4300b7a 100644 --- a/Hub/Service/Hubs/DeviceStatusHub.cs +++ b/Hub/Service/Hubs/DeviceStatusHub.cs @@ -1,17 +1,17 @@ using JetBrains.Annotations; using Microsoft.AspNetCore.SignalR; -using System; +using Microsoft.Extensions.Logging; using System.Threading.Tasks; namespace ChrisKaczor.HomeMonitor.Hub.Service.Hubs { [UsedImplicitly] - public class DeviceStatusHub : Microsoft.AspNetCore.SignalR.Hub + public class DeviceStatusHub(ILogger logger) : Microsoft.AspNetCore.SignalR.Hub { [UsedImplicitly] public async Task RequestLatestStatus() { - Console.WriteLine("RequestLatestStatus"); + logger.LogInformation("RequestLatestStatus"); await Clients.Others.SendAsync("RequestLatestStatus"); } @@ -19,7 +19,7 @@ namespace ChrisKaczor.HomeMonitor.Hub.Service.Hubs [UsedImplicitly] public async Task SendLatestStatus(string message) { - Console.WriteLine($"LatestStatus: {message}"); + logger.LogInformation($"LatestStatus: {message}"); await Clients.Others.SendAsync("LatestStatus", message); } diff --git a/Hub/Service/Hubs/EnvironmentHub.cs b/Hub/Service/Hubs/EnvironmentHub.cs index 9dc86e9..d87767d 100644 --- a/Hub/Service/Hubs/EnvironmentHub.cs +++ b/Hub/Service/Hubs/EnvironmentHub.cs @@ -1,17 +1,17 @@ using JetBrains.Annotations; using Microsoft.AspNetCore.SignalR; -using System; +using Microsoft.Extensions.Logging; using System.Threading.Tasks; namespace ChrisKaczor.HomeMonitor.Hub.Service.Hubs { [UsedImplicitly] - public class EnvironmentHub : Microsoft.AspNetCore.SignalR.Hub + public class EnvironmentHub(ILogger logger) : Microsoft.AspNetCore.SignalR.Hub { [UsedImplicitly] public async Task SendMessage(string message) { - Console.WriteLine(message); + logger.LogInformation(message); await Clients.Others.SendAsync("Message", message); } diff --git a/Hub/Service/Hubs/PowerHub.cs b/Hub/Service/Hubs/PowerHub.cs index 9f3d425..5fb55f4 100644 --- a/Hub/Service/Hubs/PowerHub.cs +++ b/Hub/Service/Hubs/PowerHub.cs @@ -1,17 +1,17 @@ using JetBrains.Annotations; using Microsoft.AspNetCore.SignalR; -using System; +using Microsoft.Extensions.Logging; using System.Threading.Tasks; namespace ChrisKaczor.HomeMonitor.Hub.Service.Hubs { [UsedImplicitly] - public class PowerHub : Microsoft.AspNetCore.SignalR.Hub + public class PowerHub(ILogger logger) : Microsoft.AspNetCore.SignalR.Hub { [UsedImplicitly] public async Task SendLatestSample(string message) { - Console.WriteLine(message); + logger.LogInformation(message); await Clients.Others.SendAsync("LatestSample", message); } diff --git a/Hub/Service/Hubs/WeatherHub.cs b/Hub/Service/Hubs/WeatherHub.cs index 4d75d1d..0fd6c84 100644 --- a/Hub/Service/Hubs/WeatherHub.cs +++ b/Hub/Service/Hubs/WeatherHub.cs @@ -1,17 +1,17 @@ using JetBrains.Annotations; using Microsoft.AspNetCore.SignalR; -using System; +using Microsoft.Extensions.Logging; using System.Threading.Tasks; namespace ChrisKaczor.HomeMonitor.Hub.Service.Hubs { [UsedImplicitly] - public class WeatherHub : Microsoft.AspNetCore.SignalR.Hub + public class WeatherHub(ILogger logger) : Microsoft.AspNetCore.SignalR.Hub { [UsedImplicitly] public async Task SendLatestReading(string message) { - Console.WriteLine(message); + logger.LogInformation(message); await Clients.Others.SendAsync("LatestReading", message); } diff --git a/Hub/Service/Program.cs b/Hub/Service/Program.cs index d475899..b867d22 100644 --- a/Hub/Service/Program.cs +++ b/Hub/Service/Program.cs @@ -1,18 +1,43 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using ChrisKaczor.Common.OpenTelemetry; +using ChrisKaczor.HomeMonitor.Hub.Service.Hubs; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using System.Reflection; -namespace ChrisKaczor.HomeMonitor.Hub.Service +namespace ChrisKaczor.HomeMonitor.Hub.Service; + +public static class Program { - public static class Program + public static void Main(string[] args) { - public static void Main(string[] args) - { - CreateWebHostBuilder(args).Build().Run(); - } + var builder = WebApplication.CreateBuilder(args); - private static IWebHostBuilder CreateWebHostBuilder(string[] args) - { - return WebHost.CreateDefaultBuilder(args).UseStartup(); - } + builder.Configuration.AddEnvironmentVariables(); + + builder.Services.AddCommonOpenTelemetry(Assembly.GetExecutingAssembly().GetName().Name, builder.Configuration["Telemetry:Endpoint"]); + + builder.Services.AddCors(o => o.AddPolicy("CorsPolicy", corsPolicyBuilder => corsPolicyBuilder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("http://localhost:4200"))); + + builder.Services.AddSignalR().AddJsonProtocol(options => options.PayloadSerializerOptions.WriteIndented = false); + + // --- + + var app = builder.Build(); + + if (builder.Environment.IsDevelopment()) + app.UseDeveloperExceptionPage(); + + app.UseCors("CorsPolicy"); + + app.UseRouting(); + + app.MapHub("/weather"); + app.MapHub("/power"); + app.MapHub("/device-status"); + app.MapHub("/environment"); + + app.Run(); } } \ No newline at end of file diff --git a/Hub/Service/Properties/launchSettings.json b/Hub/Service/Properties/launchSettings.json index 61dbd97..09aa53b 100644 --- a/Hub/Service/Properties/launchSettings.json +++ b/Hub/Service/Properties/launchSettings.json @@ -1,30 +1,12 @@ { - "$schema": "http://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:53872", - "sslPort": 0 + "profiles": { + "Service": { + "commandName": "Project", + "launchBrowser": false, + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } } - }, - "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "values", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "Service": { - "commandName": "Project", - "launchBrowser": true, - "launchUrl": "values", - "applicationUrl": "http://localhost:5000", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} \ No newline at end of file +} diff --git a/Hub/Service/Service.csproj b/Hub/Service/Service.csproj index 20bd52a..f31df7a 100644 --- a/Hub/Service/Service.csproj +++ b/Hub/Service/Service.csproj @@ -1,7 +1,7 @@  - net5.0 + net8.0 InProcess ChrisKaczor.HomeMonitor.Hub.Service ChrisKaczor.HomeMonitor.Hub.Service @@ -10,7 +10,8 @@ - - + + + diff --git a/Hub/Service/Startup.cs b/Hub/Service/Startup.cs deleted file mode 100644 index 8b69e99..0000000 --- a/Hub/Service/Startup.cs +++ /dev/null @@ -1,42 +0,0 @@ -using ChrisKaczor.HomeMonitor.Hub.Service.Hubs; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace ChrisKaczor.HomeMonitor.Hub.Service -{ - public class Startup - { - public void ConfigureServices(IServiceCollection services) - { - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); - - services.AddCors(o => o.AddPolicy("CorsPolicy", builder => builder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("http://localhost:4200"))); - - services.AddSignalR().AddJsonProtocol(options => options.PayloadSerializerOptions.WriteIndented = false); - } - - public void Configure(IApplicationBuilder applicationBuilder, IWebHostEnvironment environment) - { - if (environment.IsDevelopment()) - { - applicationBuilder.UseDeveloperExceptionPage(); - } - - applicationBuilder.UseCors("CorsPolicy"); - - applicationBuilder.UseRouting(); - - applicationBuilder.UseEndpoints(endpoints => - { - endpoints.MapHub("/weather"); - endpoints.MapHub("/power"); - endpoints.MapHub("/device-status"); - endpoints.MapHub("/environment"); - endpoints.MapDefaultControllerRoute(); - }); - } - } -} \ No newline at end of file diff --git a/Hub/Service/appsettings.json b/Hub/Service/appsettings.json index def9159..1366cb0 100644 --- a/Hub/Service/appsettings.json +++ b/Hub/Service/appsettings.json @@ -1,8 +1,12 @@ { - "Logging": { - "LogLevel": { - "Default": "Warning" + "Logging": { + "LogLevel": { + "Default": "Warning", + "Microsoft": "Information" + } + }, + "AllowedHosts": "*", + "Telemetry": { + "Endpoint": "http://signoz-otel-collector.platform:4317/" } - }, - "AllowedHosts": "*" } diff --git a/Hub/Service/deploy/manifest.yaml b/Hub/Service/deploy/manifest.yaml index e90c3b0..ff3984c 100644 --- a/Hub/Service/deploy/manifest.yaml +++ b/Hub/Service/deploy/manifest.yaml @@ -38,7 +38,7 @@ metadata: spec: ports: - name: client - port: 80 + port: 8080 selector: app: hub-service type: ClusterIP @@ -62,7 +62,7 @@ spec: - kind: Service name: hub-service namespace: home-monitor - port: 80 + port: 8080 --- apiVersion: traefik.containo.us/v1alpha1 kind: Middleware