Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Jul 19, 2023
1 parent 98a705b commit ea4e951
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 44 deletions.
21 changes: 0 additions & 21 deletions internal/aghhttp/aghhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package aghhttp

import (
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -61,23 +60,3 @@ func WriteTextPlainDeprecated(w http.ResponseWriter, r *http.Request) (isPlainTe

return true
}

// WriteJSONResponse sets the content-type header in w.Header() to
// "application/json", writes a header with a "200 OK" status, encodes resp to
// w, calls [Error] on any returned error, and returns it as well.
func WriteJSONResponse(w http.ResponseWriter, r *http.Request, resp any) (err error) {
return WriteJSONResponseCode(w, r, http.StatusOK, resp)
}

// WriteJSONResponseCode is like [WriteJSONResponse] but adds the ability to
// redefine the status code.
func WriteJSONResponseCode(w http.ResponseWriter, r *http.Request, code int, resp any) (err error) {
w.Header().Set(httphdr.ContentType, HdrValApplicationJSON)
w.WriteHeader(code)
err = json.NewEncoder(w).Encode(resp)
if err != nil {
Error(r, w, http.StatusInternalServerError, "encoding resp: %s", err)
}

return err
}
35 changes: 21 additions & 14 deletions internal/aghhttp/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,35 @@ func (t *JSONTime) UnmarshalJSON(b []byte) (err error) {
return nil
}

// WriteJSONOKResponse writes headers with the code 200 OK, encodes v into w,
// and logs any errors it encounters. r is used to get additional information
// from the request.
func WriteJSONOKResponse(w http.ResponseWriter, r *http.Request, v any) {
writeJSONResponse(w, r, v, http.StatusOK)
// WriteJSONResponse sets the content-type header in w.Header() to
// "application/json", writes a header with a "200 OK" status, encodes resp to
// w, calls [Error] on any returned error, and returns it as well.
func WriteJSONResponse(w http.ResponseWriter, r *http.Request, resp any) (err error) {
return WriteJSONResponseCode(w, r, http.StatusOK, resp)
}

// writeJSONResponse writes headers with code, encodes v into w, and logs any
// errors it encounters. r is used to get additional information from the
// request.
func writeJSONResponse(w http.ResponseWriter, r *http.Request, v any, code int) {
// TODO(a.garipov): Put some of these to a middleware.
// WriteJSONResponseCode is like [WriteJSONResponse] but adds the ability to
// redefine the status code.
func WriteJSONResponseCode(w http.ResponseWriter, r *http.Request, code int, resp any) (err error) {
h := w.Header()
h.Set(httphdr.ContentType, HdrValApplicationJSON)
h.Set(httphdr.Server, UserAgent())

w.WriteHeader(code)

err := json.NewEncoder(w).Encode(v)
err = json.NewEncoder(w).Encode(resp)
if err != nil {
log.Error("websvc: writing resp to %s %s: %s", r.Method, r.URL.Path, err)
Error(r, w, http.StatusInternalServerError, "encoding resp: %s", err)
}

return err
}

// WriteJSONOKResponse writes headers with the code 200 OK, encodes v into w,
// and logs any errors it encounters. r is used to get additional information
// from the request.
func WriteJSONOKResponse(w http.ResponseWriter, r *http.Request, v any) {
_ = WriteJSONResponseCode(w, r, http.StatusOK, v)
}

// ErrorCode is the error code as used by the HTTP API. See the ErrorCode
Expand Down Expand Up @@ -136,8 +143,8 @@ type HTTPAPIErrorResp struct {
func WriteJSONErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
log.Error("websvc: %s %s: %s", r.Method, r.URL.Path, err)

writeJSONResponse(w, r, &HTTPAPIErrorResp{
_ = WriteJSONResponseCode(w, r, http.StatusUnprocessableEntity, &HTTPAPIErrorResp{
Code: ErrorCodeTMP000,
Msg: err.Error(),
}, http.StatusUnprocessableEntity)
})
}
19 changes: 10 additions & 9 deletions internal/home/clientshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,21 @@ func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *C

weekly, ignoreQueryLog, ignoreStatistics := cj.copySettings(prev)

bs := &filtering.BlockedServices{
Schedule: weekly,
IDs: cj.BlockedServices,
}
err = bs.Validate()
if err != nil {
return nil, fmt.Errorf("validating blocked services: %w", err)
}

c = &Client{
safeSearchConf: safeSearchConf,

Name: cj.Name,

BlockedServices: &filtering.BlockedServices{
Schedule: weekly,
IDs: cj.BlockedServices,
},
BlockedServices: bs,

IDs: cj.IDs,
Tags: cj.Tags,
Expand All @@ -176,11 +182,6 @@ func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *C
IgnoreStatistics: ignoreStatistics,
}

err = c.BlockedServices.Validate()
if err != nil {
return nil, fmt.Errorf("validating blocked services: %w", err)
}

if safeSearchConf.Enabled {
err = c.setSafeSearch(
safeSearchConf,
Expand Down

0 comments on commit ea4e951

Please sign in to comment.