-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
99 lines (82 loc) · 2.18 KB
/
main.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
98
99
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/joho/godotenv"
)
func init() {
err := godotenv.Load()
if err != nil {
fmt.Println(err)
log.Fatal("Error loading env file")
}
}
func main() {
http.Handle("/", http.FileServer(http.Dir(os.Getenv("STATIC_DIR"))))
http.HandleFunc("/search", HandleFindPlaces)
http.HandleFunc("/ping", HandlePing)
http.HandleFunc("/key", HandleGetKey)
http.ListenAndServe(":8080", nil)
}
func HandlePing(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "success",
"statusCode": 200,
"data": "pong",
})
}
func HandleFindPlaces(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
params := r.URL.Query()
location := params.Get("latlng")
keyword := strings.Replace(params.Get("term"), " ", "+", -1)
key := os.Getenv("GOOGLE_MAPS_API_KEY")
fields := "photos,formatted_address,name"
base := "https://maps.googleapis.com/maps/api/place/nearbysearch/json?fields"
url := fmt.Sprintf("%s=%s&location=%s&radius=150000&keyword=%s&key=%s", base, fields, location, keyword, key)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
return
}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(body)
}
func HandleGetKey(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
key := os.Getenv("GOOGLE_MAPS_API_KEY")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "success",
"statusCode": 200,
"key": key,
})
}