Toolhub / Program.cs
unifare
Initial commit: ToolHub ASP.NET Core app
5fc700d
using ToolHub.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
// 添加HttpContext访问器
builder.Services.AddHttpContextAccessor();
// 配置FreeSql
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? "Data Source=toolhub.db";
builder.Services.AddSingleton<IFreeSql>(provider =>
{
var freeSql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, connectionString)
.UseAutoSyncStructure(true) // 自动同步数据库结构
.Build();
return freeSql;
});
// 注册服务
builder.Services.AddScoped<IToolService, ToolService>();
builder.Services.AddScoped<IUserService, UserService>();
// 配置身份验证
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Admin/Login";
options.LogoutPath = "/Admin/Logout";
options.ExpireTimeSpan = TimeSpan.FromDays(7);
});
var app = builder.Build();
// 初始化数据库和种子数据
using (var scope = app.Services.CreateScope())
{
var freeSql = scope.ServiceProvider.GetRequiredService<IFreeSql>();
// 确保数据库表已创建
freeSql.CodeFirst.SyncStructure<ToolHub.Models.User>();
freeSql.CodeFirst.SyncStructure<ToolHub.Models.Category>();
freeSql.CodeFirst.SyncStructure<ToolHub.Models.Tool>();
freeSql.CodeFirst.SyncStructure<ToolHub.Models.Tag>();
freeSql.CodeFirst.SyncStructure<ToolHub.Models.ToolTag>();
freeSql.CodeFirst.SyncStructure<ToolHub.Models.UserFavorite>();
freeSql.CodeFirst.SyncStructure<ToolHub.Models.UserToolAccess>();
freeSql.CodeFirst.SyncStructure<ToolHub.Models.ToolStatistics>();
// 添加初始数据
await SeedDataAsync(freeSql);
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapStaticAssets();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}")
.WithStaticAssets();
app.Run();
// 数据初始化方法
static async Task SeedDataAsync(IFreeSql freeSql)
{
// 检查是否已有数据
var categoryCount = await freeSql.Select<ToolHub.Models.Category>().CountAsync();
if (categoryCount > 0) return;
// 添加分类
var categories = new List<ToolHub.Models.Category>
{
new() { Name = "PDF工具", Description = "PDF文档处理相关工具", Icon = "fas fa-file-pdf", Color = "#dc3545", SortOrder = 1 },
new() { Name = "图片工具", Description = "图片处理和编辑工具", Icon = "fas fa-image", Color = "#165DFF", SortOrder = 2 },
new() { Name = "视频工具", Description = "视频处理和编辑工具", Icon = "fas fa-video", Color = "#FF7D00", SortOrder = 3 },
new() { Name = "文档转换", Description = "各种文档格式转换工具", Icon = "fas fa-file-alt", Color = "#52C41A", SortOrder = 4 },
new() { Name = "数据计算", Description = "数据计算和统计工具", Icon = "fas fa-calculator", Color = "#36CFC9", SortOrder = 5 },
new() { Name = "开发工具", Description = "程序开发相关工具", Icon = "fas fa-code", Color = "#722ED1", SortOrder = 6 },
new() { Name = "文本工具", Description = "文本处理和编辑工具", Icon = "fas fa-font", Color = "#13C2C2", SortOrder = 7 },
new() { Name = "生活娱乐", Description = "日常生活和娱乐工具", Icon = "fas fa-gamepad", Color = "#FA8C16", SortOrder = 8 }
};
await freeSql.Insert(categories).ExecuteAffrowsAsync();
// 添加示例工具
var tools = new List<ToolHub.Models.Tool>
{
new() { Name = "PDF转Word", Description = "高效将PDF文件转换为可编辑的Word文档,保留原始格式和排版",
Icon = "fas fa-file-word", Image = "https://design.gemcoder.com/staticResource/echoAiSystemImages/33769d8fd6cd6c3290700a9a1849a860.png",
CategoryId = 1, IsHot = true, Rating = 4.9m, RatingCount = 1250, ViewCount = 125000, SortOrder = 1 },
new() { Name = "图片压缩", Description = "减小图片文件大小,保持高质量,支持批量处理多种图片格式",
Icon = "fas fa-compress", Image = "https://design.gemcoder.com/staticResource/echoAiSystemImages/34a8fb1fdc206c992dddf5cda0318963.png",
CategoryId = 2, IsHot = true, Rating = 4.8m, RatingCount = 980, ViewCount = 98000, SortOrder = 2 },
new() { Name = "证件照生成", Description = "快速制作符合各国签证和证件要求的标准证件照,支持多种尺寸",
Icon = "fas fa-user-circle", Image = "https://design.gemcoder.com/staticResource/echoAiSystemImages/47b898b061a999bcbb5ee1defbdb6800.png",
CategoryId = 2, IsRecommended = true, Rating = 4.7m, RatingCount = 750, ViewCount = 75000, SortOrder = 3 },
new() { Name = "在线录屏", Description = "无需安装软件,直接在浏览器中录制屏幕、摄像头和音频,支持多种格式导出",
Icon = "fas fa-video", Image = "https://design.gemcoder.com/staticResource/echoAiSystemImages/930732ebbce6d0418fefe467d9f74ebc.png",
CategoryId = 3, IsNew = true, Rating = 4.6m, RatingCount = 520, ViewCount = 52000, SortOrder = 4 },
new() { Name = "PDF转Excel", Description = "将PDF表格数据转换为可编辑的Excel文件",
Icon = "fas fa-file-excel", CategoryId = 4, IsNew = true, Rating = 4.5m, RatingCount = 320, ViewCount = 32000, SortOrder = 5 },
new() { Name = "房贷利率计算器", Description = "计算不同贷款方式下的月供和总利息",
Icon = "fas fa-calculator", CategoryId = 5, Rating = 4.6m, RatingCount = 890, ViewCount = 89000, SortOrder = 6 }
};
await freeSql.Insert(tools).ExecuteAffrowsAsync();
// 添加默认管理员用户
var adminPassword = "admin123"; // 实际应用中应该使用更安全的密码
using var sha256 = System.Security.Cryptography.SHA256.Create();
var hashedBytes = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(adminPassword + "ToolHub_Salt"));
var hashedPassword = Convert.ToBase64String(hashedBytes);
var admin = new ToolHub.Models.User
{
UserName = "admin",
Email = "admin@toolhub.com",
PasswordHash = hashedPassword,
NickName = "管理员",
Role = "Admin"
};
await freeSql.Insert(admin).ExecuteAffrowsAsync();
}