websvc: add dns and http apis

This commit is contained in:
Ainar Garipov
2022-08-31 17:53:45 +03:00
parent c098960b39
commit 27bd8bc58b
11 changed files with 547 additions and 42 deletions

View File

@@ -27,7 +27,7 @@ const nsecPerMsec = float64(time.Millisecond / time.Nanosecond)
// always nil.
func (t jsonTime) MarshalJSON() (b []byte, err error) {
msec := float64(time.Time(t).UnixNano()) / nsecPerMsec
b = strconv.AppendFloat(nil, msec, 'f', 3, 64)
b = strconv.AppendFloat(nil, msec, 'f', -1, 64)
return b, nil
}
@@ -59,3 +59,16 @@ func writeJSONResponse(w io.Writer, r *http.Request, v any) {
log.Error("websvc: writing resp to %s %s: %s", r.Method, r.URL.Path, err)
}
}
// writeHTTPError is a helper for logging and writing HTTP errors.
//
// TODO(a.garipov): Improve codes, and add JSON error codes.
func writeHTTPError(w http.ResponseWriter, r *http.Request, err error) {
log.Error("websvc: %s %s: %s", r.Method, r.URL.Path, err)
w.WriteHeader(http.StatusUnprocessableEntity)
_, werr := io.WriteString(w, err.Error())
if werr != nil {
log.Debug("websvc: writing error resp to %s %s: %s", r.Method, r.URL.Path, werr)
}
}