Skip to content

Instantly share code, notes, and snippets.

@asjadathick
Last active August 19, 2021 23:31
Show Gist options
  • Save asjadathick/a9b30d067d238e7b6e6eeadac6bf5bc7 to your computer and use it in GitHub Desktop.
Save asjadathick/a9b30d067d238e7b6e6eeadac6bf5bc7 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
"regexp"
)
var rEverything = regexp.MustCompile(`.*`) // Route these to the backend ES cluster
var rDoc = regexp.MustCompile(`/_doc`) //any singular indexing operation
func proxy(w http.ResponseWriter, r *http.Request) {
//mock bulk API response
resp := make(map[string]string)
resp["message"] = "Proxied"
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Println(err)
}
resp["request"] = string(requestDump)
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
}
func home(w http.ResponseWriter, r *http.Request) {
//return string as json
fmt.Fprintf(w, "{ \"name\" : \"instance-0000000045\", \"cluster_name\" : \"ddc7994029894f8d8ed1847318505939\", \"cluster_uuid\" : \"VJM2yDPqTLaaXEK0wAO-sA\", \"version\" : { \"number\" : \"7.13.2\", \"build_flavor\" : \"default\", \"build_type\" : \"docker\", \"build_hash\" : \"4d960a0733be83dd2543ca018aa4ddc42e956800\", \"build_date\" : \"2021-06-10T21:01:55.251515791Z\", \"build_snapshot\" : false, \"lucene_version\" : \"8.8.2\", \"minimum_wire_compatibility_version\" : \"6.8.0\", \"minimum_index_compatibility_version\" : \"6.0.0-beta1\" }, \"tagline\" : \"You Know, for Search\"}")
}
func bulk(w http.ResponseWriter, r *http.Request) {
//mock bulk API response
resp := make(map[string]string)
resp["message"] = "Bulked"
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Println(err)
}
resp["request"] = string(requestDump)
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
}
func index(w http.ResponseWriter, r *http.Request) {
//mock bulk API response
h := json.RawMessage(`{
"_index" : "mocked",
"_type" : "_doc",
"_id" : "mocked",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}`)
jsonResp, err := json.Marshal(h)
if err != nil {
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
}
w.Write(jsonResp)
}
func route(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
//authenticate supplied credentials
switch {
case r.URL.Path == "/":
home(w, r)
case r.URL.Path == "/_bulk":
bulk(w, r)
case rDoc.MatchString(r.URL.Path):
index(w, r)
case rEverything.MatchString(r.URL.Path):
proxy(w, r)
default:
fmt.Println("Unknown pattern")
}
}
func main() {
http.HandleFunc("/", route)
http.ListenAndServe(":9243", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment