|
| 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