| package main |
| |
| import ( |
| "encoding/json" |
| "net/http" |
| |
| "go.skia.org/infra/go/httputils" |
| ) |
| |
| // setJSONHeaders sets secure headers for JSON responses. |
| func setJSONHeaders(w http.ResponseWriter) { |
| h := w.Header() |
| h.Set("Access-Control-Allow-Origin", "*") |
| h.Set("Content-Type", "application/json") |
| h.Set("X-Content-Type-Options", "nosniff") |
| } |
| |
| // sendJsonResponse serializes resp to JSON. If an error occurs |
| // a text based error code is send to the client. |
| func sendJsonResponse(w http.ResponseWriter, resp interface{}) { |
| setJSONHeaders(w) |
| if err := json.NewEncoder(w).Encode(resp); err != nil { |
| httputils.ReportError(w, nil, err, "Failed to encode JSON response.") |
| } |
| } |