forked from HouzuoGuo/tiedot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
82 lines (78 loc) · 1.82 KB
/
index.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Index management handlers. */
package v3
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
func Index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "application/json")
var col, path string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "path", &path) {
return
}
V3Sync.Lock()
defer V3Sync.Unlock()
dbcol := V3DB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
if err := dbcol.Index(strings.Split(path, ",")); err != nil {
http.Error(w, fmt.Sprint(err), 400)
return
}
w.WriteHeader(201)
}
func Indexes(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "application/json")
var col string
if !Require(w, r, "col", &col) {
return
}
V3Sync.Lock()
defer V3Sync.Unlock()
dbcol := V3DB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
indexes := make([]string, 0)
for path := range dbcol.SecIndexes {
indexes = append(indexes, path)
}
resp, err := json.Marshal(indexes)
if err != nil {
http.Error(w, fmt.Sprint("Server error."), 500)
return
}
w.Write(resp)
}
func Unindex(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "application/json")
var col, path string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "path", &path) {
return
}
V3Sync.Lock()
defer V3Sync.Unlock()
dbcol := V3DB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
if err := dbcol.Unindex(strings.Split(path, ",")); err != nil {
http.Error(w, fmt.Sprint(err), 400)
return
}
}