diff --git a/Birdmap.API/Dockerfile b/Birdmap.API/Dockerfile index 2763799..5782a3c 100644 --- a/Birdmap.API/Dockerfile +++ b/Birdmap.API/Dockerfile @@ -1,14 +1,13 @@ -#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 -RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - +RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash - RUN apt-get update RUN apt-get install -y nodejs FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build -RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - +RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash - RUN apt-get update RUN apt-get install -y nodejs WORKDIR /src diff --git a/Birdmap.API/Dockerfile.original b/Birdmap.API/Dockerfile.original deleted file mode 100644 index 890c074..0000000 --- a/Birdmap.API/Dockerfile.original +++ /dev/null @@ -1,28 +0,0 @@ -#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. -FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base -WORKDIR /app -EXPOSE 80 -EXPOSE 443 -RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - -RUN apt-get install -y nodejs - -FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build -RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - -RUN apt-get install -y nodejs -WORKDIR /src -COPY ["Birdmap.API/Birdmap.API.csproj", "Birdmap.API/"] -COPY ["Birdmap.BLL/Birdmap.BLL.csproj", "Birdmap.BLL/"] -COPY ["Birdmap.Common/Birdmap.Common.csproj", "Birdmap.Common/"] -COPY ["Birdmap.DAL/Birdmap.DAL.csproj", "Birdmap.DAL/"] -RUN dotnet restore "Birdmap.API/Birdmap.API.csproj" -COPY . . -WORKDIR "/src/Birdmap.API" -RUN dotnet build "Birdmap.API.csproj" -c Release -o /app/build - -FROM build AS publish -RUN dotnet publish "Birdmap.API.csproj" -c Release -o /app/publish - -FROM base AS final -WORKDIR /app -COPY --from=publish /app/publish . -ENTRYPOINT ["dotnet", "Birdmap.API.dll"] \ No newline at end of file diff --git a/Birdmap.API/Dockerfile1.original b/Birdmap.API/Dockerfile1.original deleted file mode 100644 index 2763799..0000000 --- a/Birdmap.API/Dockerfile1.original +++ /dev/null @@ -1,30 +0,0 @@ -#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. -FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base -WORKDIR /app -EXPOSE 80 -EXPOSE 443 -RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - -RUN apt-get update -RUN apt-get install -y nodejs - -FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build -RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - -RUN apt-get update -RUN apt-get install -y nodejs -WORKDIR /src -COPY ["Birdmap.API/Birdmap.API.csproj", "Birdmap.API/"] -COPY ["Birdmap.BLL/Birdmap.BLL.csproj", "Birdmap.BLL/"] -COPY ["Birdmap.Common/Birdmap.Common.csproj", "Birdmap.Common/"] -COPY ["Birdmap.DAL/Birdmap.DAL.csproj", "Birdmap.DAL/"] -RUN dotnet restore "Birdmap.API/Birdmap.API.csproj" -COPY . . -WORKDIR "/src/Birdmap.API" -RUN dotnet build "Birdmap.API.csproj" -c Release -o /app/build - -FROM build AS publish -RUN dotnet publish "Birdmap.API.csproj" -c Release -o /app/publish - -FROM base AS final -WORKDIR /app -COPY --from=publish /app/publish . -ENTRYPOINT ["dotnet", "Birdmap.API.dll"] \ No newline at end of file diff --git a/Birdmap.API/Program.cs b/Birdmap.API/Program.cs index 6310be1..ac86a8b 100644 --- a/Birdmap.API/Program.cs +++ b/Birdmap.API/Program.cs @@ -1,5 +1,6 @@ using Birdmap.DAL; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -20,12 +21,6 @@ namespace Birdmap.API logger.Debug("Building host..."); var host = CreateHostBuilder(args).Build(); - using (var scope = host.Services.CreateScope()) - { - var db = scope.ServiceProvider.GetRequiredService(); - db.Database.EnsureCreated(); - } - logger.Debug("Seeding database..."); SeedDatabase(host); @@ -45,6 +40,10 @@ namespace Birdmap.API public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.AddEnvironmentVariables(prefix: "Birdmap_"); + }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); @@ -59,8 +58,8 @@ namespace Birdmap.API private static void SeedDatabase(IHost host) { using var scope = host.Services.CreateScope(); - var dbInitializer = scope.ServiceProvider.GetRequiredService(); + var dbInitializer = scope.ServiceProvider.GetRequiredService(); dbInitializer.Initialize(); } } diff --git a/Birdmap.API/Properties/launchSettings.json b/Birdmap.API/Properties/launchSettings.json index a68e28b..e828804 100644 --- a/Birdmap.API/Properties/launchSettings.json +++ b/Birdmap.API/Properties/launchSettings.json @@ -2,6 +2,10 @@ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, + "iis": { + "applicationUrl": "http://localhost/Birdmap.API", + "sslPort": 0 + }, "iisExpress": { "applicationUrl": "http://localhost:63288", "sslPort": 44331 @@ -12,6 +16,7 @@ "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { + "Birdmap_LocalDbConnectionString": "Data Source=DESKTOP-3600;Initial Catalog=birdmap2;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False", "ASPNETCORE_ENVIRONMENT": "Development" } }, diff --git a/Birdmap.API/appsettings.json b/Birdmap.API/appsettings.json index 721fc14..da4be80 100644 --- a/Birdmap.API/appsettings.json +++ b/Birdmap.API/appsettings.json @@ -10,7 +10,7 @@ "Secret": "7vj.3KW.hYE!}4u6", // "LocalDbConnectionString": "Data Source=DESKTOP-3600\\SQLEXPRESS;Initial Catalog=birdmap;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False", //"LocalDbConnectionString": "Data Source=DESKTOP-3600;Initial Catalog=birdmap;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False", - "LocalDbConnectionString": "Data Source=db;Initial Catalog=master;User=sa;Password=asdfjkl-123", + "LocalDbConnectionString": "Data Source=db;Initial Catalog=birdmap;User=sa;Password=RPSsql12345", "Defaults": { "Services": { "Local Database": "https://localhost:44331/health", @@ -30,6 +30,7 @@ ] }, "UseDummyServices": true, + "ServicesBaseUrl": "https://birb.k8s.kmlabz.com/", "Mqtt": { "BrokerHostSettings": { "Host": "localhost", diff --git a/Birdmap.BLL/Services/LiveDeviceService.cs b/Birdmap.BLL/Services/LiveDeviceService.cs index e902146..d57eb11 100644 --- a/Birdmap.BLL/Services/LiveDeviceService.cs +++ b/Birdmap.BLL/Services/LiveDeviceService.cs @@ -23,8 +23,9 @@ namespace Birdmap.BLL.Services private System.Net.Http.HttpClient _httpClient; private System.Lazy _settings; - public LiveDummyService(System.Net.Http.HttpClient httpClient) + public LiveDummyService(string baseUrl, System.Net.Http.HttpClient httpClient) { + _baseUrl = baseUrl; _httpClient = httpClient; _settings = new System.Lazy(CreateSerializerSettings); } diff --git a/Birdmap.BLL/Services/LiveInputService.cs b/Birdmap.BLL/Services/LiveInputService.cs index 15e7e1d..f9d6743 100644 --- a/Birdmap.BLL/Services/LiveInputService.cs +++ b/Birdmap.BLL/Services/LiveInputService.cs @@ -23,8 +23,9 @@ namespace Birdmap.BLL.Services private System.Net.Http.HttpClient _httpClient; private System.Lazy _settings; - public LiveInputService(System.Net.Http.HttpClient httpClient) + public LiveInputService(string baseUrl, System.Net.Http.HttpClient httpClient) { + _baseUrl = baseUrl; _httpClient = httpClient; _settings = new System.Lazy(CreateSerializerSettings); } diff --git a/Birdmap.BLL/Startup.cs b/Birdmap.BLL/Startup.cs index 9132d87..2544875 100644 --- a/Birdmap.BLL/Startup.cs +++ b/Birdmap.BLL/Startup.cs @@ -2,6 +2,7 @@ using Birdmap.BLL.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using System.Net.Http; namespace Birdmap.BLL { @@ -20,8 +21,20 @@ namespace Birdmap.BLL } else { - services.AddTransient(); - services.AddTransient(); + var baseUrl = configuration.GetValue("ServicesBaseUrl"); + + services.AddTransient(serviceProvider => + { + var httpClient = serviceProvider.GetService(); + var service = new LiveInputService(baseUrl, httpClient); + return service; + }); + services.AddTransient(serviceProvider => + { + var httpClient = serviceProvider.GetService(); + var service = new LiveDummyService(baseUrl, httpClient); + return service; + }); } return services; diff --git a/Birdmap.DAL/DbInitializer.cs b/Birdmap.DAL/DbInitializer.cs index b578247..77f1d91 100644 --- a/Birdmap.DAL/DbInitializer.cs +++ b/Birdmap.DAL/DbInitializer.cs @@ -22,10 +22,17 @@ namespace Birdmap.DAL public void Initialize() { + EnsureCreated(); AddDefaultUsers(); AddDefaultServices(); } + private void EnsureCreated() + { + _logger.LogInformation("Ensuring database is created..."); + _context.Database.EnsureCreated(); + } + private void AddDefaultServices() { _logger.LogInformation("Removing previously added default services..."); diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index b52ad14..0000000 --- a/Dockerfile +++ /dev/null @@ -1,22 +0,0 @@ -# https://hub.docker.com/_/microsoft-dotnet -FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build -WORKDIR /app -EXPOSE 80 - -RUN curl --silent --location https://deb.nodesource.com/setup_10.x | bash - -RUN apt-get install --yes nodejs - -# copy csproj and restore as distinct layers -WORKDIR /src -COPY . . -RUN dotnet restore "Birdmap.API/Birdmap.API.csproj" - -# copy everything else and build app -RUN dotnet publish "Birdmap.API/Birdmap.API.csproj" -c Release -o /app - - -# final stage/image -FROM mcr.microsoft.com/dotnet/aspnet:5.0 -WORKDIR /app -COPY --from=build /app . -ENTRYPOINT ["dotnet", "Birdmap.API.dll"] diff --git a/docker-compose.yml b/docker-compose.yml index 1dd542d..cdf26ce 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,20 +2,36 @@ version: '3.4' services: db: - image: "mcr.microsoft.com/mssql/server:2017-latest" + image: "mcr.microsoft.com/mssql/server:2019-latest" environment: - ACCEPT_EULA=Y - - SA_PASSWORD=asdfjkl-123 + - SA_PASSWORD=RPSsql12345 birdmap.api: image: ${DOCKER_REGISTRY-}birdmapapi ports: - - "8000:80" - - "5001:443" + - "5000:5000" + - "5001:5001" build: context: . dockerfile: Birdmap.API/Dockerfile depends_on: - db environment: - - ASPNETCORE_URLS=https://+:443;http://+:80 + - ASPNETCORE_URLS=https://localhost:5001;http://localhost:5000 + - Birdmap_LocalDbConnectionString=Data Source=db;Initial Catalog=birdmap;User=sa;Password=RPSsql12345 + - Birdmap_Defaults__Users__0__Name=admin + - Birdmap_Defaults__Users__0__Password=pass + - Birdmap_Defaults__Users__0__Role=Admin + - Birdmap_Defaults__Users__1__Name=user + - Birdmap_Defaults__Users__1__Password=pass + - Birdmap_Defaults__Users__1__Role=User + - Birdmap_Defaults__Services__Local-Database=https://localhost/health + - Birdmap_UseDummyServices=true + - Birdmap_ServicesBaseUrl=https://birb.k8s.kmlabz.com/ + - Birdmap_Mqtt__BrokerHostSettings__Host=localhost + - Birdmap_Mqtt__BrokerHostSettings__Port=1883 + - Birdmap_Mqtt__ClientSettings__Id=ASP.NET Core client + - Birdmap_Mqtt__ClientSettings__Username=username + - Birdmap_Mqtt__ClientSettings__Password=password + - Birdmap_Mqtt__ClientSettings__Topic=devices/output \ No newline at end of file