-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarter.go
157 lines (128 loc) · 4.37 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
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
package main
import (
"log"
"net/http"
"encoding/json"
"fmt"
"os"
"github.com/gorilla/mux"
mgo "gopkg.in/mgo.v2"
)
var mongoClient *mgo.Session
func main() {
log.SetOutput(os.Stdout)
mongoClient = MongoGetSession(os.Getenv("MONGO_IP"), os.Getenv("MONGO_USERNAME"), os.Getenv("MONGO_PASSWORD"))
MongoCreateCollectionIndexes(mongoClient)
router := makeRouter()
fmt.Println("server now starts")
log.Fatal(http.ListenAndServe(":9681", router))
}
func makeRouter() *mux.Router {
router := mux.NewRouter()
// Insert
router.HandleFunc("/hitec/repository/app/store/app-page/google-play/", postAppPageGooglePlay).Methods("POST")
router.HandleFunc("/hitec/repository/app/store/app-review/google-play/", postAppReviewGooglePlay).Methods("POST")
router.HandleFunc("/hitec/repository/app/observe/app/google-play/package-name/{package_name}/interval/{interval}", postObserveAppGooglePlay).Methods("POST")
router.HandleFunc("/hitec/repository/app/non-existing/app-review/google-play/", postNonExistingAppReviewsGooglePlay).Methods("POST")
// Get
router.HandleFunc("/hitec/repository/app/observable/google-play", getObsevableGooglePlay).Methods("GET")
router.HandleFunc("/hitec/repository/app/google-play/package-name/{package_name}/class/{class}", getAppReviewsOfClass).Methods("GET")
return router
}
func postAppPageGooglePlay(w http.ResponseWriter, r *http.Request) {
// get data from the request
var appPage AppPageGooglePlay
err := json.NewDecoder(r.Body).Decode(&appPage)
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
// insert data into the db
m := mongoClient.Copy()
defer m.Close()
ok := MongoInsertAppPageGooglePlay(m, appPage)
// send response
if ok {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
func postAppReviewGooglePlay(w http.ResponseWriter, r *http.Request) {
// get data from the request
var appReviews []AppReviewGooglePlay
err := json.NewDecoder(r.Body).Decode(&appReviews)
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
// insert data into the db
m := mongoClient.Copy()
defer m.Close()
for _, review := range appReviews {
MongoInsertAppReviewGooglePlay(m, review)
}
// send response
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
}
func postObserveAppGooglePlay(w http.ResponseWriter, r *http.Request) {
// get data from the request
params := mux.Vars(r)
packageName := params["package_name"]
interval := params["interval"] // possible intervals: minutely, hourly, daily, monthly
var observalbe = ObservableGooglePlay{PackageName: packageName, Interval: interval}
// insert data into the db
m := mongoClient.Copy()
defer m.Close()
ok := MongoInsertObservableGooglePlay(m, observalbe)
// send response
if ok {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
func postNonExistingAppReviewsGooglePlay(w http.ResponseWriter, r *http.Request) {
// get data from the request
var appReviews []AppReviewGooglePlay
err := json.NewDecoder(r.Body).Decode(&appReviews)
if err != nil {
fmt.Printf("ERROR: %s for request body: %v\n", err, r.Body)
w.WriteHeader(http.StatusBadRequest)
return
}
// insert data into the db
m := mongoClient.Copy()
defer m.Close()
nonExistingAppReviews := MongoGetNonExistingAppReviewGooglePlay(m, appReviews)
// send response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(nonExistingAppReviews)
}
func getObsevableGooglePlay(w http.ResponseWriter, r *http.Request) {
// get data from the db
m := mongoClient.Copy()
defer m.Close()
observables := MongoGetAllObservableGooglePlay(m)
// send response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(observables)
}
func getAppReviewsOfClass(w http.ResponseWriter, r *http.Request) {
// get request param
params := mux.Vars(r)
packageName := params["package_name"]
reviewClass := params["class"]
// query db
m := mongoClient.Copy()
bugReports := MongoGetGooglePlayReviewOfClass(m, packageName, reviewClass)
// if no recent data exist, return false
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(bugReports)
}