Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
- name: Save the SSH private key to a file
run: |
if [ ! -f ~/.ssh/id_rsa ]; then
echo "Save the SSH private key to a file"
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
Expand All @@ -58,6 +59,7 @@ jobs:
- name: Add remote server to known_hosts
run: |
if ! grep -q "${{ env.SSH_HOST }}" ~/.ssh/known_hosts; then
echo "Add remote server to known_hosts"
ssh-keyscan ${{ env.SSH_HOST }} >> ~/.ssh/known_hosts
fi

Expand Down
59 changes: 0 additions & 59 deletions src/MvcDemo/Data/DemoDbInitializer.cs

This file was deleted.

16 changes: 0 additions & 16 deletions src/MvcDemo/Data/DemoDesignTimeDbContextFactory.cs

This file was deleted.

52 changes: 0 additions & 52 deletions src/MvcDemo/Data/HostDatabaseExtensions.cs

This file was deleted.

90 changes: 90 additions & 0 deletions src/MvcDemo/Data/WebApplicationDatabaseExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Demo.Data;

public static class WebApplicationDatabaseExtensions
{
public static WebApplication MigrateDatabase(this WebApplication app)
{
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;

try
{
var db = services.GetRequiredService<DemoDbContext>();
db.Database.Migrate();
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while migrating the database.");
}
}

return app;
}


public static WebApplication SeedDatabase(this WebApplication app, string adminUserName, string adminInitPassword)
{
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;

try
{
Task.Run(async () =>
{
var userManager = services.GetRequiredService<UserManager<IdentityUser>>();
await EnsureAdminUser(userManager, adminUserName, adminInitPassword);
});
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while seeding the database.");
}
}

return app;
}

private static async Task EnsureAdminUser(UserManager<IdentityUser> userManager, string adminUserName, string adminEmail)
{
var adminInitPassword = adminEmail;

var user = await userManager.FindByNameAsync(adminUserName);
if (user == null)
{
user = new IdentityUser()
{
UserName = adminUserName,
Email = adminEmail,
EmailConfirmed = true,
};
var result = await userManager.CreateAsync(user, adminInitPassword);
if (!result.Succeeded)
{
throw new Exception(GetErrorMessage(result));
}
}
}

private static string GetErrorMessage(IdentityResult identityResult)
{
var result = "";

foreach (var error in identityResult.Errors)
{
result += $"[{error.Code}]{error.Description}" + Environment.NewLine;
}
return result;
}
}
2 changes: 1 addition & 1 deletion src/MvcDemo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PackageReference Include="Myvas.AspNetCore.Authentication.WeixinOpen" Version="9.0.*" />
<PackageReference Include="Myvas.AspNetCore.TencentSms" Version="9.0.*" />
<PackageReference Include="Myvas.AspNetCore.ViewDivert" Version="9.0.*" />
<PackageReference Include="Myvas.AspNetCore.Weixin" Version="9.0.0-rc.5" />
<PackageReference Include="Myvas.AspNetCore.Weixin" Version="9.0.0-rc.6" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.*" />
</ItemGroup>

Expand Down
5 changes: 3 additions & 2 deletions src/MvcDemo/HostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Demo;

public static class HostExtensions
{
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder)
{
var Configuration = builder.Configuration;

Expand Down Expand Up @@ -91,6 +91,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
{
o.Configuration = Configuration.GetConnectionString("RedisConnection");
})
.AddJsapiTicketRedisCacheProvider()
.AddWeixinSite(o =>
{
o.Debug = true; // for this demo for debugging
Expand All @@ -105,7 +106,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
})
.AddWeixinEventSink<DemoEventSink>();

return builder.Build();
return builder;
}

public static WebApplication ConfigurePipeline(this WebApplication app)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading