website/pkg/controller/api/json_layer.go
Noah 8419958b25 Likes on Comments, and other minor improvements
* Add "Like" buttons to comments and forum posts.
* Make "private" profiles more private (logged-in users see only their profile
  pic, display name, and can friend request or message, if they are not approved
  friends of the private user)
* Add "logged-out view" visibility setting to profiles: to share a link to your
  page on other sites. Opt-in setting - default is login required to view your
  public profile page.
* CSRF cookie fix.
* Updated FAQ & Privacy pages.
2022-08-29 20:00:15 -07:00

52 lines
987 B
Go

package api
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
// Envelope is the standard JSON response envelope.
type Envelope struct {
Data interface{} `json:"data"`
StatusCode int
}
// ParseJSON request body.
func ParseJSON(r *http.Request, v interface{}) error {
if r.Header.Get("Content-Type") != "application/json" {
return errors.New("request Content-Type must be application/json")
}
// Parse request body.
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
// Parse params from JSON.
if err := json.Unmarshal(body, v); err != nil {
return err
}
return nil
}
// SendJSON response.
func SendJSON(w http.ResponseWriter, statusCode int, v interface{}) {
buf, err := json.Marshal(Envelope{
Data: v,
StatusCode: statusCode,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
w.Write(buf)
}