func()

in internal/server/server.go [227:287]


func (s *IndexServer) handleIndex() http.HandlerFunc {
	route := "index"

	type response struct {
		Success bool
	}

	parseRequest := func(r *http.Request) (IndexRequest, error) {
		var req IndexRequest
		err := s.decode(r, &req)

		if err != nil {
			return req, errors.New("json parser error")
		}

		return req, nil
	}

	return func(w http.ResponseWriter, r *http.Request) {
		req, err := parseRequest(r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}

		if s.overIndexingLimit() {
			http.Error(w, "over concurrency limit", http.StatusTooManyRequests)
			return
		}

		if !s.IndexingLock.TryLock(req.RepoID) {
			s.respondWithError(w, r, route, errors.New("indexing is already in progress"), http.StatusLocked)
			return
		}
		defer s.IndexingLock.Unlock(req.RepoID)

		err = s.IndexBuilder.IndexRepository(
			r.Context(),
			req,
			callback.CallbackFunc{
				OnSuccess: func(params callback.CallbackParams) { //nolint:contextcheck
					s.CallbackAPI.SendSuccess(r.Context(), params, s.IndexBuilder.GetIndexDir(), req.RepoID)
				},
				OnFailure: func(params callback.CallbackParams, errorReason error) { //nolint:contextcheck
					s.CallbackAPI.SendFailure(r.Context(), params, s.IndexBuilder.GetIndexDir(), req.RepoID, errorReason)
				},
			},
		)

		if err != nil {
			s.respondWithError(w, r, route, err, http.StatusInternalServerError)
			return
		}

		resp := response{
			Success: true,
		}

		s.respondWith(w, r, route, resp)
	}
}