@@ -15,7 +15,6 @@ import (
1515 "time"
1616
1717 "github.com/go-chi/chi/v5"
18- "github.com/go-chi/render"
1918 "github.com/go-pkgz/auth/v2"
2019 "github.com/go-pkgz/auth/v2/token"
2120 cache "github.com/go-pkgz/lcw/v2"
@@ -77,7 +76,7 @@ func (s *private) previewCommentCtrl(w http.ResponseWriter, r *http.Request) {
7776 user := rest .MustGetUserInfo (r )
7877
7978 comment := store.Comment {}
80- if err := render . DecodeJSON (http .MaxBytesReader (w , r .Body , hardBodyLimit ), & comment ); err != nil {
79+ if err := json . NewDecoder (http .MaxBytesReader (w , r .Body , hardBodyLimit )). Decode ( & comment ); err != nil {
8180 rest .SendErrorJSON (w , r , http .StatusBadRequest , err , "can't bind comment" , rest .ErrDecode )
8281 return
8382 }
@@ -100,14 +99,15 @@ func (s *private) previewCommentCtrl(w http.ResponseWriter, r *http.Request) {
10099 return
101100 }
102101 }
103-
104- render .HTML (w , r , comment .Text )
102+ w .Header ().Set ("Content-Type" , "text/html; charset=utf-8" )
103+ w .WriteHeader (http .StatusOK )
104+ _ , _ = w .Write ([]byte (comment .Text ))
105105}
106106
107107// POST /comment - adds comment, resets all immutable fields
108108func (s * private ) createCommentCtrl (w http.ResponseWriter , r * http.Request ) {
109109 comment := store.Comment {}
110- if err := render . DecodeJSON (http .MaxBytesReader (w , r .Body , hardBodyLimit ), & comment ); err != nil {
110+ if err := json . NewDecoder (http .MaxBytesReader (w , r .Body , hardBodyLimit )). Decode ( & comment ); err != nil {
111111 rest .SendErrorJSON (w , r , http .StatusBadRequest , err , "can't bind comment" , rest .ErrDecode )
112112 return
113113 }
@@ -176,8 +176,7 @@ func (s *private) createCommentCtrl(w http.ResponseWriter, r *http.Request) {
176176
177177 log .Printf ("[DEBUG] created comment %+v" , finalComment )
178178
179- render .Status (r , http .StatusCreated )
180- render .JSON (w , r , & finalComment )
179+ rest .WriteJSON (w , http .StatusCreated , & finalComment )
181180}
182181
183182// PUT /comment/{id}?site=siteID&url=post-url - update comment
@@ -188,7 +187,7 @@ func (s *private) updateCommentCtrl(w http.ResponseWriter, r *http.Request) {
188187 Delete bool
189188 }{}
190189
191- if err := render . DecodeJSON (http .MaxBytesReader (w , r .Body , hardBodyLimit ), & edit ); err != nil {
190+ if err := json . NewDecoder (http .MaxBytesReader (w , r .Body , hardBodyLimit )). Decode ( & edit ); err != nil {
192191 rest .SendErrorJSON (w , r , http .StatusBadRequest , err , "can't read comment details from body" , rest .ErrDecode )
193192 return
194193 }
@@ -233,7 +232,7 @@ func (s *private) updateCommentCtrl(w http.ResponseWriter, r *http.Request) {
233232 }
234233
235234 s .cache .Flush (cache .Flusher (locator .SiteID ).Scopes (locator .SiteID , locator .URL , lastCommentsScope , user .ID ))
236- render . JSON (w , r , res )
235+ rest . WriteJSON (w , http . StatusOK , res )
237236}
238237
239238// GET /user?site=siteID - returns user info
@@ -251,7 +250,7 @@ func (s *private) userInfoCtrl(w http.ResponseWriter, r *http.Request) {
251250 }
252251 }
253252
254- render . JSON (w , r , user )
253+ rest . WriteJSON (w , http . StatusOK , user )
255254}
256255
257256// PUT /vote/{id}?site=siteID&url=post-url&vote=1 - vote for/against comment
@@ -292,7 +291,7 @@ func (s *private) voteCtrl(w http.ResponseWriter, r *http.Request) {
292291 return
293292 }
294293 s .cache .Flush (cache .Flusher (locator .SiteID ).Scopes (locator .URL , comment .User .ID ))
295- render . JSON (w , r , R.JSON {"id" : comment .ID , "score" : comment .Score })
294+ rest . WriteJSON (w , http . StatusOK , R.JSON {"id" : comment .ID , "score" : comment .Score })
296295}
297296
298297// getEmailCtrl gets email address for authenticated user.
@@ -305,7 +304,7 @@ func (s *private) getEmailCtrl(w http.ResponseWriter, r *http.Request) {
305304 log .Printf ("[WARN] can't read email for %s, %v" , user .ID , err )
306305 }
307306
308- render . JSON (w , r , R.JSON {"user" : user , "address" : address })
307+ rest . WriteJSON (w , http . StatusOK , R.JSON {"user" : user , "address" : address })
309308}
310309
311310// sendEmailConfirmationCtrl gets address and siteID from query, makes confirmation token and sends it to user.
@@ -322,7 +321,7 @@ func (s *private) sendEmailConfirmationCtrl(w http.ResponseWriter, r *http.Reque
322321 Address string
323322 autoConfirm bool
324323 }{autoConfirm : true }
325- if err := render . DecodeJSON (http .MaxBytesReader (w , r .Body , hardBodyLimit ), & subscribe ); err != nil {
324+ if err := json . NewDecoder (http .MaxBytesReader (w , r .Body , hardBodyLimit )). Decode ( & subscribe ); err != nil {
326325 if err != io .EOF {
327326 rest .SendErrorJSON (w , r , http .StatusBadRequest , err , "can't parse request body" , rest .ErrDecode )
328327 return
@@ -385,7 +384,7 @@ func (s *private) sendEmailConfirmationCtrl(w http.ResponseWriter, r *http.Reque
385384 },
386385 )
387386
388- render . JSON (w , r , R.JSON {"user" : user , "address" : subscribe .Address , "updated" : false })
387+ rest . WriteJSON (w , http . StatusOK , R.JSON {"user" : user , "address" : subscribe .Address , "updated" : false })
389388}
390389
391390// telegramSubscribeCtrl generates and verifies telegram notification request
@@ -423,7 +422,7 @@ func (s *private) telegramSubscribeCtrl(w http.ResponseWriter, r *http.Request)
423422
424423 s .telegramService .AddToken (tkn , user .ID , siteID , expires )
425424
426- render . JSON (w , r , R.JSON {"token" : tkn , "bot" : s .telegramService .GetBotUsername ()})
425+ rest . WriteJSON (w , http . StatusOK , R.JSON {"token" : tkn , "bot" : s .telegramService .GetBotUsername ()})
427426
428427 return
429428 }
@@ -445,7 +444,7 @@ func (s *private) telegramSubscribeCtrl(w http.ResponseWriter, r *http.Request)
445444 return
446445 }
447446
448- render . JSON (w , r , R.JSON {"updated" : true , "address" : val })
447+ rest . WriteJSON (w , http . StatusOK , R.JSON {"updated" : true , "address" : val })
449448}
450449
451450// setConfirmedEmailCtrl uses provided token parameter (generated by sendEmailConfirmationCtrl) to set email and add it to user token
@@ -457,7 +456,7 @@ func (s *private) setConfirmedEmailCtrl(w http.ResponseWriter, r *http.Request)
457456 Site string
458457 Token string
459458 }{}
460- if err := render . DecodeJSON (http .MaxBytesReader (w , r .Body , hardBodyLimit ), & confirm ); err != nil {
459+ if err := json . NewDecoder (http .MaxBytesReader (w , r .Body , hardBodyLimit )). Decode ( & confirm ); err != nil {
461460 if err != io .EOF {
462461 rest .SendErrorJSON (w , r , http .StatusBadRequest , err , "can't parse request body" , rest .ErrDecode )
463462 return
@@ -513,7 +512,7 @@ func (s *private) setEmail(w http.ResponseWriter, r *http.Request, userID, siteI
513512 rest .SendErrorJSON (w , r , http .StatusInternalServerError , err , "failed to set token" , rest .ErrInternal )
514513 return
515514 }
516- render . JSON (w , r , R.JSON {"updated" : true , "address" : val })
515+ rest . WriteJSON (w , http . StatusOK , R.JSON {"updated" : true , "address" : val })
517516}
518517
519518// POST/GET /email/unsubscribe.html?site=siteID&tkn=jwt - unsubscribe the user in token from email notifications
@@ -597,7 +596,9 @@ func (s *private) emailUnsubscribeCtrl(w http.ResponseWriter, r *http.Request) {
597596 tmpl := template .Must (template .New ("unsubscribe" ).Parse (tmplstr ))
598597 msg := bytes.Buffer {}
599598 MustExecute (tmpl , & msg , nil )
600- render .HTML (w , r , msg .String ())
599+ w .Header ().Set ("Content-Type" , "text/html; charset=utf-8" )
600+ w .WriteHeader (http .StatusOK )
601+ _ , _ = w .Write (msg .Bytes ())
601602}
602603
603604// DELETE /email?site=siteID - removes user's email
@@ -624,7 +625,7 @@ func (s *private) deleteEmailCtrl(w http.ResponseWriter, r *http.Request) {
624625 return
625626 }
626627 }
627- render . JSON (w , r , R.JSON {"deleted" : true })
628+ rest . WriteJSON (w , http . StatusOK , R.JSON {"deleted" : true })
628629}
629630
630631// DELETE /telegram?site=siteID - removes user's telegram
@@ -638,7 +639,7 @@ func (s *private) deleteTelegramCtrl(w http.ResponseWriter, r *http.Request) {
638639 rest .SendErrorJSON (w , r , http .StatusBadRequest , err , "can't delete telegram for user" , code )
639640 return
640641 }
641- render . JSON (w , r , R.JSON {"deleted" : true })
642+ rest . WriteJSON (w , http . StatusOK , R.JSON {"deleted" : true })
642643}
643644
644645// GET /userdata?site=siteID - exports all data about the user as a json with user info and list of all comments
@@ -726,7 +727,7 @@ func (s *private) deleteMeCtrl(w http.ResponseWriter, r *http.Request) {
726727 }
727728
728729 link := fmt .Sprintf ("%s/web/deleteme.html?token=%s" , s .remarkURL , tokenStr )
729- render . JSON (w , r , R.JSON {"site" : siteID , "user_id" : user .ID , "token" : tokenStr , "link" : link })
730+ rest . WriteJSON (w , http . StatusOK , R.JSON {"site" : siteID , "user_id" : user .ID , "token" : tokenStr , "link" : link })
730731}
731732
732733// POST /image - save image with form request
@@ -751,7 +752,7 @@ func (s *private) savePictureCtrl(w http.ResponseWriter, r *http.Request) {
751752 return
752753 }
753754
754- render . JSON (w , r , R.JSON {"id" : id })
755+ rest . WriteJSON (w , http . StatusOK , R.JSON {"id" : id })
755756}
756757
757758func (s * private ) isReadOnly (locator store.Locator ) bool {
0 commit comments