-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.go
More file actions
58 lines (50 loc) · 1.36 KB
/
http_server.go
File metadata and controls
58 lines (50 loc) · 1.36 KB
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"encoding/json"
)
func elRequest(url string,data string)(string) {
fmt.Println("The elastic search URL:>", url)
var query = []byte(data)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(query))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
return string(body)
}
func parseReqBodyToJson(r *http.Request) map[string]interface{} {
result, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
var f interface{}
err := json.Unmarshal(result, &f)
if err != nil {
panic(err)
}
return f.(map[string]interface{})
}
func elHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
payload := parseReqBodyToJson(r)
url := payload["url"].(string)
query := payload["query"].(string)
result := elRequest(url,query)
w.Header().Set("content-type", "application/json")
w.Write([]byte(result))
}
}
func main(){
http.Handle("/", http.FileServer(http.Dir("public")))
http.HandleFunc("/elsearch",elHandler)
http.ListenAndServe(":8888", nil)
}