using Dpz.Core.Hangfire;
using Dpz.Core.Web.Library.Activator;
using Dpz.Core.Web.Library.Hub;
using Hangfire;
using Microsoft.AspNetCore.Localization;
namespace Dpz.Core.Web.Library;
public static class AppExtensions
{
/// <summary>
/// 添加Hangfire 服务
/// </summary>
/// <returns></returns>
public static void HangfireService(this IServiceCollection services, IConfiguration configuration)
{
services.AddHangfireService(configuration, "Core");
}
/// <summary>
/// hangfire UI设置及周期性任务调度
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static void HangfireSetting(this IApplicationBuilder app)
{
var supportedCultures = new[]
{
new CultureInfo("zh-CN"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("zh-CN"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
});
//Note 系统初始化后再调用计划任务
//var cnBataLoggerService = (ILogger<CnBateTaskActivator>)app.ApplicationServices.GetService(typeof(ILogger<CnBateTaskActivator>));
//GlobalConfiguration.Configuration.UseActivator(new CnBateTaskActivator(cnBataLoggerService));
app.UseHangfireDashboard("/runtask", new DashboardOptions
{
Authorization = new[] { new HangfireFilter() },
AppPath = "/",
StatsPollingInterval = 5000,
//可编辑任务
IsReadOnlyFunc = _ => false,
DarkModeEnabled = true,
DashboardTitle = "任务管理",
FaviconPath = "/favicon.ico"
});
/*
知乎更新反爬虫机制,需要x-zse-96 header,有空研究知乎的加密机制
// 每天UTC时间10点 北京时间18点 删除热榜旧数据
RecurringJob.AddOrUpdate<HotTopicTaskActivator>(x => x.AutoClearHotTopic(), "0 0 10 * * ?");
// 每天UTC时间6点 北京时间12点 发布今日热榜
RecurringJob.AddOrUpdate<HotTopicTaskActivator>(x => x.PublishHotTopic(), "0 0 6 * * ?");
*/
//每周一 UTC时间22点 北京时间6点 生成站点地图
RecurringJob.AddOrUpdate<SiteMapTaskActivator>("站点地图",x => x.StartAsync(), "0 0 22 ? * SUN");
//每天 UTC时间1点 北京时间9点 获取网易每日推荐歌曲
//RecurringJob.AddOrUpdate<NetEasyMusicActivator>(x => x.StartAsync(), "0 0 1 * * ?");
// //每天 UTC时间17点 北京时间1点 删除旧日志
// RecurringJob.AddOrUpdate<LogTaskActivator>(x => x.ClearStart(), "0 0 17 * * ?");
// //每隔 5小时删除旧的CnBeta数据
// RecurringJob.AddOrUpdate<CnBetaTaskActivator>(x => x.DeleteAsync(), "1 */5 * * *");
//#if DEBUG == false
// //每隔 3小时获取最新CnBeta数据
// RecurringJob.AddOrUpdate<CnBetaTaskActivator>(x => x.Start(), "1 */3 * * *");
//
// //每天 UTC时间20点 北京时间4点 执行任务
// RecurringJob.AddOrUpdate<SteamActivator>(x => x.UpdateLibraryAsync(), "0 0 20 ? * ? ");
//#endif
// //每天 UTC时间19点 北京时间3点 执行任务
// RecurringJob.AddOrUpdate<TaskListHandleActivator>(x => x.HandleAsync(), "0 0 19 ? * ? ");
}
public static void ConfigurationRoute(this IApplicationBuilder app)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("mumble", "mumble.html",
new { controller = "Talk", action = "Index", pageIndex = 1 });
endpoints.MapControllerRoute("mumble-page", "mumble-{pageIndex}.html",
new { controller = "Talk", action = "Index" });
endpoints.MapControllerRoute("mumble-comment", "mumble/comment/{id}",
new { controller = "Talk", action = "Comment" });
endpoints.MapControllerRoute("talk", "talk.html",
new { controller = "Talk", action = "Index", pageIndex = 1 });
endpoints.MapControllerRoute("talkPager", "talk-{pageIndex}.html",
new { controller = "Talk", action = "Index" });
endpoints.MapControllerRoute("talk-comment", "talk/comment/{id}",
new { controller = "Talk", action = "Comment" });
endpoints.MapControllerRoute("ArticleFirstPage", "article/list.html",
new { controller = "Article", action = "Index", pageIndex = 1 });
endpoints.MapControllerRoute("ArticlePage", "article/list-{pageIndex}.html",
new { controller = "Article", action = "Index" });
endpoints.MapControllerRoute("Article", "article/read/{id}.html",
new { controller = "Article", action = "Read" });
endpoints.MapControllerRoute("login", "login.html", new { controller = "Home", Action = "Login" });
endpoints.MapControllerRoute(
"area",
"{area:exists}/{controller=Main}/{action=Index}/{id?}"
);
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
endpoints.MapHub<ChatHub>("/chathub");
endpoints.MapHub<RobotChatHub>("/robotChat");
endpoints.MapHub<Notification>("/notification");
endpoints.MapHub<BarrageHub>("/barrage");
endpoints.MapHub<ApplicationNotificationHub>("/app/notification");
});
}
}