handleMasterQuery allows an app to register the master query that defines the domain limiting all subsequent search queries.
(w http.ResponseWriter, r *http.Request)
| 108 | // handleMasterQuery allows an app to register the master query that defines the |
| 109 | // domain limiting all subsequent search queries. |
| 110 | func (a *Handler) handleMasterQuery(w http.ResponseWriter, r *http.Request) { |
| 111 | if !(a.auth.AllowedAccess(r)&auth.OpAll == auth.OpAll) { |
| 112 | auth.SendUnauthorized(w, r) |
| 113 | return |
| 114 | } |
| 115 | if r.Method != http.MethodPost { |
| 116 | http.Error(w, "not a POST", http.StatusMethodNotAllowed) |
| 117 | return |
| 118 | } |
| 119 | if a.sh == nil { |
| 120 | http.Error(w, "app proxy has no search handler", 500) |
| 121 | return |
| 122 | } |
| 123 | if refresh, _ := strconv.ParseBool(r.FormValue("refresh")); refresh { |
| 124 | if err := a.refreshDomainBlobs(); err != nil { |
| 125 | if err == errRefreshSuppress { |
| 126 | http.Error(w, "too many refresh requests", http.StatusTooManyRequests) |
| 127 | } else { |
| 128 | http.Error(w, fmt.Sprintf("%v", err), 500) |
| 129 | } |
| 130 | return |
| 131 | } |
| 132 | w.Write([]byte("OK")) |
| 133 | return |
| 134 | } |
| 135 | sq := new(search.SearchQuery) |
| 136 | if err := sq.FromHTTP(r); err != nil { |
| 137 | http.Error(w, fmt.Sprintf("error reading master query: %v", err), 500) |
| 138 | return |
| 139 | } |
| 140 | masterQuery := *sq |
| 141 | masterQuery.Describe = masterQuery.Describe.Clone() |
| 142 | sr, err := a.sh.Query(r.Context(), sq) |
| 143 | if err != nil { |
| 144 | http.Error(w, fmt.Sprintf("error running master query: %v", err), 500) |
| 145 | return |
| 146 | } |
| 147 | a.masterQueryMu.Lock() |
| 148 | defer a.masterQueryMu.Unlock() |
| 149 | a.masterQuery = &masterQuery |
| 150 | a.domainBlobs = make(map[blob.Ref]bool, len(sr.Describe.Meta)) |
| 151 | for _, v := range sr.Describe.Meta { |
| 152 | a.domainBlobs[v.BlobRef] = true |
| 153 | } |
| 154 | a.domainBlobsRefresh = time.Now() |
| 155 | w.Write([]byte("OK")) |
| 156 | } |
| 157 | |
| 158 | var errRefreshSuppress = errors.New("refresh request suppressed") |
| 159 |
no test coverage detected