@@ -13,7 +13,7 @@ import (
13
13
"go.uber.org/zap"
14
14
)
15
15
16
- func apiTorrentsHandler (w http.ResponseWriter , r * http.Request ) {
16
+ func apiTorrents (w http.ResponseWriter , r * http.Request ) {
17
17
// @lastOrderedValue AND @lastID are either both supplied or neither of them should be supplied
18
18
// at all; and if that is NOT the case, then return an error.
19
19
if q := r .URL .Query (); ! ((q .Get ("lastOrderedValue" ) != "" && q .Get ("lastID" ) != "" ) ||
@@ -29,6 +29,7 @@ func apiTorrentsHandler(w http.ResponseWriter, r *http.Request) {
29
29
Ascending * bool `schema:"ascending"`
30
30
LastOrderedValue * float64 `schema:"lastOrderedValue"`
31
31
LastID * uint64 `schema:"lastID"`
32
+ Limit * uint `schema:"limit"`
32
33
}
33
34
if err := decoder .Decode (& tq , r .URL .Query ()); err != nil {
34
35
respondError (w , 400 , "error while parsing the URL: %s" , err .Error ())
@@ -69,27 +70,26 @@ func apiTorrentsHandler(w http.ResponseWriter, r *http.Request) {
69
70
}
70
71
}
71
72
73
+ if tq .Limit == nil {
74
+ tq .Limit = new (uint )
75
+ * tq .Limit = 20
76
+ }
77
+
72
78
torrents , err := database .QueryTorrents (
73
79
* tq .Query , * tq .Epoch , orderBy ,
74
- * tq .Ascending , N_TORRENTS , tq .LastOrderedValue , tq .LastID )
80
+ * tq .Ascending , * tq . Limit , tq .LastOrderedValue , tq .LastID )
75
81
if err != nil {
76
82
respondError (w , 400 , "query error: %s" , err .Error ())
77
83
return
78
84
}
79
85
80
- // TODO: use plain Marshal
81
- jm , err := json .MarshalIndent (torrents , "" , " " )
82
- if err != nil {
83
- respondError (w , 500 , "json marshalling error: %s" , err .Error ())
84
- return
85
- }
86
-
87
- if _ , err = w .Write (jm ); err != nil {
88
- zap .L ().Warn ("couldn't write http.ResponseWriter" , zap .Error (err ))
86
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
87
+ if err = json .NewEncoder (w ).Encode (torrents ); err != nil {
88
+ zap .L ().Warn ("JSON encode error" , zap .Error (err ))
89
89
}
90
90
}
91
91
92
- func apiTorrentsInfohashHandler (w http.ResponseWriter , r * http.Request ) {
92
+ func apiTorrent (w http.ResponseWriter , r * http.Request ) {
93
93
infohashHex := mux .Vars (r )["infohash" ]
94
94
95
95
infohash , err := hex .DecodeString (infohashHex )
@@ -107,19 +107,13 @@ func apiTorrentsInfohashHandler(w http.ResponseWriter, r *http.Request) {
107
107
return
108
108
}
109
109
110
- // TODO: use plain Marshal
111
- jm , err := json .MarshalIndent (torrent , "" , " " )
112
- if err != nil {
113
- respondError (w , 500 , "json marshalling error: %s" , err .Error ())
114
- return
115
- }
116
-
117
- if _ , err = w .Write (jm ); err != nil {
118
- zap .L ().Warn ("couldn't write http.ResponseWriter" , zap .Error (err ))
110
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
111
+ if err = json .NewEncoder (w ).Encode (torrent ); err != nil {
112
+ zap .L ().Warn ("JSON encode error" , zap .Error (err ))
119
113
}
120
114
}
121
115
122
- func apiFilesInfohashHandler (w http.ResponseWriter , r * http.Request ) {
116
+ func apiFilelist (w http.ResponseWriter , r * http.Request ) {
123
117
infohashHex := mux .Vars (r )["infohash" ]
124
118
125
119
infohash , err := hex .DecodeString (infohashHex )
@@ -137,19 +131,13 @@ func apiFilesInfohashHandler(w http.ResponseWriter, r *http.Request) {
137
131
return
138
132
}
139
133
140
- // TODO: use plain Marshal
141
- jm , err := json .MarshalIndent (files , "" , " " )
142
- if err != nil {
143
- respondError (w , 500 , "json marshalling error: %s" , err .Error ())
144
- return
145
- }
146
-
147
- if _ , err = w .Write (jm ); err != nil {
148
- zap .L ().Warn ("couldn't write http.ResponseWriter" , zap .Error (err ))
134
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
135
+ if err = json .NewEncoder (w ).Encode (files ); err != nil {
136
+ zap .L ().Warn ("JSON encode error" , zap .Error (err ))
149
137
}
150
138
}
151
139
152
- func apiStatisticsHandler (w http.ResponseWriter , r * http.Request ) {
140
+ func apiStatistics (w http.ResponseWriter , r * http.Request ) {
153
141
from := r .URL .Query ().Get ("from" )
154
142
155
143
// TODO: use gorilla?
@@ -175,15 +163,9 @@ func apiStatisticsHandler(w http.ResponseWriter, r *http.Request) {
175
163
return
176
164
}
177
165
178
- // TODO: use plain Marshal
179
- jm , err := json .MarshalIndent (stats , "" , " " )
180
- if err != nil {
181
- respondError (w , 500 , "json marshalling error: %s" , err .Error ())
182
- return
183
- }
184
-
185
- if _ , err = w .Write (jm ); err != nil {
186
- zap .L ().Warn ("couldn't write http.ResponseWriter" , zap .Error (err ))
166
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
167
+ if err = json .NewEncoder (w ).Encode (stats ); err != nil {
168
+ zap .L ().Warn ("JSON encode error" , zap .Error (err ))
187
169
}
188
170
}
189
171
0 commit comments