- 新增 SIGINT/SIGTERM 信号处理与 http.Server.Shutdown,退出前确保统计数据保存、数据库正常关闭 - 为 http.Server 设置 ReadHeaderTimeout/ReadTimeout/WriteTimeout/IdleTimeout,防止慢客户端长期占用连接 - 全局配置指针改为 atomic.Pointer 并提供 GetAppConfig 访问器,消除配置热更新时的数据竞争,同时删除冗余的 manager.config 字段 - 请求统计九组计数器重构为 map[string]*atomic.Int64,统一 incrementStat/getStatsSnapshot 访问,精简约 40 行样板代码 - go mod tidy 清理 go.mod 与 go.sum 中的冗余依赖条目
88 lines
2.0 KiB
Go
88 lines
2.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fanqie/internal/fqapi"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
// HandleSearch 搜索处理
|
|
func HandleSearch(w http.ResponseWriter, r *http.Request) {
|
|
incrementStat("total", "search")
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
query := r.URL.Query().Get("query")
|
|
if query == "" {
|
|
query = r.URL.Query().Get("key")
|
|
}
|
|
|
|
offsetStr := r.URL.Query().Get("offset")
|
|
offset := 0
|
|
if offsetStr != "" {
|
|
offset, _ = strconv.Atoi(offsetStr)
|
|
}
|
|
|
|
tabTypeStr := r.URL.Query().Get("tab_type")
|
|
tabType := 3
|
|
if tabTypeStr != "" {
|
|
tabType, _ = strconv.Atoi(tabTypeStr)
|
|
}
|
|
|
|
result, err := fqapi.HandleBookSearch(query, offset, tabType)
|
|
if err != nil {
|
|
fmt.Fprintf(w, `{"code": -1, "msg": "搜索失败: %s"}`, err.Error())
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, result)
|
|
}
|
|
|
|
// HandleDetail 详情处理
|
|
func HandleDetail(w http.ResponseWriter, r *http.Request) {
|
|
incrementStat("total", "detail")
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
bookID := r.URL.Query().Get("book_id")
|
|
|
|
result, err := fqapi.HandleGetDetail(bookID)
|
|
if err != nil {
|
|
fmt.Fprintf(w, `{"code": -1, "msg": "获取详情失败: %s"}`, err.Error())
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, result)
|
|
}
|
|
|
|
// HandleCatalog 目录处理
|
|
func HandleCatalog(w http.ResponseWriter, r *http.Request) {
|
|
incrementStat("total", "catalog")
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
bookID := r.URL.Query().Get("book_id")
|
|
|
|
result, err := fqapi.HandleGetCatalog(bookID)
|
|
if err != nil {
|
|
fmt.Fprintf(w, `{"code": -1, "msg": "获取目录失败: %s"}`, err.Error())
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, result)
|
|
}
|
|
|
|
// HandleMultiDetail 批量详情处理
|
|
func HandleMultiDetail(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
bookID := r.URL.Query().Get("book_id")
|
|
|
|
result, err := fqapi.HandleGetMultiDetail(bookID)
|
|
if err != nil {
|
|
fmt.Fprintf(w, `{"code": -1, "msg": "批量获取详情失败: %s"}`, err.Error())
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, result)
|
|
}
|
|
|