package auth import ( "encoding/json" "net/http" "networktool/internal/config" "networktool/internal/model" ) func Middleware(cfg config.Config, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { password := r.Header.Get("X-Admin-Password") if password == "" { writeJSON(w, http.StatusUnauthorized, model.APIResponse{Code: 1001, Message: "缺少密码", Data: nil}) return } if password != cfg.AdminPassword { writeJSON(w, http.StatusUnauthorized, model.APIResponse{Code: 1002, Message: "密码错误", Data: nil}) return } next.ServeHTTP(w, r) }) } func writeJSON(w http.ResponseWriter, status int, payload model.APIResponse) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) _ = json.NewEncoder(w).Encode(payload) }