-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
97 lines (72 loc) · 1.84 KB
/
search.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"io/ioutil"
"encoding/json"
"github.com/gorilla/mux"
)
// Index Path //
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Elastic search API using GO!")
}
// Search //
func Search(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
query := url.QueryEscape(name)
// URL //
url := fmt.Sprintf("http://192.168.8.20:9200/product/list/_search?q=%s", query)
// Build request //
req, err := http.NewRequest("GET", url, nil)
if err != nil{
log.Fatal("New Request : ", err)
return
}
/*
// Ambil response code http header jika, rest service tidak aktif / error //
// http-200 --> OK //
// else failed //
response_code, err := json.Marshal(r.Header.Get("Status Code"))
if err != nil{
log.Fatal("Get HTTP Response Code : ", err)
return
}
fmt.Println(string(byte))
*/
client := &http.Client{}
resp, err := client.Do(req)
if err != nil{
log.Fatal("Do", err)
return
}
defer resp.Body.Close()
// Get All HTTP BODY //
jsonData, err := ioutil.ReadAll(resp.Body)
if err != nil{
log.Println(err)
}
// Convert into String //
json_string := string(jsonData)
// Unmarshall JSON into Go Struct //
var post Post
json.Unmarshal([]byte(json_string), &post)
fmt.Println(post)
// Marshall Go struct into JSON //
b, err := json.Marshal(post)
if err != nil {
fmt.Println(err)
return
}
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, string(b))
/*
if err := json.NewEncoder(w).Encode(b); err != nil{
panic(err)
}
*/
//fmt.Println(string(b))
}