添加了Swagger API文档以及诸多优化
This commit is contained in:
@@ -64,6 +64,12 @@ func (s *Server) Start() error {
|
||||
|
||||
// API路由
|
||||
if s.config.EnableAPI {
|
||||
// 重定向/api到Swagger UI页面
|
||||
mux.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/api/index.html", http.StatusMovedPermanently)
|
||||
})
|
||||
|
||||
// 注册所有API端点
|
||||
mux.HandleFunc("/api/stats", s.handleStats)
|
||||
mux.HandleFunc("/api/shield", s.handleShield)
|
||||
mux.HandleFunc("/api/shield/localrules", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -102,9 +108,13 @@ func (s *Server) Start() error {
|
||||
mux.HandleFunc("/api/query/type", s.handleQueryTypeStats)
|
||||
// WebSocket端点
|
||||
mux.HandleFunc("/ws/stats", s.handleWebSocketStats)
|
||||
|
||||
// 将/api/下的静态文件服务指向static/api目录,放在最后以避免覆盖API端点
|
||||
apiFileServer := http.FileServer(http.Dir("./static/api"))
|
||||
mux.Handle("/api/", http.StripPrefix("/api", apiFileServer))
|
||||
}
|
||||
|
||||
// 自定义静态文件服务处理器,用于禁用浏览器缓存
|
||||
// 自定义静态文件服务处理器,用于禁用浏览器缓存,放在API路由之后
|
||||
fileServer := http.FileServer(http.Dir("./static"))
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// 添加Cache-Control头,禁用浏览器缓存
|
||||
@@ -135,6 +145,14 @@ func (s *Server) Stop() {
|
||||
}
|
||||
|
||||
// handleStats 处理统计信息请求
|
||||
// @Summary 获取系统统计信息
|
||||
// @Description 获取DNS服务器和Shield的统计信息
|
||||
// @Tags stats
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} map[string]interface{} "统计信息"
|
||||
// @Failure 500 {object} map[string]string "服务器内部错误"
|
||||
// @Router /api/stats [get]
|
||||
func (s *Server) handleStats(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -610,7 +628,18 @@ func (s *Server) handleTopDomains(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(domainList)
|
||||
}
|
||||
|
||||
// handleShield 处理屏蔽规则管理请求
|
||||
// handleShield 处理Shield相关操作
|
||||
// @Summary 管理Shield配置
|
||||
// @Description 获取或更新Shield的配置信息
|
||||
// @Tags shield
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param config body map[string]interface{} false "Shield配置信息"
|
||||
// @Success 200 {object} map[string]interface{} "配置信息"
|
||||
// @Failure 400 {object} map[string]string "请求参数错误"
|
||||
// @Failure 500 {object} map[string]string "服务器内部错误"
|
||||
// @Router /api/shield [get]
|
||||
// @Router /api/shield [post]
|
||||
func (s *Server) handleShield(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
@@ -690,7 +719,22 @@ func (s *Server) handleShield(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// handleShieldBlacklists 处理远程黑名单管理请求
|
||||
// handleShieldBlacklists 处理黑名单相关操作
|
||||
// @Summary 管理黑名单
|
||||
// @Description 处理黑名单的CRUD操作,包括获取列表、添加、更新和删除黑名单
|
||||
// @Tags shield
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param name path string false "黑名单名称(用于删除操作)"
|
||||
// @Param blacklist body map[string]interface{} false "黑名单信息(用于添加/更新操作)"
|
||||
// @Success 200 {object} map[string]interface{} "操作成功"
|
||||
// @Failure 400 {object} map[string]string "请求参数错误"
|
||||
// @Failure 404 {object} map[string]string "黑名单不存在"
|
||||
// @Failure 500 {object} map[string]string "服务器内部错误"
|
||||
// @Router /api/shield/blacklists [get]
|
||||
// @Router /api/shield/blacklists [post]
|
||||
// @Router /api/shield/blacklists [put]
|
||||
// @Router /api/shield/blacklists/{name} [delete]
|
||||
func (s *Server) handleShieldBlacklists(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user