-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
70 lines (67 loc) · 1.67 KB
/
index.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
package main
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
_ "github.com/go-sql-driver/mysql"
)
func indexPlaces(db *sql.DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
type Place struct {
Id int64
Name string
}
query := "select id,name from PLACE;"
rows, err := db.Query(query)
if err == nil {
for rows.Next() {
var place Place
err = rows.Scan(&place.Id, &place.Name)
if err == nil {
data, err := json.Marshal(place)
if err == nil {
url := "http://localhost:9200/findaway/places/" + strconv.Itoa(int(place.Id))
response, _ := http.Post(url, "application/json", bytes.NewBuffer(data))
fmt.Println(response.Body)
}
}
}
}
}
}
}
func searchPlaces(db *sql.DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
type Body struct {
Search string
}
body := Body{}
err := json.NewDecoder(r.Body).Decode(&body)
if err == nil {
fmt.Println(body)
defer r.Body.Close()
url := "http://localhost:9200/findaway/places/_search?q=Name:*" + body.Search + "*"
res, _ := http.Get(url)
defer res.Body.Close()
var response map[string]interface{}
data, err := ioutil.ReadAll(res.Body)
if err == nil {
jsonData := string(data)
json.Unmarshal([]byte(jsonData), &response)
response["status"] = "OK"
} else {
response["status"] = "Not OK"
}
json.NewEncoder(w).Encode(response)
} else {
fmt.Println("error : " + err.Error())
}
}
}
}