-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsearch.go
88 lines (83 loc) · 2.36 KB
/
search.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
83
84
85
86
87
88
package proxy
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/jpfielding/gorets/pkg/rets"
)
// Search ...
func Search(ops map[string]string, srcs Sources) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
sub := strings.TrimPrefix(req.URL.Path, ops["Search"])
parts := strings.Split(sub, "/")
src := parts[0]
usr := parts[1]
if _, ok := srcs[src]; !ok {
res.WriteHeader(http.StatusNotFound)
fmt.Fprintf(res, "source %s not found", src)
return
}
if _, ok := srcs[src][usr]; !ok {
res.WriteHeader(http.StatusNotFound)
fmt.Fprintf(res, "user %s not found", usr)
return
}
session := srcs[src][usr]
r, urls, err := session.Get()
if err != nil {
res.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(res, "source %s, user %s login failed", src, usr)
return
}
// TODO also check body in case of POST params in body
values := req.URL.Query()
params := rets.SearchParams{
SearchType: values.Get("SearchType"),
Class: values.Get("Class"),
Query: values.Get("Query"),
}
// TODO apply optionally
params.Format = values.Get("Format")
params.Select = values.Get("Select")
params.Payload = values.Get("Payload")
params.QueryType = values.Get("QueryType")
params.RestrictedIndicator = values.Get("RestrictedIndicator")
if c := values.Get("Count"); c != "" {
params.Count, _ = strconv.Atoi(c)
}
if l := values.Get("Limit"); l != "" {
if strings.ToUpper(l) == "NONE" {
params.Limit = -1
} else {
params.Limit, _ = strconv.Atoi(l)
}
}
if o := values.Get("Offset"); o != "" {
params.Offset, _ = strconv.Atoi(o)
}
if s := values.Get("StandardNames"); s != "" {
params.StandardNames, _ = strconv.Atoi(s)
}
// TODO standardnames
ctx := context.Background()
response, err := rets.SearchResponse(ctx, r, rets.SearchRequest{
URL: urls.Search,
HTTPMethod: req.Method,
HTTPFormEncodedValues: (req.Method == "POST"),
SearchParams: params,
})
defer response.Body.Close()
if err != nil {
res.WriteHeader(http.StatusBadGateway)
fmt.Fprintf(res, "search err %s", err)
return
}
// success, send the urls (modified to point to this server)
res.Header()["Content-Type"] = response.Header["Content-Type"]
res.WriteHeader(http.StatusOK)
io.Copy(res, response.Body)
}
}