Skip to content
Snippets Groups Projects

Full MVC of gitlab-zoekt-indexer

Merged Dylan Griffith requested to merge add-new-api-paths into main
All threads resolved!
Compare and Show latest version
5 files
+ 74
62
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -6,7 +6,6 @@ import (
@@ -6,7 +6,6 @@ import (
"errors"
"errors"
"flag"
"flag"
"fmt"
"fmt"
"io"
"log"
"log"
"net/http"
"net/http"
"os"
"os"
@@ -14,6 +13,7 @@ import (
@@ -14,6 +13,7 @@ import (
"strconv"
"strconv"
"time"
"time"
 
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -53,18 +53,11 @@ func (s *indexServer) createIndexDir() {
@@ -53,18 +53,11 @@ func (s *indexServer) createIndexDir() {
}
}
}
}
func (s *indexServer) handleHealthCheck() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Nothing to do. Just return 200
}
}
func (s *indexServer) handleStatus() http.HandlerFunc {
func (s *indexServer) handleStatus() http.HandlerFunc {
route := "status"
route := "status"
return func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
param := r.URL.Query().Get("id")
param := chi.URLParam(r, "id")
repoID, err := strconv.ParseUint(param, 10, 32)
repoID, err := strconv.ParseUint(param, 10, 32)
if err != nil {
if err != nil {
@@ -80,24 +73,21 @@ func (s *indexServer) handleStatus() http.HandlerFunc {
@@ -80,24 +73,21 @@ func (s *indexServer) handleStatus() http.HandlerFunc {
currentSHA, err := idx.CurrentSHA()
currentSHA, err := idx.CurrentSHA()
if err != nil {
if err != nil {
s.respondWithError(w, r.Method, route, err)
s.respondWithError(w, r, route, err)
return
return
}
}
response := map[string]any{
"Success": true,
"SHA": currentSHA,
}
if err != nil {
if err != nil {
s.respondWithError(w, r.Method, route, err)
s.respondWithError(w, r, route, err)
return
return
}
}
w.Header().Set("Content-Type", "application/json")
response := map[string]any{
_ = json.NewEncoder(w).Encode(response)
"Success": true,
 
"SHA": currentSHA,
 
}
s.incrementRequestsTotal(r.Method, route, http.StatusOK)
s.respondWith(w, r, route, response)
}
}
}
}
@@ -107,14 +97,18 @@ func (s *indexServer) handleMetrics() http.HandlerFunc {
@@ -107,14 +97,18 @@ func (s *indexServer) handleMetrics() http.HandlerFunc {
}
}
}
}
func (s *indexServer) serveIndex() http.HandlerFunc {
func (s *indexServer) decode(r *http.Request, v interface{}) error {
 
dec := json.NewDecoder(r.Body)
 
dec.DisallowUnknownFields()
 
return dec.Decode(v)
 
}
 
 
func (s *indexServer) handleIndex() http.HandlerFunc {
route := "index"
route := "index"
parseRequest := func(r io.Reader) (indexRequest, error) {
parseRequest := func(r *http.Request) (indexRequest, error) {
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()
var req indexRequest
var req indexRequest
err := dec.Decode(&req)
err := s.decode(r, &req)
if err != nil {
if err != nil {
return req, errors.New("json parser error")
return req, errors.New("json parser error")
@@ -132,29 +126,30 @@ func (s *indexServer) serveIndex() http.HandlerFunc {
@@ -132,29 +126,30 @@ func (s *indexServer) serveIndex() http.HandlerFunc {
}
}
return func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
req, err := parseRequest(r.Body)
req, err := parseRequest(r)
if err != nil {
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, err.Error(), http.StatusBadRequest)
return
return
}
}
response, err := s.indexRepository(req)
err = s.indexRepository(req)
if err != nil {
if err != nil {
s.respondWithError(w, r.Method, route, err)
s.respondWithError(w, r, route, err)
return
return
}
}
w.Header().Set("Content-Type", "application/json")
response := map[string]any{
_ = json.NewEncoder(w).Encode(response)
"Success": true,
 
}
s.incrementRequestsTotal(r.Method, route, http.StatusOK)
s.respondWith(w, r, route, response)
}
}
}
}
func (s *indexServer) indexRepository(req indexRequest) (map[string]any, error) {
func (s *indexServer) indexRepository(req indexRequest) error {
timeout, err := time.ParseDuration(req.Timeout)
timeout, err := time.ParseDuration(req.Timeout)
if err != nil {
if err != nil {
return nil, fmt.Errorf("failed to parse Timeout: %v with error %v", req.Timeout, err)
return fmt.Errorf("failed to parse Timeout: %v with error %v", req.Timeout, err)
}
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
@@ -171,14 +166,10 @@ func (s *indexServer) indexRepository(req indexRequest) (map[string]any, error)
@@ -171,14 +166,10 @@ func (s *indexServer) indexRepository(req indexRequest) (map[string]any, error)
}
}
if err := idx.IndexRepository(ctx); err != nil {
if err := idx.IndexRepository(ctx); err != nil {
return nil, err
return err
}
response := map[string]any{
"Success": true,
}
}
return response, nil
return nil
}
}
func (s *indexServer) handleTruncate() http.HandlerFunc {
func (s *indexServer) handleTruncate() http.HandlerFunc {
@@ -189,24 +180,32 @@ func (s *indexServer) handleTruncate() http.HandlerFunc {
@@ -189,24 +180,32 @@ func (s *indexServer) handleTruncate() http.HandlerFunc {
if err != nil {
if err != nil {
err = fmt.Errorf("failed to empty indexDir: %v with error: %v", s.indexDir, err)
err = fmt.Errorf("failed to empty indexDir: %v with error: %v", s.indexDir, err)
s.respondWithError(w, r.Method, route, err)
s.respondWithError(w, r, route, err)
return
return
}
}
response := map[string]any{
response := map[string]any{
"Success": true,
"Success": true,
}
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(response)
s.incrementRequestsTotal(r.Method, route, http.StatusOK)
s.respondWith(w, r, route, response)
}
}
}
}
func (s *indexServer) respondWithError(w http.ResponseWriter, method, route string, err error) {
func (s *indexServer) respondWith(w http.ResponseWriter, r *http.Request, route string, data interface{}) {
 
w.Header().Set("Content-Type", "application/json")
 
 
if err := json.NewEncoder(w).Encode(data); err != nil {
 
s.respondWithError(w, r, route, err)
 
}
 
 
s.incrementRequestsTotal(r.Method, route, http.StatusOK)
 
}
 
 
func (s *indexServer) respondWithError(w http.ResponseWriter, r *http.Request, route string, err error) {
responseCode := http.StatusInternalServerError
responseCode := http.StatusInternalServerError
s.incrementRequestsTotal(method, route, responseCode)
s.incrementRequestsTotal(r.Method, route, responseCode)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(responseCode)
w.WriteHeader(responseCode)
@@ -286,11 +285,11 @@ func main() {
@@ -286,11 +285,11 @@ func main() {
func (s *indexServer) startIndexingApi(listen string) {
func (s *indexServer) startIndexingApi(listen string) {
s.initMetrics()
s.initMetrics()
s.createIndexDir()
s.createIndexDir()
s.routes()
router := s.router()
log.Printf("Starting server on %s", listen)
log.Printf("Starting server on %s", listen)
if err := http.ListenAndServe(listen, nil); err != nil {
if err := http.ListenAndServe(listen, router); err != nil {
log.Fatal(err)
log.Fatal(err)
}
}
}
}
Loading