diff --git a/src/backend/.dockerignore b/src/backend/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/src/backend/.dockerignore @@ -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/** \ No newline at end of file diff --git a/src/backend/BookmarkApi/BookmarkApi.csproj b/src/backend/BookmarkApi/BookmarkApi.csproj index 663989f..1e726ce 100644 --- a/src/backend/BookmarkApi/BookmarkApi.csproj +++ b/src/backend/BookmarkApi/BookmarkApi.csproj @@ -6,6 +6,8 @@ enable true $(NoWarn);1591 + 88af096c-2bee-4382-be70-be7c461d3862 + Linux @@ -17,6 +19,7 @@ + diff --git a/src/backend/BookmarkApi/Dockerfile b/src/backend/BookmarkApi/Dockerfile new file mode 100644 index 0000000..8f00e13 --- /dev/null +++ b/src/backend/BookmarkApi/Dockerfile @@ -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"] \ No newline at end of file diff --git a/src/backend/BookmarkApi/Program.cs b/src/backend/BookmarkApi/Program.cs index f691b6f..24be560 100644 --- a/src/backend/BookmarkApi/Program.cs +++ b/src/backend/BookmarkApi/Program.cs @@ -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() ?? Array.Empty(); 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()) { diff --git a/src/backend/BookmarkApi/Properties/launchSettings.json b/src/backend/BookmarkApi/Properties/launchSettings.json index 78a661a..dd8beee 100644 --- a/src/backend/BookmarkApi/Properties/launchSettings.json +++ b/src/backend/BookmarkApi/Properties/launchSettings.json @@ -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" +} \ No newline at end of file diff --git a/src/backend/BookmarkApi/appsettings.json b/src/backend/BookmarkApi/appsettings.json index 0acee63..bb0c297 100644 --- a/src/backend/BookmarkApi/appsettings.json +++ b/src/backend/BookmarkApi/appsettings.json @@ -1,4 +1,4 @@ -{ +{ "Logging": { "LogLevel": { "Default": "Information", @@ -23,5 +23,13 @@ "http://localhost:5174", "chrome-extension://*" ] + }, + //服务器配置 + "Kestrel": { + "Endpoints": { + "Http": { + "Url": "http://*:8080" + } + } } }