-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.go
More file actions
60 lines (46 loc) · 1.59 KB
/
debug.go
File metadata and controls
60 lines (46 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package peerdb
import (
"net/http"
internalSite "gitlab.com/peerdb/peerdb/internal/site"
"gitlab.com/peerdb/peerdb/store"
"gitlab.com/tozd/go/errors"
"gitlab.com/tozd/waf"
internalSearch "gitlab.com/peerdb/peerdb/internal/search"
)
// DebugMappingGetAPI handles GET requests to serve generated ElasticSearch mapping for debugging.
func (s *Service) DebugMappingGetAPI(w http.ResponseWriter, req *http.Request, _ waf.Params) {
if !s.Development {
s.NotFoundWithError(w, req, errors.New("not in development mode"))
return
}
site := waf.MustGetSite[*internalSite.Site](req.Context())
indexConfiguration, errE := internalSearch.Mapping(site.LanguagePriority)
if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, indexConfiguration, nil)
}
// DebugIndexedGetAPI handles GET requests to return a document as it would be converted and indexed to ElasticSearch.
func (s *Service) DebugIndexedGetAPI(w http.ResponseWriter, req *http.Request, params waf.Params) {
if !s.Development {
s.NotFoundWithError(w, req, errors.New("not in development mode"))
return
}
ctx := req.Context()
dataJSON, metadata, version, handled := s.documentGetData(w, req, params)
if handled {
return
}
site := waf.MustGetSite[*internalSite.Site](ctx)
searchDoc, errE := site.Base.IndexedDocument(ctx, dataJSON, metadata)
if errors.Is(errE, store.ErrAccessDenied) {
s.ForbiddenWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
w.Header().Set("Version", version.String())
s.WriteJSON(w, req, searchDoc, nil)
}