fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

indexserver: add endpoint /debug/reindex (#497)

Now, reindex requests are handled asynchronously, which means the client
won't have to wait around for big repos to be indexed. This makes things
easier to handle once we call this endpoint from Sourcegraph.

+34 -2
+34 -2
cmd/zoekt-sourcegraph-indexserver/main.go
··· 647 647 func (s *Server) addDebugHandlers(mux *http.ServeMux) { 648 648 // Sourcegraph's site admin view requires indexserver to serve it's admin view 649 649 // on "/". 650 - mux.Handle("/", http.HandlerFunc(s.handleReIndex)) 650 + mux.Handle("/", http.HandlerFunc(s.handleRoot)) 651 651 652 + mux.Handle("/debug/reindex", http.HandlerFunc(s.handleReindex)) 652 653 mux.Handle("/debug/indexed", http.HandlerFunc(s.handleDebugIndexed)) 653 654 mux.Handle("/debug/list", http.HandlerFunc(s.handleDebugList)) 654 655 mux.Handle("/debug/merge", http.HandlerFunc(s.handleDebugMerge)) ··· 670 671 </body></html> 671 672 `)) 672 673 673 - func (s *Server) handleReIndex(w http.ResponseWriter, r *http.Request) { 674 + func (s *Server) handleRoot(w http.ResponseWriter, r *http.Request) { 674 675 renderRoot := func(indexMsg string) { 675 676 type Repo struct { 676 677 ID uint32 ··· 711 712 712 713 indexMsg, err := s.forceIndex(uint32(id)) 713 714 715 + // TODO: we won't need "headless" once Sourcegraph calls 716 + // "/debug/handleReindex". 717 + 714 718 // ?headless 715 719 if _, ok := r.URL.Query()["headless"]; ok { 716 720 if err != nil { ··· 726 730 w.Header().Set("Allow", "GET, POST") 727 731 w.WriteHeader(http.StatusMethodNotAllowed) 728 732 } 733 + } 734 + 735 + // handleReindex triggers a reindex asynocronously. If a reindex was triggered 736 + // the request returns with status 202. The caller can infer the new state of 737 + // the index by calling List. 738 + func (s *Server) handleReindex(w http.ResponseWriter, r *http.Request) { 739 + if r.Method != http.MethodPost { 740 + w.Header().Set("Allow", http.MethodPost) 741 + w.WriteHeader(http.StatusMethodNotAllowed) 742 + return 743 + } 744 + 745 + err := r.ParseForm() 746 + if err != nil { 747 + http.Error(w, err.Error(), http.StatusBadRequest) 748 + return 749 + } 750 + 751 + id, err := strconv.Atoi(r.Form.Get("repo")) 752 + if err != nil { 753 + http.Error(w, err.Error(), http.StatusBadRequest) 754 + return 755 + } 756 + 757 + go func() { s.forceIndex(uint32(id)) }() 758 + 759 + // 202 Accepted 760 + w.WriteHeader(http.StatusAccepted) 729 761 } 730 762 731 763 func (s *Server) handleDebugList(w http.ResponseWriter, r *http.Request) {