fix: docker适应性修复

master
ArgonarioD 2023-07-05 11:07:08 +08:00
parent 8711c95824
commit 8cf7ef4c52
10 changed files with 53 additions and 18 deletions

View File

@ -5,6 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<UserSecretsId>e5e584e4-bc5e-4ce3-b4ad-d9c46fe19967</UserSecretsId>
</PropertyGroup>
<ItemGroup>

View File

@ -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"]

View File

@ -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
};
}

View File

@ -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.");
}
}

View File

@ -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<SslOptions>()!
.BuildServerCertificate());
});
});
@ -34,6 +33,7 @@ if (app.Environment.IsDevelopment()) {
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/hello", () => "hello");
app.UseHttpsRedirection();

View File

@ -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"
}

View File

@ -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"
}
}

View File

@ -1,8 +1,15 @@
{
"Logging": {
"LogLevel": {
"Default": "Information"
"Default": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Kafka": {
"BootstrapServers": "kafka:9092",
"ConsumerGroupId": "file-server"
},
"Ssl": {
"UseKeyFile": true
}
}

View File

@ -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)}");

2
buildDockerImage.ps1 Normal file
View File

@ -0,0 +1,2 @@
docker build -f .\AicsKnowledgeBase_file\Dockerfile -t auto/aics_file:latest .
pause