Skip to content

Commit

Permalink
Meilisearch endpoint version compatible (#185)
Browse files Browse the repository at this point in the history
* Meilisearch endpoint version compatible

The method of updating the searchable attributes settings changed to `PUT`
More details https://github.com/meilisearch/meilisearch/releases/tag/v0.28.0
  • Loading branch information
waybackarchiver authored Jul 17, 2022
1 parent 0067fb4 commit 21ccecd
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/google/go-github/v40 v40.0.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/go-version v1.6.0
github.com/iawia002/lux v0.14.0
github.com/mattn/go-mastodon v0.0.5-0.20210515144304-86627ec7d635
github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down
60 changes: 59 additions & 1 deletion service/indexing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"

"github.com/hashicorp/go-version"
"github.com/rs/xid"
"github.com/wabarc/wayback"
"github.com/wabarc/wayback/config"
Expand Down Expand Up @@ -46,6 +48,9 @@ type Meili struct {
// Meilisearch admin API key, which can be emptied.
apikey string

// Version of the Meilisearch server.
version string

client *http.Client
}

Expand Down Expand Up @@ -76,9 +81,42 @@ func (m *Meili) Setup() error {
if err != nil {
return err
}
err = m.getVersion()
if err != nil {
return err
}
return m.sortable()
}

// getVersion specifies its version of the meilisearch server.
func (m *Meili) getVersion() error {
endpoint := fmt.Sprintf(`%s/version`, m.endpoint)
resp, err := m.do(http.MethodGet, endpoint, nil)
if err != nil {
return errors.Wrap(err, `get version: request failed`)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return errors.New(`get version: request failed: ` + resp.Status)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, `get version: reads body failed`)
}

var server struct {
Version string `json:"pkgVersion"`
}
if err := json.Unmarshal(body, &server); err != nil {
return errors.Wrap(err, `get version: unmarshal json failed`)
}
m.version = server.Version

return nil
}

type indexes struct {
UID string `json:"uid"`
Name string `json:"name"`
Expand Down Expand Up @@ -162,7 +200,22 @@ func (m *Meili) createIndex() error {
func (m *Meili) sortable() error {
endpoint := fmt.Sprintf(`%s/indexes/%s/settings/sortable-attributes`, m.endpoint, m.indexing)
payload := fmt.Sprintf(`["id"]`)
resp, err := m.do(http.MethodPost, endpoint, strings.NewReader(payload))
method := http.MethodPost
ver, err := version.NewVersion(m.version)
if err != nil {
return errors.Wrap(err, `set sortable attributes: invalid version: `+m.version)
}
// The method of updating the searchable attributes settings changed to `PUT`
// See https://github.com/meilisearch/meilisearch/releases/tag/v0.28.0
constraints, err := version.NewConstraint(`>= 0.28`)
if err != nil {
return errors.Wrap(err, `set sortable attributes: new constraint failed`)
}
if constraints.Check(ver) {
method = http.MethodPut
}

resp, err := m.do(method, endpoint, strings.NewReader(payload))
if err != nil {
return errors.Wrap(err, `set sortable attributes: request failed`)
}
Expand Down Expand Up @@ -254,6 +307,11 @@ func (m *Meili) documents(cols []wayback.Collect) (docs []document) {
Source: src,
}
for _, col := range maps {
_, err := url.Parse(col.Dst)
// If the URI is invalid, the results will be an empty string.
if err != nil {
col.Dst = ""
}
switch col.Arc {
case config.SLOT_IA:
doc.IA = col.Dst
Expand Down
38 changes: 37 additions & 1 deletion service/indexing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import (
var (
apiKey = `foo`

respGetVersion = `{
"commitSha": "b46889b5f0f2f8b91438a08a358ba8f05fc09fc1",
"commitDate": "2019-11-15T09:51:54.278247+00:00",
"pkgVersion": "0.1.1"
}
`
respGetIndex = fmt.Sprintf(`{
"uid": "%s",
"name": "%s",
Expand Down Expand Up @@ -77,12 +83,22 @@ var (
Ext: config.SLOT_PH,
},
}
invalidSample = []wayback.Collect{
{
Arc: config.SLOT_IA,
Dst: "invalid URL",
Src: "https://example.com/",
Ext: config.SLOT_IA,
},
}

handlerFunc = func(w http.ResponseWriter, r *http.Request) {
switch {
case !strings.Contains(r.Header.Get(`Authorization`), apiKey):
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(respInvalidRequest))
case r.URL.Path == `/version`:
_, _ = w.Write([]byte(respGetVersion))
case r.Method == http.MethodGet && r.URL.Path == `/indexes/`+indexing: // get index
_, _ = w.Write([]byte(respGetIndex))
case r.Method == http.MethodPost && r.URL.Path == `/indexes`: // create index
Expand Down Expand Up @@ -211,14 +227,34 @@ func TestPushDocument(t *testing.T) {
collect: sample,
returns: ``,
},
{
collect: invalidSample,
returns: ``,
},
}

for _, test := range tests {
t.Run("", func(t *testing.T) {
err := m.push(sample)
err := m.push(test.collect)
if err != nil && err.Error() != test.returns {
t.Fatalf(`unexpected push document: %v`, err)
}
})
}
}

func TestVersion(t *testing.T) {
_, mux, server := helper.MockServer()
defer server.Close()

mux.HandleFunc("/", handlerFunc)

m := NewMeili(server.URL, apiKey, indexing)
err := m.getVersion()
if err != nil {
t.Fatalf(`unexpected get version: %v`, err)
}
if m.version == "" {
t.Fatal(`unexpected version`)
}
}

0 comments on commit 21ccecd

Please sign in to comment.