-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
89 lines (69 loc) · 1.69 KB
/
serve.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
package main
import (
"encoding/json"
"fmt"
"github.com/labstack/echo"
// "github.com/labstack/echo/middleware"
"net/http"
"net/url"
"os"
)
type SearchForm struct { // general search request from spa
Term string `json:"term"`
Location string `json:"location"`
Limit string `json:"limit"`
}
type SummaryForm struct { // place summary request from spa
Name string `json:"name"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
func Serve() {
e := echo.New()
e.File("/", "index.html")
e.Static("/dist", "dist")
e.Static("/static", "static")
e.POST("/places", postPlaces)
e.POST("/summary", postSummary)
// e.Use(middleware.CORS()) // TODO: remove for production
e.Logger.Fatal(e.Start(":" + os.Getenv("PORT")))
}
func postPlaces(c echo.Context) (err error) { // respond to place search post
var u = new(SearchForm)
if err = c.Bind(u); err != nil {
return err
}
form := url.Values{}
form.Add("location", u.Location)
form.Add("term", u.Term)
form.Add("limit", u.Limit)
res, err := YelpSearch(form.Encode(), yelpToken)
if err != nil {
fmt.Println(err)
}
out, err := json.Marshal(res)
if err != nil {
fmt.Println(err)
}
return c.String(http.StatusOK, string(out))
}
func postSummary(c echo.Context) (err error) { // respond to place summary post
var u = new(SummaryForm)
if err = c.Bind(u); err != nil {
return err
}
text, gReviews, err := SearchGoogleReviews(u, apiKeys)
if err != nil {
fmt.Println(err)
}
res, err := Summarize(text, apiKeys)
if err != nil {
fmt.Println(err)
}
res.Reviews = gReviews
out, err := json.Marshal(res)
if err != nil {
fmt.Println(err)
}
return c.String(http.StatusOK, string(out))
}