-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
114 lines (107 loc) · 3.53 KB
/
process.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net/http"
"strings"
srvConfig "github.com/CHESSComputing/golib/config"
server "github.com/CHESSComputing/golib/server"
services "github.com/CHESSComputing/golib/services"
"github.com/gin-gonic/gin"
)
// helper function to clean query
func cleanQuery(query string) string {
q := strings.Replace(query, "\r", "", -1)
q = strings.Replace(query, "\n", "", -1)
return q
}
// check if query is valid JSON
func validJSON(query string) error {
if !strings.Contains(query, "{") {
return nil
}
var data map[string]any
err := json.Unmarshal([]byte(query), &data)
return err
}
// helper function to process service request
func processResults(c *gin.Context, rec services.ServiceRequest, user string, idx, limit int) {
tmpl := server.MakeTmpl(StaticFs, "Search")
tmpl["Base"] = srvConfig.Config.Frontend.WebServer.Base
log.Printf("service request record\n%s", rec.String())
query := cleanQuery(rec.ServiceQuery.Query)
tmpl["Query"] = query
err1 := validJSON(query)
data, err2 := json.Marshal(rec)
if err1 != nil || err2 != nil {
tmpl["FixQuery"] = query
msg := "Given query is not valid JSON, error: "
err := err1
if err1 != nil {
msg += err1.Error()
} else if err2 != nil {
msg += err2.Error()
err = err2
}
tmpl["Content"] = msg
page := server.TmplPage(StaticFs, "query_error.tmpl", tmpl)
msg = string(template.HTML(page))
handleError(c, http.StatusBadRequest, msg, err)
return
}
// search request to DataDiscovery service
rurl := fmt.Sprintf("%s/search", srvConfig.Config.Services.DiscoveryURL)
resp, err := _httpReadRequest.Post(rurl, "application/json", bytes.NewBuffer(data))
if err != nil {
msg := "unable to get meta-data from upstream server"
handleError(c, http.StatusInternalServerError, msg, err)
return
}
// parse data records from meta-data service
var response services.ServiceResponse
defer resp.Body.Close()
data, err = io.ReadAll(resp.Body)
if err != nil {
content := errorTmpl(c, "unable to read response body, error", err)
c.Data(http.StatusBadRequest, "text/html; charset=utf-8", []byte(header()+content+footer()))
return
}
err = json.Unmarshal(data, &response)
if err != nil {
content := errorTmpl(c, "unable to unmarshal response, error", err)
c.Data(http.StatusBadRequest, "text/html; charset=utf-8", []byte(header()+content+footer()))
return
}
if Verbose > 1 {
log.Printf("meta-data response\n%+v", response)
}
if response.Results.NRecords == 0 {
tmpl["Content"] = fmt.Sprintf("No records found for your query:\n<pre>%s</pre>", query)
page := server.TmplPage(StaticFs, "noresults.tmpl", tmpl)
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(header()+page+footerEmpty()))
return
}
records := response.Results.Records
nrecords := response.Results.NRecords
content := records2html(user, records)
tmpl["Records"] = template.HTML(content)
sortKey := "date"
if len(rec.ServiceQuery.SortKeys) > 0 {
sortKey = rec.ServiceQuery.SortKeys[0]
}
sortOrder := "descending"
if rec.ServiceQuery.SortOrder == 1 {
sortOrder = "ascending"
}
pages := pagination(c, query, nrecords, idx, limit, sortKey, sortOrder)
tmpl["Pagination"] = template.HTML(pages)
page := server.TmplPage(StaticFs, "records.tmpl", tmpl)
// we will not use footer() on handlers since user may expand records
// instead we'll use footerEmpty() function
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(header()+page+footerEmpty()))
// c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(header()+page+footer()))
}