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
1 file
+ 63
40
Compare changes
  • Side-by-side
  • Inline
@@ -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"
@@ -57,6 +56,11 @@ func (s *indexServer) createIndexDir() {
@@ -57,6 +56,11 @@ func (s *indexServer) createIndexDir() {
func (s *indexServer) handleStatus() http.HandlerFunc {
func (s *indexServer) handleStatus() http.HandlerFunc {
route := "status"
route := "status"
 
type response struct {
 
Success bool
 
SHA string
 
}
 
return func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
param := chi.URLParam(r, "id")
param := chi.URLParam(r, "id")
repoID, err := strconv.ParseUint(param, 10, 32)
repoID, err := strconv.ParseUint(param, 10, 32)
@@ -83,12 +87,12 @@ func (s *indexServer) handleStatus() http.HandlerFunc {
@@ -83,12 +87,12 @@ func (s *indexServer) handleStatus() http.HandlerFunc {
return
return
}
}
response := map[string]any{
resp := response{
"Success": true,
Success: true,
"SHA": currentSHA,
SHA: currentSHA,
}
}
s.respondWith(w, r, route, response)
s.respondWith(w, r, route, resp)
}
}
}
}
@@ -98,14 +102,22 @@ func (s *indexServer) handleMetrics() http.HandlerFunc {
@@ -98,14 +102,22 @@ func (s *indexServer) handleMetrics() 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 {
func (s *indexServer) handleIndex() http.HandlerFunc {
route := "index"
route := "index"
parseRequest := func(r io.Reader) (indexRequest, error) {
type response struct {
dec := json.NewDecoder(r)
Success bool
dec.DisallowUnknownFields()
}
 
 
parseRequest := func(r *http.Request) (indexRequest, error) {
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")
@@ -123,7 +135,7 @@ func (s *indexServer) handleIndex() http.HandlerFunc {
@@ -123,7 +135,7 @@ func (s *indexServer) handleIndex() 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
@@ -135,11 +147,11 @@ func (s *indexServer) handleIndex() http.HandlerFunc {
@@ -135,11 +147,11 @@ func (s *indexServer) handleIndex() http.HandlerFunc {
return
return
}
}
response := map[string]any{
resp := response{
"Success": true,
Success: true,
}
}
s.respondWith(w, r, route, response)
s.respondWith(w, r, route, resp)
}
}
}
}
@@ -171,6 +183,29 @@ func (s *indexServer) indexRepository(req indexRequest) error {
@@ -171,6 +183,29 @@ func (s *indexServer) indexRepository(req indexRequest) error {
func (s *indexServer) handleTruncate() http.HandlerFunc {
func (s *indexServer) handleTruncate() http.HandlerFunc {
route := "truncate"
route := "truncate"
 
 
type response struct {
 
Success bool
 
}
 
 
emptyDirectory := func(dir string) error {
 
files, err := os.ReadDir(dir)
 
 
if err != nil {
 
return err
 
}
 
 
for _, file := range files {
 
filePath := filepath.Join(dir, file.Name())
 
err := os.RemoveAll(filePath)
 
if err != nil {
 
return err
 
}
 
}
 
 
return nil
 
}
 
return func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
err := emptyDirectory(s.indexDir)
err := emptyDirectory(s.indexDir)
@@ -181,11 +216,11 @@ func (s *indexServer) handleTruncate() http.HandlerFunc {
@@ -181,11 +216,11 @@ func (s *indexServer) handleTruncate() http.HandlerFunc {
return
return
}
}
response := map[string]any{
resp := response{
"Success": true,
Success: true,
}
}
s.respondWith(w, r, route, response)
s.respondWith(w, r, route, resp)
}
}
}
}
@@ -200,18 +235,24 @@ func (s *indexServer) respondWith(w http.ResponseWriter, r *http.Request, route
@@ -200,18 +235,24 @@ func (s *indexServer) respondWith(w http.ResponseWriter, r *http.Request, route
}
}
func (s *indexServer) respondWithError(w http.ResponseWriter, r *http.Request, route string, err error) {
func (s *indexServer) respondWithError(w http.ResponseWriter, r *http.Request, route string, err error) {
responseCode := http.StatusInternalServerError
type response struct {
 
Success bool
 
Error string
 
}
s.incrementRequestsTotal(r.Method, route, responseCode)
responseCode := http.StatusInternalServerError
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(responseCode)
w.WriteHeader(responseCode)
response := map[string]any{
"Success": false,
resp := response{
"Error": err.Error(),
Success: false,
 
Error: err.Error(),
}
}
_ = json.NewEncoder(w).Encode(response)
_ = json.NewEncoder(w).Encode(resp)
 
 
s.incrementRequestsTotal(r.Method, route, responseCode)
}
}
func (s *indexServer) incrementRequestsTotal(method, route string, responseCode int) {
func (s *indexServer) incrementRequestsTotal(method, route string, responseCode int) {
@@ -236,24 +277,6 @@ func (s *indexServer) initMetrics() {
@@ -236,24 +277,6 @@ func (s *indexServer) initMetrics() {
)
)
}
}
func emptyDirectory(dir string) error {
files, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, file := range files {
filePath := filepath.Join(dir, file.Name())
err := os.RemoveAll(filePath)
if err != nil {
return err
}
}
return nil
}
func parseOptions() options {
func parseOptions() options {
indexDir := flag.String("index_dir", "", "directory holding index shards.")
indexDir := flag.String("index_dir", "", "directory holding index shards.")
listen := flag.String("listen", ":6060", "listen on this address.")
listen := flag.String("listen", ":6060", "listen on this address.")
Loading