Skip to content

Commit cf37baf

Browse files
committed
Examples: Extending the client APIs
1 parent 2c835c5 commit cf37baf

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

_examples/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ The [**`fasthttp`**](./fasthttp) directory contains a demonstration of replacing
2525

2626
The [**`instrumentation`**](./instrumentation) directory contains recipes for instrumenting the client with the OpenCensus and Elastic APM packages.
2727

28+
## Extension
29+
30+
The [**`extension`**](./extension) directory contains an example of extending the client APIs, for example
31+
to support custom endpoints installed by a plugin.
32+
2833
## Google Cloud Functions
2934

3035
The [**`cloudfunction`**](./cloudfunction) directory contains a simple web service for checking Elasticsearch cluster status.

_examples/extension/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
GO_TEST_CMD = $(if $(shell which richgo),richgo test,go test)
2+
3+
test: ## Run tests
4+
go run main.go
5+
6+
.PHONY: test

_examples/extension/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Example: Extending the Client
2+
3+
This example demonstrates how to extend the client, in order to call custom APIs, for example added by a [plugin](https://github.com/elastic/elasticsearch/blob/master/plugins/examples/rest-handler/src/main/java/org/elasticsearch/example/resthandler/ExampleCatAction.java).
4+
5+
The [`main.go`](main.go) example defines a custom type, which embeds the regular Elasticsearch client, and adds a `Custom` namespace with an `Example()` method.
6+
7+
To run the example:
8+
9+
```bash
10+
go run main.go
11+
12+
# GET http://localhost:9209/_cat/health 200 OK 25ms
13+
# « 1555252476 14:34:36 go-elasticsearch green 1 1 0 0 0 0 0 0 - 100.0%
14+
#
15+
# GET http://localhost:9209/_cat/example 200 OK 0s
16+
# « Hello from Cat Example action
17+
```

_examples/extension/main.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// +build ignore
2+
3+
// This examples demonstrates how extend the API of the client by embedding it inside a custom type.
4+
5+
package main
6+
7+
import (
8+
"io"
9+
"log"
10+
"net"
11+
"net/http"
12+
"net/http/httputil"
13+
"net/url"
14+
"os"
15+
16+
"github.com/elastic/go-elasticsearch/v8"
17+
"github.com/elastic/go-elasticsearch/v8/esapi"
18+
"github.com/elastic/go-elasticsearch/v8/estransport"
19+
)
20+
21+
const port = "9209"
22+
23+
// ExtendedClient allows to call regular and custom APIs.
24+
//
25+
type ExtendedClient struct {
26+
*elasticsearch.Client
27+
Custom *ExtendedAPI
28+
}
29+
30+
// ExtendedAPI contains custom APIs.
31+
//
32+
type ExtendedAPI struct {
33+
*elasticsearch.Client
34+
}
35+
36+
// Example calls a custom REST API, "/_cat/example".
37+
//
38+
func (c *ExtendedAPI) Example() (*esapi.Response, error) {
39+
req, _ := http.NewRequest("GET", "/_cat/example", nil) // errcheck exclude
40+
41+
res, err := c.Perform(req)
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
return &esapi.Response{StatusCode: res.StatusCode, Body: res.Body, Header: res.Header}, nil
47+
}
48+
49+
func main() {
50+
log.SetFlags(0)
51+
52+
started := make(chan bool)
53+
54+
// --> Start the proxy server
55+
//
56+
go startServer(started)
57+
58+
esclient, err := elasticsearch.NewClient(elasticsearch.Config{
59+
Addresses: []string{"http://localhost:" + port},
60+
Logger: &estransport.ColorLogger{Output: os.Stdout, EnableRequestBody: true, EnableResponseBody: true},
61+
})
62+
if err != nil {
63+
log.Fatalf("Error creating the client: %s", err)
64+
}
65+
66+
es := ExtendedClient{Client: esclient, Custom: &ExtendedAPI{esclient}}
67+
<-started
68+
69+
// --> Call a regular Elasticsearch API
70+
//
71+
es.Cat.Health()
72+
73+
// --> Call a custom API
74+
//
75+
es.Custom.Example()
76+
}
77+
78+
func startServer(started chan<- bool) {
79+
proxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: "localhost:9200"})
80+
81+
// Respond with custom content on "GET /_cat/example", proxy to Elasticsearch for other requests
82+
//
83+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
84+
if r.Method == "GET" && r.URL.Path == "/_cat/example" {
85+
io.WriteString(w, "Hello from Cat Example action")
86+
return
87+
}
88+
proxy.ServeHTTP(w, r)
89+
})
90+
91+
ln, err := net.Listen("tcp", "localhost:"+port)
92+
if err != nil {
93+
log.Fatalf("Unable to start server: %s", err)
94+
}
95+
96+
go http.Serve(ln, nil)
97+
started <- true
98+
}

0 commit comments

Comments
 (0)