/
server.go
149 lines (127 loc) · 2.99 KB
/
server.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
// Primary package for the gopkg api server.
package gopkg
import (
"appengine"
"appengine/datastore"
"appengine/search"
"encoding/json"
"github.com/gorilla/mux"
"io/ioutil"
"log"
"net/http"
"time"
)
type Package struct {
Url string
Date time.Time
}
type SearchResult struct {
Id string
Url string
}
type Duplicate struct {
Err string
}
func init() {
r := mux.NewRouter()
r.HandleFunc("/add", addHandler)
r.HandleFunc("/search/{query}", searchHandler)
r.HandleFunc("/show", showHandler)
http.Handle("/", r)
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
index, err := search.Open("Package")
if err != nil {
log.Print(err)
}
c := appengine.NewContext(r)
packages := []*SearchResult{}
for t := index.Search(c, vars["query"], nil); ; {
var doc Package
id, err2 := t.Next(&doc)
if err2 == search.Done {
break
}
if err2 != nil {
log.Print(w, "Search error: %v\n", err2)
break
}
log.Print(id)
packages = append(packages, &SearchResult{
Id: id,
Url: doc.Url,
})
}
jsonPackages, err3 := json.Marshal(packages)
if err3 != nil {
http.Error(w, err3.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonPackages)
}
func showHandler(w http.ResponseWriter, r *http.Request) {
}
// Callback handler for the /add api endpoint.
func addHandler(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Print(err)
}
packageUrl := string(body)
log.Print("adding: " + packageUrl)
p := &Package{
Url: packageUrl,
Date: time.Now(),
}
noneFound, err := searchForDuplicate(p)
if err != nil {
log.Print(err)
}
if noneFound {
addToDatabase(p, r)
addToIndex(p, w, r)
} else {
returnDuplicateWarning(p, w)
}
}
// Returns a warning to the user if the package already exists in the index.
func returnDuplicateWarning(p *Package, w http.ResponseWriter) {
w.WriteHeader(200)
d := Duplicate{
Err: "The package " + p.Url + " is already in the index.",
}
js, err := json.Marshal(d)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
// Searches the database for the package they are trying to add so that there
// are not any duplicate entries.
func searchForDuplicate(p *Package) (bool, error) {
return true, nil
}
// Adds a Package struct into the App Engine datastore.
func addToDatabase(p *Package, r *http.Request) {
c := appengine.NewContext(r)
key := datastore.NewIncompleteKey(c, "Package", nil)
datastore.Put(c, key, p)
}
func addToIndex(p *Package, w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
index, err1 := search.Open("Package")
if err1 != nil {
http.Error(w, err1.Error(), http.StatusInternalServerError)
return
}
id, err2 := index.Put(c, "", p)
if err2 != nil {
http.Error(w, err2.Error(), http.StatusInternalServerError)
return
}
log.Print("index id " + id)
}