@@ -13,7 +13,7 @@ import (
1313 "go.uber.org/zap"
1414)
1515
16- func apiTorrentsHandler (w http.ResponseWriter , r * http.Request ) {
16+ func apiTorrents (w http.ResponseWriter , r * http.Request ) {
1717 // @lastOrderedValue AND @lastID are either both supplied or neither of them should be supplied
1818 // at all; and if that is NOT the case, then return an error.
1919 if q := r .URL .Query (); ! ((q .Get ("lastOrderedValue" ) != "" && q .Get ("lastID" ) != "" ) ||
@@ -29,6 +29,7 @@ func apiTorrentsHandler(w http.ResponseWriter, r *http.Request) {
2929 Ascending * bool `schema:"ascending"`
3030 LastOrderedValue * float64 `schema:"lastOrderedValue"`
3131 LastID * uint64 `schema:"lastID"`
32+ Limit * uint `schema:"limit"`
3233 }
3334 if err := decoder .Decode (& tq , r .URL .Query ()); err != nil {
3435 respondError (w , 400 , "error while parsing the URL: %s" , err .Error ())
@@ -69,27 +70,26 @@ func apiTorrentsHandler(w http.ResponseWriter, r *http.Request) {
6970 }
7071 }
7172
73+ if tq .Limit == nil {
74+ tq .Limit = new (uint )
75+ * tq .Limit = 20
76+ }
77+
7278 torrents , err := database .QueryTorrents (
7379 * tq .Query , * tq .Epoch , orderBy ,
74- * tq .Ascending , N_TORRENTS , tq .LastOrderedValue , tq .LastID )
80+ * tq .Ascending , * tq . Limit , tq .LastOrderedValue , tq .LastID )
7581 if err != nil {
7682 respondError (w , 400 , "query error: %s" , err .Error ())
7783 return
7884 }
7985
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 ))
8989 }
9090}
9191
92- func apiTorrentsInfohashHandler (w http.ResponseWriter , r * http.Request ) {
92+ func apiTorrent (w http.ResponseWriter , r * http.Request ) {
9393 infohashHex := mux .Vars (r )["infohash" ]
9494
9595 infohash , err := hex .DecodeString (infohashHex )
@@ -107,19 +107,13 @@ func apiTorrentsInfohashHandler(w http.ResponseWriter, r *http.Request) {
107107 return
108108 }
109109
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 ))
119113 }
120114}
121115
122- func apiFilesInfohashHandler (w http.ResponseWriter , r * http.Request ) {
116+ func apiFilelist (w http.ResponseWriter , r * http.Request ) {
123117 infohashHex := mux .Vars (r )["infohash" ]
124118
125119 infohash , err := hex .DecodeString (infohashHex )
@@ -137,19 +131,13 @@ func apiFilesInfohashHandler(w http.ResponseWriter, r *http.Request) {
137131 return
138132 }
139133
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 ))
149137 }
150138}
151139
152- func apiStatisticsHandler (w http.ResponseWriter , r * http.Request ) {
140+ func apiStatistics (w http.ResponseWriter , r * http.Request ) {
153141 from := r .URL .Query ().Get ("from" )
154142
155143 // TODO: use gorilla?
@@ -175,15 +163,9 @@ func apiStatisticsHandler(w http.ResponseWriter, r *http.Request) {
175163 return
176164 }
177165
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 ))
187169 }
188170}
189171
0 commit comments