🔧 chore: 初始化番茄小说 API 项目结构
新增项目基础框架,包含核心 API 实现、算法模块、HTTP 处理器等完整功能模块
This commit is contained in:
commit
6981148727
107 files changed
+12463
No files matched your search
@@ -0,0 +1,182 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fanqie/internal/algos"
|
||||
"fanqie/internal/common"
|
||||
"fanqie/internal/config"
|
||||
"fanqie/internal/fqapi"
|
||||
"fanqie/internal/handlers"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
logger = common.NewLogger("main", "INFO", "")
|
||||
configWatcher *config.ConfigWatcher
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger.Info("番茄小说API服务 - v2.0.0")
|
||||
|
||||
// 加载配置文件
|
||||
initConfig()
|
||||
|
||||
// 初始化数据库
|
||||
if err := fqapi.InitDB(); err != nil {
|
||||
logger.Error("数据库初始化失败: " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
defer fqapi.CloseCache() // 程序退出时关闭数据库
|
||||
|
||||
// 加载统计数据
|
||||
handlers.LoadStatistics()
|
||||
|
||||
// 启动统计数据定期保存
|
||||
handlers.StartStatisticsSaver()
|
||||
defer handlers.StopStatisticsSaver() // 程序退出时保存统计数据
|
||||
|
||||
// 启动缓存自动清理任务
|
||||
fqapi.StartCacheCleanup()
|
||||
defer fqapi.StopCacheCleanup() // 程序退出时停止清理任务
|
||||
|
||||
// 初始化算法切换器
|
||||
initAlgorithm()
|
||||
|
||||
// 初始化设备密钥池
|
||||
if err := initDeviceKeyPool(); err != nil {
|
||||
logger.Error("设备密钥池初始化失败: " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// 设置路由
|
||||
setupRoutes()
|
||||
|
||||
// 启动服务器
|
||||
startServer()
|
||||
}
|
||||
|
||||
// initConfig 初始化配置
|
||||
func initConfig() {
|
||||
configPath := "./config/config.yaml"
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
os.MkdirAll("./config", 0755)
|
||||
}
|
||||
|
||||
if err := config.LoadConfig(configPath); err != nil {
|
||||
logger.Warning(fmt.Sprintf("加载配置失败: %v", err))
|
||||
}
|
||||
|
||||
// 启动配置文件热更新监控
|
||||
watcher, err := config.StartConfigWatcher(configPath)
|
||||
if err != nil {
|
||||
logger.Warning(fmt.Sprintf("配置监控失败: %v", err))
|
||||
} else {
|
||||
configWatcher = watcher
|
||||
}
|
||||
|
||||
// 注册配置更新回调
|
||||
config.RegisterConfigCallback(func(cfg *config.Config) {
|
||||
algos.GlobalAlgorithmSwitcher.SwitchAlgorithm(cfg.Fanqie.Algorithm)
|
||||
algos.GlobalSignatureSwitcher.SwitchSignature(cfg.Fanqie.Signature)
|
||||
})
|
||||
}
|
||||
|
||||
// initAlgorithm 初始化算法
|
||||
func initAlgorithm() {
|
||||
if config.AppConfig != nil {
|
||||
algos.GlobalAlgorithmSwitcher.SwitchAlgorithm(config.AppConfig.Fanqie.Algorithm)
|
||||
logger.Info(fmt.Sprintf("当前算法: %s", config.AppConfig.Fanqie.Algorithm))
|
||||
|
||||
algos.GlobalSignatureSwitcher.SwitchSignature(config.AppConfig.Fanqie.Signature)
|
||||
|
||||
// 根据签名方法显示详细信息
|
||||
var signatureDetails string
|
||||
if config.AppConfig.Fanqie.Signature == "sign4" {
|
||||
signatureDetails = "X-Gorgon + X-Ladon + X-Argus + X-Khronos"
|
||||
} else {
|
||||
signatureDetails = "X-Gorgon + X-Khronos"
|
||||
}
|
||||
logger.Info(fmt.Sprintf("当前签名方法: %s", signatureDetails))
|
||||
} else {
|
||||
algos.GlobalAlgorithmSwitcher.SwitchAlgorithm("gorgon")
|
||||
algos.GlobalSignatureSwitcher.SwitchSignature("sign2")
|
||||
}
|
||||
}
|
||||
|
||||
// initDeviceKeyPool 初始化设备密钥池
|
||||
func initDeviceKeyPool() error {
|
||||
pool := fqapi.GetDeviceKeyPool()
|
||||
if err := pool.Initialize(); err != nil {
|
||||
return fmt.Errorf("初始化设备密钥池失败: %w", err)
|
||||
}
|
||||
|
||||
// 显示池状态
|
||||
stats := pool.GetStats()
|
||||
logger.Info(fmt.Sprintf("设备密钥池状态 - 总数: %d, 有效: %d, 无效: %d",
|
||||
stats["total"], stats["valid"], stats["invalid"]))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// setupRoutes 设置路由
|
||||
func setupRoutes() {
|
||||
http.HandleFunc("/", handlers.HandleMain)
|
||||
http.HandleFunc("/status", handlers.RequireAccessKey(handlers.HandleStatus))
|
||||
http.HandleFunc("/self-info", handlers.RequireAccessKey(handlers.HandleSelfInfo))
|
||||
http.HandleFunc("/vacuum", handlers.RequireAccessKey(handlers.HandleVacuum))
|
||||
|
||||
// API端点
|
||||
http.HandleFunc("/search", handlers.HandleSearch)
|
||||
http.HandleFunc("/detail", handlers.HandleDetail)
|
||||
http.HandleFunc("/info", handlers.HandleDetail)
|
||||
http.HandleFunc("/catalog", handlers.HandleCatalog)
|
||||
http.HandleFunc("/directory/all_items", handlers.HandleCatalog)
|
||||
http.HandleFunc("/multi-detail", handlers.HandleMultiDetail)
|
||||
http.HandleFunc("/multi-content", handlers.HandleMultiContent)
|
||||
http.HandleFunc("/content", handlers.HandleContent)
|
||||
http.HandleFunc("/item-summary", handlers.HandleItemSummary)
|
||||
http.HandleFunc("/category-front", handlers.HandleCategoryFront)
|
||||
http.HandleFunc("/category-landing", handlers.HandleCategoryLanding)
|
||||
http.HandleFunc("/audio", handlers.HandleAudio)
|
||||
http.HandleFunc("/audio/proxy/", handlers.HandleAudioProxy)
|
||||
http.HandleFunc("/video", handlers.HandleVideo)
|
||||
http.HandleFunc("/manga", handlers.HandleManga)
|
||||
http.HandleFunc("/manga/image/", handlers.HandleMangaImageProxy)
|
||||
|
||||
// 评论相关端点
|
||||
http.HandleFunc("/chapter_review", handlers.HandleChapterReview)
|
||||
http.HandleFunc("/review", handlers.HandleReview)
|
||||
http.HandleFunc("/review_html", handlers.HandleReviewHTML)
|
||||
|
||||
// 表情图片静态文件服务
|
||||
http.Handle("/emoji/", http.StripPrefix("/emoji/", http.FileServer(http.Dir("emoji"))))
|
||||
}
|
||||
|
||||
// startServer 启动服务器
|
||||
func startServer() {
|
||||
host := "0.0.0.0"
|
||||
port := "40180"
|
||||
|
||||
if config.AppConfig != nil {
|
||||
if config.AppConfig.Server.Host != "" {
|
||||
host = config.AppConfig.Server.Host
|
||||
}
|
||||
if config.AppConfig.Server.Port != "" {
|
||||
port = config.AppConfig.Server.Port
|
||||
}
|
||||
}
|
||||
|
||||
addr := host + ":" + port
|
||||
logger.Info(fmt.Sprintf("服务器启动在 %s", addr))
|
||||
|
||||
if err := http.ListenAndServe(addr, nil); err != nil {
|
||||
logger.Error(fmt.Sprintf("服务器启动失败: %v", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
}
|
||||
Reference in new issue
Block a user