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> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<UserSecretsId>e5e584e4-bc5e-4ce3-b4ad-d9c46fe19967</UserSecretsId>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,7 +1,6 @@
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app WORKDIR /app
EXPOSE 80 EXPOSE 8081/udp
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src WORKDIR /src
@ -16,5 +15,6 @@ RUN dotnet publish "AicsKnowledgeBase_file.csproj" -c Release -o /app/publish /p
FROM base AS final FROM base AS final
WORKDIR /app WORKDIR /app
VOLUME ["/app/ssl", "/app/files"]
COPY --from=publish /app/publish . COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AicsKnowledgeBase_file.dll"] ENTRYPOINT ["dotnet", "AicsKnowledgeBase_file.dll"]

View File

@ -25,7 +25,8 @@ public class UpdateFileMessageConsumerHandler : BackgroundService {
_consumerConfig = new ConsumerConfig { _consumerConfig = new ConsumerConfig {
BootstrapServers = _options.BootstrapServers, BootstrapServers = _options.BootstrapServers,
GroupId = _options.ConsumerGroupId, 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 System.Text.Json;
using AicsKnowledgeBase_file.Handlers; using AicsKnowledgeBase_file.Handlers;
using AicsKnowledgeBase_file.Models; using AicsKnowledgeBase_file.Models;
using CommonResources;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("https://*:8081");
builder.WebHost.ConfigureKestrel((context, options) => { builder.WebHost.UseKestrel((context, options) => {
options.ListenAnyIP(8090, listenOptions => { options.ConfigureEndpointDefaults(listenOptions => {
listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3; listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
// listenOptions.Protocols = HttpProtocols.Http1AndHttp2; listenOptions.UseHttps(builder.Configuration.GetSection(SslOptions.SectionName).Get<SslOptions>()!
listenOptions.UseHttps(Resources.GetServerCertificate()); .BuildServerCertificate());
}); });
}); });
@ -34,6 +33,7 @@ if (app.Environment.IsDevelopment()) {
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
} }
app.MapGet("/hello", () => "hello"); app.MapGet("/hello", () => "hello");
app.UseHttpsRedirection(); app.UseHttpsRedirection();

View File

@ -22,9 +22,7 @@
"https": { "https": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "applicationUrl": "https://127.0.0.1:8081",
"launchUrl": "swagger",
"applicationUrl": "https://127.0.0.1:8090",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }

View File

@ -5,7 +5,11 @@
} }
}, },
"Kafka": { "Kafka": {
"BootstrapServers": "localhost:9094", "BootstrapServers": "localhost:9094"
"ConsumerGroupId": "file-server" },
"Ssl": {
"UseKeyFile": false,
"CertFilePath": "../CommonResources/ssl/ssl-rsa.pfx",
"Password": "auto"
} }
} }

View File

@ -1,8 +1,15 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "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, 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(); var headers = new StringBuilder();
foreach (var (headerName, headerValues) in resp.Headers) { foreach (var (headerName, headerValues) in resp.Headers) {
headers.AppendLine($"{headerName}: {string.Join(", ", headerValues)}"); 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