-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstarter.go
52 lines (43 loc) · 1.2 KB
/
starter.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
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"log"
"net/http"
)
const (
requestError = "The request could not be recovered"
)
func main() {
log.Fatal(http.ListenAndServe(":9622", makeRouter()))
}
func makeRouter() *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/hitec/crawl/app-page/google-play/{package_name}", getAppPage).Methods("GET")
return router
}
func recoverAPICall(w http.ResponseWriter, page AppPage) {
if r := recover(); r != nil {
w.WriteHeader(http.StatusInternalServerError)
page.Errors = append(page.Errors, requestError)
}
}
func getAppPage(w http.ResponseWriter, r *http.Request) {
appPage := AppPage{}
w.Header().Set("Content-Type", "application/json")
defer recoverAPICall(w, appPage)
// get request param
params := mux.Vars(r)
packageName := params["package_name"]
// crawl app reviews
appPage = Crawl(packageName)
serveResponse(w, appPage, http.StatusOK)
}
// serves the generated content
func serveResponse(writer http.ResponseWriter, page AppPage, status int) {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(status)
encoder := json.NewEncoder(writer)
encoder.SetEscapeHTML(false)
encoder.Encode(page)
}