feat: 更新接口

This commit is contained in:
GukSang.Jin
2026-03-03 16:49:57 +08:00
parent 2a312e798f
commit 874ed80fe0
18 changed files with 376 additions and 367 deletions

28
PetWash.Api/.dockerignore Normal file
View File

@@ -0,0 +1,28 @@
**/.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
**/*.db
**/*.db-shm
**/*.db-wal

30
PetWash.Api/Dockerfile Normal file
View File

@@ -0,0 +1,30 @@
# 使用 .NET 8.0 SDK 作为构建镜像
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
# 复制项目文件并还原依赖
COPY ["PetWash.Api/PetWash.Api.csproj", "PetWash.Api/"]
RUN dotnet restore "PetWash.Api/PetWash.Api.csproj"
# 复制所有文件并构建
COPY . .
WORKDIR "/src/PetWash.Api"
RUN dotnet build "PetWash.Api.csproj" -c Release -o /app/build
# 发布应用
FROM build AS publish
RUN dotnet publish "PetWash.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false
# 使用运行时镜像
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
WORKDIR /app
EXPOSE 80
EXPOSE 443
# 复制发布的文件
COPY --from=publish /app/publish .
# 设置环境变量
ENV ASPNETCORE_URLS=http://+:80
ENTRYPOINT ["dotnet", "PetWash.Api.dll"]

View File

@@ -4,11 +4,13 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>2f7df0d9-7cee-4b58-bb61-3b653acfdb98</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.14" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -4,9 +4,24 @@ using PetWash.Api.Services;
var builder = WebApplication.CreateBuilder(args);
// 添加数据库
// 配置数据库(支持 SQLite 和 MySQL
var dbProvider = builder.Configuration.GetValue<string>("DatabaseProvider") ?? "Sqlite";
var connectionString = dbProvider == "MySql"
? builder.Configuration.GetConnectionString("MySqlConnection")
: builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<PetWashDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection") ?? "Data Source=petwash.db"));
{
if (dbProvider == "MySql")
{
var serverVersion = new MySqlServerVersion(new Version(8, 0, 21));
options.UseMySql(connectionString, serverVersion);
}
else
{
options.UseSqlite(connectionString ?? "Data Source=petwash.db");
}
});
// 添加服务
builder.Services.AddSingleton<MqttService>();
@@ -34,14 +49,34 @@ var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<PetWashDbContext>();
db.Database.EnsureCreated();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
try
{
logger.LogInformation("开始初始化数据库...");
// 确保数据库已创建
var created = db.Database.EnsureCreated();
if (created)
{
logger.LogInformation("数据库创建成功,种子数据已插入");
}
else
{
logger.LogInformation("数据库已存在");
}
}
catch (Exception ex)
{
logger.LogError(ex, "数据库初始化失败");
throw;
}
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// 启用 Swagger所有环境
app.UseSwagger();
app.UseSwaggerUI();
app.UseCors();
app.UseHttpsRedirection();

View File

@@ -0,0 +1,11 @@
{
"dependencies": {
"secrets1": {
"type": "secrets"
},
"sqlite1": {
"type": "sqlite",
"connectionId": "ConnectionStrings:DatabaseConnection"
}
}
}

View File

@@ -0,0 +1,13 @@
{
"dependencies": {
"secrets1": {
"type": "secrets.user"
},
"sqlite1": {
"secretStore": "LocalSecretsFile",
"resourceId": null,
"type": "sqlite.local",
"connectionId": "ConnectionStrings:DatabaseConnection"
}
}
}

View File

@@ -0,0 +1,11 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.EntityFrameworkCore": "Warning"
}
},
"AllowedHosts": "*",
"DatabaseProvider": "MySql"
}

View File

@@ -7,6 +7,8 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=petwash.db"
}
"DefaultConnection": "Data Source=petwash.db",
"MySqlConnection": "Server=101.132.182.216;Database=petwash;User=sc_root;Password=Shsc#$@2024#@!;Port=3306;CharSet=utf8mb4;"
},
"DatabaseProvider": "Sqlite"
}

View File

@@ -0,0 +1,13 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "10.0.3",
"commands": [
"dotnet-ef"
],
"rollForward": false
}
}
}

Binary file not shown.