-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
458 lines (384 loc) · 12.2 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package main
import (
"cloud.google.com/go/storage"
"context"
"encoding/json"
"fmt"
"github.com/pborman/uuid"
elastic "gopkg.in/olivere/elastic.v3"
"log"
"net/http"
"reflect"
"strconv"
"io"
"github.com/auth0/go-jwt-middleware"
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
"cloud.google.com/go/bigtable"
"path/filepath"
)
var mySigningKey = []byte("secret")
type Location struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
type Post struct {
User string `json:"user"`
Message string `json:"message"`
Location Location `json:"location"`
Url string `json:"url"`
Type string `json:"type"`
Face float64 `json:"face"`
}
const (
DISTANCE = "200km"
INDEX = "around"
TYPE = "post"
// Needs to update
PROJECT_ID = "praxis-road-206502"
BT_INSTANCE = "around-post"
// Needs to update this URL if you deploy it to cloud.
ES_URL = "http://35.239.231.162:9200"
BUCKET_NAME = "post-images-146578"
API_PREFIX = "/api/v1"
)
//mapping
var (
mediaTypes = map[string]string{
".jpeg": "image",
".jpg": "image",
".gif": "image",
".png": "image",
".mov": "video",
".mp4": "video",
".avi": "video",
".flv": "video",
".wmv": "video",
}
)
func main() {
// Create a client
client, err := elastic.NewClient(elastic.SetURL(ES_URL), elastic.SetSniff(false))
if err != nil {
panic(err)
return
}
// Use the IndexExists service to check if a specified index exists.
exists, err := client.IndexExists(INDEX).Do()
if err != nil {
panic(err)
}
if !exists {
// Create a new index.
mapping := `{
"mappings":{
"post":{
"properties":{
"location":{
"type":"geo_point"
}
}
}
}
}`
_, err := client.CreateIndex(INDEX).Body(mapping).Do()
if err != nil {
// Handle error
panic(err)
}
}
fmt.Println("started-service")
// jwt authen
// 先用router 轉給jwt 比對對不對上號 再轉交給http handler
r := mux.NewRouter()
var jwtMiddleware = jwtmiddleware.New(jwtmiddleware.Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return mySigningKey, nil
},
SigningMethod: jwt.SigningMethodHS256,
})
r.Handle(API_PREFIX+"/post", jwtMiddleware.Handler(http.HandlerFunc(handlerPost)))
r.Handle(API_PREFIX+"/search", jwtMiddleware.Handler(http.HandlerFunc(handlerSearch)))
r.Handle(API_PREFIX+"/cluster", jwtMiddleware.Handler(http.HandlerFunc(handlerCluster)))
r.Handle(API_PREFIX+"/login", http.HandlerFunc(loginHandler))
r.Handle(API_PREFIX+"/signup", http.HandlerFunc(signupHandler))
// Backend endpoints.
http.Handle(API_PREFIX+"/", r)
// Frontend endpoints.
http.Handle("/", http.FileServer(http.Dir("build")))
log.Fatal(http.ListenAndServe(":8080", nil))
log.Fatal(http.ListenAndServe(":8080", nil))
}
//{
// "user_name": "john",
// "message": "test",
// "location": {
// "lat": 37,
// "lon": -120
// }
//}
func handlerPost(w http.ResponseWriter, r *http.Request) {
// Other codes
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Authorization")
user := r.Context().Value("user")
claims := user.(*jwt.Token).Claims
username := claims.(jwt.MapClaims)["username"]
// 32 << 20 is the maxMemory param for ParseMultipartForm, equals to 32MB (1MB = 1024 * 1024 bytes = 2^20 bytes)
// After you call ParseMultipartForm, the file will be saved in the server memory with maxMemory size.
// If the file size is larger than maxMemory, the rest of the data will be saved in a system temporary file.
r.ParseMultipartForm(32 << 20)
// Parse from form data.
fmt.Printf("Received one post request %s\n", r.FormValue("message"))
lat, _ := strconv.ParseFloat(r.FormValue("lat"), 64)
lon, _ := strconv.ParseFloat(r.FormValue("lon"), 64)
p := &Post{
User: username.(string),
Message: r.FormValue("message"),
Location: Location{
Lat: lat,
Lon: lon,
},
}
id := uuid.New()
file, _, err := r.FormFile("image")
if err != nil {
http.Error(w, "Image is not available", http.StatusInternalServerError)
fmt.Printf("Image is not available %v.\n", err)
return
}
defer file.Close()
ctx := context.Background()
// replace it with your real bucket name.
_, attrs, err := saveToGCS(ctx, file, BUCKET_NAME, id)
if err != nil {
http.Error(w, "GCS is not setup", http.StatusInternalServerError)
fmt.Printf("GCS is not setup %v\n", err)
return
}
im, header, _ := r.FormFile("image")
defer im.Close()
suffix := filepath.Ext(header.Filename)
// Client needs to know the media type so as to render it.
if t, ok := mediaTypes[suffix]; ok {
p.Type = t
} else {
p.Type = "unknown"
}
// ML Engine only supports jpeg.
if suffix == ".jpeg" {
if score, err := annotate(im); err != nil {
http.Error(w, "Failed to annotate the image", http.StatusInternalServerError)
fmt.Printf("Failed to annotate the image %v\n", err)
return
} else {
p.Face = score
}
}
// Update the media link after saving to GCS.
p.Url = attrs.MediaLink
// Save to ES.
saveToES(p, id)
// Save to BigTable.
saveToBigTable(p, id)
}
func handlerSearch(w http.ResponseWriter, r *http.Request) {
fmt.Println("Received one request for search")
lat, _ := strconv.ParseFloat(r.URL.Query().Get("lat"), 64)
lon, _ := strconv.ParseFloat(r.URL.Query().Get("lon"), 64)
// range is optional
ran := DISTANCE
if val := r.URL.Query().Get("range"); val != "" {
ran = val + "km"
}
fmt.Printf( "Search received: %f %f %s\n", lat, lon, ran)
// Create a client
client, err := elastic.NewClient(elastic.SetURL(ES_URL), elastic.SetSniff(false))
if err != nil {
panic(err)
return
}
// Define geo distance query as specified in
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-geo-distance-query.html
q := elastic.NewGeoDistanceQuery("location")
q = q.Distance(ran).Lat(lat).Lon(lon)
// Some delay may range from seconds to minutes. So if you don't get enough results. Try it later.
searchResult, err := client.Search().
Index(INDEX).
Query(q).
Pretty(true).
Do()
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
fmt.Printf("Found a total of %d post\n", searchResult.TotalHits())
var typ Post
var ps []Post
for _, item := range searchResult.Each(reflect.TypeOf(typ)) { // instance of . in Java
p := item.(Post) // p = (Post) item . in Java
fmt.Printf("Post by %s: %s at lat %v and lon %v\n",
p.User, p.Message, p.Location.Lat, p.Location.Lon)
// TODO(student homework): Perform filtering based on keywords such as web spam etc.
ps = append(ps, p)
}
js, err := json.Marshal(ps)
if err != nil {
panic(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(js)
}
func saveToES(p *Post, id string) {
es_client, err := elastic.NewClient(elastic.SetURL(ES_URL), elastic.SetSniff(false))
if err != nil {
panic(err)
return
}
// Save it to index
_, err = es_client.Index().
Index(INDEX).
Type(TYPE).
Id(id).
BodyJson(p).
Refresh(true).
Do()
if err != nil {
panic(err)
return
}
fmt.Printf("Post is saved to Index: %s\n", p.Message)
}
func saveToBigTable(p *Post, id string) {
ctx := context.Background() // store metadata or environment info
// you must update project name here
bt_client, err := bigtable.NewClient(ctx, PROJECT_ID, BT_INSTANCE)
if err != nil {
panic(err)
return
}
tbl := bt_client.Open("post")
mut := bigtable.NewMutation() // one row
t := bigtable.Now() // timestamp
mut.Set("post", "user", t, []byte(p.User)) // cast to byte[]
mut.Set("post", "message", t, []byte(p.Message))
mut.Set("location", "lat", t, []byte(strconv.FormatFloat(p.Location.Lat, 'f', -1, 64)))
mut.Set("location", "lon", t, []byte(strconv.FormatFloat(p.Location.Lon, 'f', -1, 64)))
err = tbl.Apply(ctx, id, mut)
if err != nil {
panic(err)
return
}
fmt.Printf("Post is saved to BigTable: %s\n", p.Message)
}
// Save an image to GCS.
func saveToGCS(ctx context.Context, r io.Reader, bucketName, name string) (*storage.ObjectHandle, *storage.ObjectAttrs, error) {
client, err := storage.NewClient(ctx)
if err != nil {
return nil, nil, err
}
defer client.Close()
bucket := client.Bucket(bucketName)
// Next check if the bucket exists
if _, err = bucket.Attrs(ctx); err != nil {
return nil, nil, err
}
obj := bucket.Object(name)
w := obj.NewWriter(ctx)
if _, err := io.Copy(w, r); err != nil {
return nil, nil, err
}
if err := w.Close(); err != nil {
return nil, nil, err
}
if err := obj.ACL().Set(ctx, storage.AllUsers, storage.RoleReader); err != nil {
return nil, nil, err
}
attrs, err := obj.Attrs(ctx)
fmt.Printf("Post is saved to GCS: %s\n", attrs.MediaLink)
return obj, attrs, err
}
func handlerCluster(w http.ResponseWriter, r *http.Request) {
fmt.Println("Received one request for clustering")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Authorization")
if r.Method != "GET" {
return
}
term := r.URL.Query().Get("term")
// Create a client
client, err := elastic.NewClient(elastic.SetURL(ES_URL), elastic.SetSniff(false))
if err != nil {
http.Error(w, "ES is not setup", http.StatusInternalServerError)
fmt.Printf("ES is not setup %v\n", err)
return
}
// Range query.
// For details, https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
q := elastic.NewRangeQuery(term).Gte(0.9)
searchResult, err := client.Search().
Index(INDEX).
Query(q).
Pretty(true).
Do()
if err != nil {
// Handle error
m := fmt.Sprintf("Failed to query ES %v", err)
fmt.Println(m)
http.Error(w, m, http.StatusInternalServerError)
}
// searchResult is of type SearchResult and returns hits, suggestions,
// and all kinds of other information from Elasticsearch.
fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
// TotalHits is another convenience function that works even when something goes wrong.
fmt.Printf("Found a total of %d post\n", searchResult.TotalHits())
// Each is a convenience function that iterates over hits in a search result.
// It makes sure you don't need to check for nil values in the response.
// However, it ignores errors in serialization.
var typ Post
var ps []Post
for _, item := range searchResult.Each(reflect.TypeOf(typ)) {
p := item.(Post)
ps = append(ps, p)
}
js, err := json.Marshal(ps)
if err != nil {
m := fmt.Sprintf("Failed to parse post object %v", err)
fmt.Println(m)
http.Error(w, m, http.StatusInternalServerError)
return
}
w.Write(js)
}
// func handlerSearch(w http.ResponseWriter, r *http.Request) {
// fmt.Println("Received one request for search")
// lat, _ := strconv.ParseFloat(r.URL.Query().Get("lat"), 64)
// lon, _ := strconv.ParseFloat(r.URL.Query().Get("lon"), 64)
// // range is optional
// ran := DISTANCE
// if val := r.URL.Query().Get("range"); val != "" {
// ran = val + "km"
// }
// fmt.Println("range is ", ran)
// // Return a fake post
// p := &Post{
// User:"1111",
// Message:"一生必去的100个地方",
// Location: Location{
// Lat:lat,
// Lon:lon,
// },
// }
// js, err := json.Marshal(p) // Marshal(go's structure) -> return json stucture - string representation
// if err != nil {
// panic(err)
// return
// }
// w.Header().Set("Content-Type", "application/json")
// w.Write(js)
// }