From 8cf7ef4c52b928fc946d7c1fcd0f7c008fbeffb2 Mon Sep 17 00:00:00 2001 From: ArgonarioD Date: Wed, 5 Jul 2023 11:07:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20docker=E9=80=82=E5=BA=94=E6=80=A7?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AicsKnowledgeBase_file.csproj | 1 + AicsKnowledgeBase_file/Dockerfile | 4 ++-- .../UpdateFileMessageConsumerHandler.cs | 3 ++- AicsKnowledgeBase_file/Models/SslOptions.cs | 21 +++++++++++++++++++ AicsKnowledgeBase_file/Program.cs | 12 +++++------ .../Properties/launchSettings.json | 4 +--- .../appsettings.Development.json | 8 +++++-- AicsKnowledgeBase_file/appsettings.json | 11 ++++++++-- FileServerTests/UnitTest1.cs | 5 +++-- buildDockerImage.ps1 | 2 ++ 10 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 AicsKnowledgeBase_file/Models/SslOptions.cs create mode 100644 buildDockerImage.ps1 diff --git a/AicsKnowledgeBase_file/AicsKnowledgeBase_file.csproj b/AicsKnowledgeBase_file/AicsKnowledgeBase_file.csproj index 7b82206..e655dad 100644 --- a/AicsKnowledgeBase_file/AicsKnowledgeBase_file.csproj +++ b/AicsKnowledgeBase_file/AicsKnowledgeBase_file.csproj @@ -5,6 +5,7 @@ enable enable Linux + e5e584e4-bc5e-4ce3-b4ad-d9c46fe19967 diff --git a/AicsKnowledgeBase_file/Dockerfile b/AicsKnowledgeBase_file/Dockerfile index 84977fd..079d3b2 100644 --- a/AicsKnowledgeBase_file/Dockerfile +++ b/AicsKnowledgeBase_file/Dockerfile @@ -1,7 +1,6 @@ FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base WORKDIR /app -EXPOSE 80 -EXPOSE 443 +EXPOSE 8081/udp FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build WORKDIR /src @@ -16,5 +15,6 @@ RUN dotnet publish "AicsKnowledgeBase_file.csproj" -c Release -o /app/publish /p FROM base AS final WORKDIR /app +VOLUME ["/app/ssl", "/app/files"] COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "AicsKnowledgeBase_file.dll"] diff --git a/AicsKnowledgeBase_file/Handlers/UpdateFileMessageConsumerHandler.cs b/AicsKnowledgeBase_file/Handlers/UpdateFileMessageConsumerHandler.cs index 6842575..1a0161a 100644 --- a/AicsKnowledgeBase_file/Handlers/UpdateFileMessageConsumerHandler.cs +++ b/AicsKnowledgeBase_file/Handlers/UpdateFileMessageConsumerHandler.cs @@ -25,7 +25,8 @@ public class UpdateFileMessageConsumerHandler : BackgroundService { _consumerConfig = new ConsumerConfig { BootstrapServers = _options.BootstrapServers, GroupId = _options.ConsumerGroupId, - AutoOffsetReset = AutoOffsetReset.Earliest + AutoOffsetReset = AutoOffsetReset.Earliest, + AllowAutoCreateTopics = true }; } diff --git a/AicsKnowledgeBase_file/Models/SslOptions.cs b/AicsKnowledgeBase_file/Models/SslOptions.cs new file mode 100644 index 0000000..6e16f37 --- /dev/null +++ b/AicsKnowledgeBase_file/Models/SslOptions.cs @@ -0,0 +1,21 @@ +using System.Security.Cryptography.X509Certificates; + +namespace AicsKnowledgeBase_file.Models; + +public class SslOptions { + public const string SectionName = "Ssl"; + public string CertFilePath { get; set; } = null!; + public bool UseKeyFile { get; set; } = false; + public string? KeyFilePath { get; set; } = null; + public string? Password { get; set; } = null; + + public X509Certificate2 BuildServerCertificate() { + if (UseKeyFile && KeyFilePath != null) { + return X509Certificate2.CreateFromPemFile(CertFilePath, KeyFilePath); + } + if (Password != null) { + return new X509Certificate2(CertFilePath, Password); + } + throw new InvalidDataException("KeyFilePath and Password are both null."); + } +} \ No newline at end of file diff --git a/AicsKnowledgeBase_file/Program.cs b/AicsKnowledgeBase_file/Program.cs index 430eb5b..5a270d3 100644 --- a/AicsKnowledgeBase_file/Program.cs +++ b/AicsKnowledgeBase_file/Program.cs @@ -1,16 +1,15 @@ using System.Text.Json; using AicsKnowledgeBase_file.Handlers; using AicsKnowledgeBase_file.Models; -using CommonResources; using Microsoft.AspNetCore.Server.Kestrel.Core; var builder = WebApplication.CreateBuilder(args); - -builder.WebHost.ConfigureKestrel((context, options) => { - options.ListenAnyIP(8090, listenOptions => { +builder.WebHost.UseUrls("https://*:8081"); +builder.WebHost.UseKestrel((context, options) => { + options.ConfigureEndpointDefaults(listenOptions => { listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3; - // listenOptions.Protocols = HttpProtocols.Http1AndHttp2; - listenOptions.UseHttps(Resources.GetServerCertificate()); + listenOptions.UseHttps(builder.Configuration.GetSection(SslOptions.SectionName).Get()! + .BuildServerCertificate()); }); }); @@ -34,6 +33,7 @@ if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } + app.MapGet("/hello", () => "hello"); app.UseHttpsRedirection(); diff --git a/AicsKnowledgeBase_file/Properties/launchSettings.json b/AicsKnowledgeBase_file/Properties/launchSettings.json index 0e18c85..13a78d9 100644 --- a/AicsKnowledgeBase_file/Properties/launchSettings.json +++ b/AicsKnowledgeBase_file/Properties/launchSettings.json @@ -22,9 +22,7 @@ "https": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, - "launchUrl": "swagger", - "applicationUrl": "https://127.0.0.1:8090", + "applicationUrl": "https://127.0.0.1:8081", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/AicsKnowledgeBase_file/appsettings.Development.json b/AicsKnowledgeBase_file/appsettings.Development.json index d3beb18..e004b0c 100644 --- a/AicsKnowledgeBase_file/appsettings.Development.json +++ b/AicsKnowledgeBase_file/appsettings.Development.json @@ -5,7 +5,11 @@ } }, "Kafka": { - "BootstrapServers": "localhost:9094", - "ConsumerGroupId": "file-server" + "BootstrapServers": "localhost:9094" + }, + "Ssl": { + "UseKeyFile": false, + "CertFilePath": "../CommonResources/ssl/ssl-rsa.pfx", + "Password": "auto" } } diff --git a/AicsKnowledgeBase_file/appsettings.json b/AicsKnowledgeBase_file/appsettings.json index 6a845cf..06f37d8 100644 --- a/AicsKnowledgeBase_file/appsettings.json +++ b/AicsKnowledgeBase_file/appsettings.json @@ -1,8 +1,15 @@ { "Logging": { "LogLevel": { - "Default": "Information" + "Default": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Kafka": { + "BootstrapServers": "kafka:9092", + "ConsumerGroupId": "file-server" + }, + "Ssl": { + "UseKeyFile": true + } } diff --git a/FileServerTests/UnitTest1.cs b/FileServerTests/UnitTest1.cs index 9a0b901..212ef9b 100644 --- a/FileServerTests/UnitTest1.cs +++ b/FileServerTests/UnitTest1.cs @@ -12,9 +12,10 @@ public class UnitTest1 { DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact, }; - Console.WriteLine("--- 127.0.0.1:8090 ---"); + // Console.WriteLine("--- 127.0.0.1:8090 ---"); - var resp = await client.GetAsync("https://127.0.0.1:8090/hello"); + // var resp = await client.GetAsync("https://127.0.0.1:8090/hello"); + var resp = await client.GetAsync("https://api.hammer-hfut.tk:233/aics/file/hello"); var headers = new StringBuilder(); foreach (var (headerName, headerValues) in resp.Headers) { headers.AppendLine($"{headerName}: {string.Join(", ", headerValues)}"); diff --git a/buildDockerImage.ps1 b/buildDockerImage.ps1 new file mode 100644 index 0000000..0e6bd26 --- /dev/null +++ b/buildDockerImage.ps1 @@ -0,0 +1,2 @@ +docker build -f .\AicsKnowledgeBase_file\Dockerfile -t auto/aics_file:latest . +pause \ No newline at end of file