This commit is contained in:
zpc 2025-12-17 22:45:21 +08:00
parent f7df048199
commit c31c06c227
6 changed files with 114 additions and 21 deletions

30
src/backend/.dockerignore Normal file
View File

@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**

View File

@ -6,6 +6,8 @@
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
<UserSecretsId>88af096c-2bee-4382-be70-be7c461d3862</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
@ -17,6 +19,7 @@
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.11" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.23.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>

View File

@ -0,0 +1,33 @@
# 请参阅 https://aka.ms/customizecontainer 以了解如何自定义调试容器,以及 Visual Studio 如何使用此 Dockerfile 生成映像以更快地进行调试。
# 此阶段用于在快速模式(默认为调试配置)下从 VS 运行时
FROM mcr.microsoft.com/dotnet/aspnet:8.0-noble AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
# 此阶段用于生成服务项目
FROM mcr.microsoft.com/dotnet/sdk:8.0-noble AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["BookmarkApi/BookmarkApi.csproj", "BookmarkApi/"]
COPY ["BookmarkApi.Core/BookmarkApi.Core.csproj", "BookmarkApi.Core/"]
COPY ["BookmarkApi.Data/BookmarkApi.Data.csproj", "BookmarkApi.Data/"]
COPY ["BookmarkApi.Shared/BookmarkApi.Shared.csproj", "BookmarkApi.Shared/"]
RUN dotnet restore "./BookmarkApi/BookmarkApi.csproj"
COPY . .
WORKDIR "/src/BookmarkApi"
RUN dotnet build "./BookmarkApi.csproj" -c $BUILD_CONFIGURATION -o /app/build
# 此阶段用于发布要复制到最终阶段的服务项目
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./BookmarkApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# 此阶段在生产中使用,或在常规模式下从 VS 运行时使用(在不使用调试配置时为默认值)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BookmarkApi.dll"]

View File

@ -1,7 +1,9 @@
using System.Text;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using BookmarkApi.Data;
using BookmarkApi.Shared.Models;
using BookmarkApi.Core.Services;
@ -100,6 +102,7 @@ builder.Services.AddAuthentication(options =>
builder.Services.AddAuthorization();
// 配置 CORS
#if DEBUG
var allowedOrigins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? Array.Empty<string>();
builder.Services.AddCors(options =>
{
@ -119,7 +122,7 @@ builder.Services.AddCors(options =>
.AllowAnyHeader();
});
});
#endif
// 配置 FreeSql
builder.Services.AddFreeSql(builder.Configuration);
@ -145,14 +148,14 @@ if (app.Environment.IsDevelopment())
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Bookmark API v1");
options.RoutePrefix = string.Empty; // 设置 Swagger UI 为根路径
});
}); // 开发环境启用 CORS
app.UseCors("Development");
}
else
{
app.UseCors();
}
//else
//{
// app.UseCors();
//}
app.UseHttpsRedirection();
@ -160,7 +163,15 @@ app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
// 配置最小API端点
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/log", async context =>
{
await context.Response.WriteAsync("run success");
});
});
// 初始化数据库
using (var scope = app.Services.CreateScope())
{

View File

@ -1,23 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
{
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5159",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5159"
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7249;http://localhost:5159",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7249;http://localhost:5159"
},
"Container (Dockerfile)": {
"commandName": "Docker",
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"environmentVariables": {
"ASPNETCORE_HTTPS_PORTS": "8081",
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true,
"useSSL": true
}
}
}
},
"$schema": "https://json.schemastore.org/launchsettings.json"
}

View File

@ -1,4 +1,4 @@
{
{
"Logging": {
"LogLevel": {
"Default": "Information",
@ -23,5 +23,13 @@
"http://localhost:5174",
"chrome-extension://*"
]
},
//
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://*:8080"
}
}
}
}