Skip to content

Commit

Permalink
all: imp naming
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Jul 21, 2023
1 parent ea4e951 commit 9b77087
Show file tree
Hide file tree
Showing 22 changed files with 55 additions and 63 deletions.
34 changes: 13 additions & 21 deletions internal/aghhttp/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,27 @@ func (t *JSONTime) UnmarshalJSON(b []byte) (err error) {
return nil
}

// 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) {
// WriteJSONResponse writes headers with the code, encodes resp 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, code int, resp any) {
h := w.Header()
h.Set(httphdr.ContentType, HdrValApplicationJSON)
h.Set(httphdr.Server, UserAgent())

w.WriteHeader(code)

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

return err
}

// WriteJSONOKResponse writes headers with the code 200 OK, encodes v into w,
// WriteJSONResponseOK 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)
func WriteJSONResponseOK(w http.ResponseWriter, r *http.Request, v any) {
WriteJSONResponse(w, r, http.StatusOK, v)
}

// ErrorCode is the error code as used by the HTTP API. See the ErrorCode
Expand All @@ -137,13 +129,13 @@ type HTTPAPIErrorResp struct {
Msg string `json:"msg"`
}

// WriteJSONErrorResponse encodes err as a JSON error into w, and logs any
// WriteJSONResponseError encodes err as a JSON error into w, and logs any
// errors it encounters. r is used to get additional information from the
// request.
func WriteJSONErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
log.Error("websvc: %s %s: %s", r.Method, r.URL.Path, err)
func WriteJSONResponseError(w http.ResponseWriter, r *http.Request, err error) {
log.Error("aghhttp: writing json error to %s %s: %s", r.Method, r.URL.Path, err)

_ = WriteJSONResponseCode(w, r, http.StatusUnprocessableEntity, &HTTPAPIErrorResp{
WriteJSONResponse(w, r, http.StatusUnprocessableEntity, &HTTPAPIErrorResp{
Code: ErrorCodeTMP000,
Msg: err.Error(),
})
Expand Down
6 changes: 3 additions & 3 deletions internal/dhcpd/http_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (s *server) handleDHCPStatus(w http.ResponseWriter, r *http.Request) {
status.Leases = leasesToDynamic(s.Leases(LeasesDynamic))
status.StaticLeases = leasesToStatic(s.Leases(LeasesStatic))

_ = aghhttp.WriteJSONResponse(w, r, status)
aghhttp.WriteJSONResponseOK(w, r, status)
}

func (s *server) enableDHCP(ifaceName string) (code int, err error) {
Expand Down Expand Up @@ -395,7 +395,7 @@ func (s *server) handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
}
}

_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

// newNetInterfaceJSON creates a JSON object from a [net.Interface] iface.
Expand Down Expand Up @@ -547,7 +547,7 @@ func (s *server) handleDHCPFindActiveServer(w http.ResponseWriter, r *http.Reque

setOtherDHCPResult(ifaceName, result)

_ = aghhttp.WriteJSONResponse(w, r, result)
aghhttp.WriteJSONResponseOK(w, r, result)
}

// setOtherDHCPResult sets the results of the check for another DHCP server in
Expand Down
2 changes: 1 addition & 1 deletion internal/dhcpd/http_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type jsonError struct {
// TODO(a.garipov): Either take the logger from the server after we've
// refactored logging or make this not a method of *Server.
func (s *server) notImplemented(w http.ResponseWriter, r *http.Request) {
_ = aghhttp.WriteJSONResponseCode(w, r, http.StatusNotImplemented, &jsonError{
aghhttp.WriteJSONResponse(w, r, http.StatusNotImplemented, &jsonError{
Message: aghos.Unsupported("dhcp").Error(),
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (s *Server) accessListJSON() (j accessListJSON) {
}

func (s *Server) handleAccessList(w http.ResponseWriter, r *http.Request) {
_ = aghhttp.WriteJSONResponse(w, r, s.accessListJSON())
aghhttp.WriteJSONResponseOK(w, r, s.accessListJSON())
}

// validateAccessSet checks the internal accessListJSON lists. To search for
Expand Down
4 changes: 2 additions & 2 deletions internal/dnsforward/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (s *Server) getDNSConfig() (c *jsonDNSConfig) {
// handleGetConfig handles requests to the GET /control/dns_info endpoint.
func (s *Server) handleGetConfig(w http.ResponseWriter, r *http.Request) {
resp := s.getDNSConfig()
_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

func (req *jsonDNSConfig) checkBlockingMode() (err error) {
Expand Down Expand Up @@ -757,7 +757,7 @@ func (s *Server) handleTestUpstreamDNS(w http.ResponseWriter, r *http.Request) {
}
}

_ = aghhttp.WriteJSONResponse(w, r, result)
aghhttp.WriteJSONResponseOK(w, r, result)
}

// handleCacheClear is the handler for the POST /control/cache_clear HTTP API.
Expand Down
8 changes: 4 additions & 4 deletions internal/filtering/blocked.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ func (d *DNSFilter) ApplyBlockedServicesList(setts *Settings, list []string) {
}

func (d *DNSFilter) handleBlockedServicesIDs(w http.ResponseWriter, r *http.Request) {
_ = aghhttp.WriteJSONResponse(w, r, serviceIDs)
aghhttp.WriteJSONResponseOK(w, r, serviceIDs)
}

func (d *DNSFilter) handleBlockedServicesAll(w http.ResponseWriter, r *http.Request) {
_ = aghhttp.WriteJSONResponse(w, r, struct {
aghhttp.WriteJSONResponseOK(w, r, struct {
BlockedServices []blockedService `json:"blocked_services"`
}{
BlockedServices: blockedServices,
Expand All @@ -134,7 +134,7 @@ func (d *DNSFilter) handleBlockedServicesList(w http.ResponseWriter, r *http.Req
list := d.Config.BlockedServices.IDs
d.confLock.RUnlock()

_ = aghhttp.WriteJSONResponse(w, r, list)
aghhttp.WriteJSONResponseOK(w, r, list)
}

// handleBlockedServicesSet is the handler for the POST
Expand Down Expand Up @@ -170,7 +170,7 @@ func (d *DNSFilter) handleBlockedServicesGet(w http.ResponseWriter, r *http.Requ
bsvc = d.Config.BlockedServices.Clone()
}()

_ = aghhttp.WriteJSONResponse(w, r, bsvc)
aghhttp.WriteJSONResponseOK(w, r, bsvc)
}

// handleBlockedServicesUpdate is the handler for the PUT
Expand Down
10 changes: 5 additions & 5 deletions internal/filtering/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (d *DNSFilter) handleFilteringRefresh(w http.ResponseWriter, r *http.Reques
return
}

_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

type filterJSON struct {
Expand Down Expand Up @@ -354,7 +354,7 @@ func (d *DNSFilter) handleFilteringStatus(w http.ResponseWriter, r *http.Request
resp.UserRules = d.UserRules
d.filtersMu.RUnlock()

_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

// Set filtering configuration
Expand Down Expand Up @@ -456,7 +456,7 @@ func (d *DNSFilter) handleCheckHost(w http.ResponseWriter, r *http.Request) {
}
}

_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

// setProtectedBool sets the value of a boolean pointer under a lock. l must
Expand Down Expand Up @@ -504,7 +504,7 @@ func (d *DNSFilter) handleSafeBrowsingStatus(w http.ResponseWriter, r *http.Requ
Enabled: protectedBool(&d.confLock, &d.Config.SafeBrowsingEnabled),
}

_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

// handleParentalEnable is the handler for the POST /control/parental/enable
Expand All @@ -530,7 +530,7 @@ func (d *DNSFilter) handleParentalStatus(w http.ResponseWriter, r *http.Request)
Enabled: protectedBool(&d.confLock, &d.Config.ParentalEnabled),
}

_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

// RegisterFilteringHandlers - register handlers
Expand Down
2 changes: 1 addition & 1 deletion internal/filtering/rewritehttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (d *DNSFilter) handleRewriteList(w http.ResponseWriter, r *http.Request) {
}
d.confLock.Unlock()

_ = aghhttp.WriteJSONResponse(w, r, arr)
aghhttp.WriteJSONResponseOK(w, r, arr)
}

func (d *DNSFilter) handleRewriteAdd(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion internal/filtering/safesearchhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (d *DNSFilter) handleSafeSearchStatus(w http.ResponseWriter, r *http.Reques
resp = d.Config.SafeSearchConf
}()

_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

// handleSafeSearchSettings is the handler for PUT /control/safesearch/settings
Expand Down
4 changes: 2 additions & 2 deletions internal/home/clientshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, r *http

data.Tags = clientTags

_ = aghhttp.WriteJSONResponse(w, r, data)
aghhttp.WriteJSONResponseOK(w, r, data)
}

// jsonToClient converts JSON object to Client object.
Expand Down Expand Up @@ -364,7 +364,7 @@ func (clients *clientsContainer) handleFindClient(w http.ResponseWriter, r *http
})
}

_ = aghhttp.WriteJSONResponse(w, r, data)
aghhttp.WriteJSONResponseOK(w, r, data)
}

// findRuntime looks up the IP in runtime and temporary storages, like
Expand Down
2 changes: 1 addition & 1 deletion internal/home/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func handleStatus(w http.ResponseWriter, r *http.Request) {
resp.IsDHCPAvailable = Context.dhcpServer != nil
}

_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

// ------------------------
Expand Down
4 changes: 2 additions & 2 deletions internal/home/controlinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (web *webAPI) handleInstallGetAddresses(w http.ResponseWriter, r *http.Requ
data.Interfaces[iface.Name] = iface
}

_ = aghhttp.WriteJSONResponse(w, r, data)
aghhttp.WriteJSONResponseOK(w, r, data)
}

type checkConfReqEnt struct {
Expand Down Expand Up @@ -190,7 +190,7 @@ func (web *webAPI) handleInstallCheckConfig(w http.ResponseWriter, r *http.Reque
resp.StaticIP = handleStaticIP(req.DNS.IP, req.SetStaticIP)
}

_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

// handleStaticIP - handles static IP request
Expand Down
4 changes: 2 additions & 2 deletions internal/home/controlupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func handleVersionJSON(w http.ResponseWriter, r *http.Request) {
resp := &versionResponse{}
if Context.disableUpdate {
resp.Disabled = true
_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)

return
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func handleVersionJSON(w http.ResponseWriter, r *http.Request) {
return
}

_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

// requestVersionInfo sets the VersionInfo field of resp if it can reach the
Expand Down
2 changes: 1 addition & 1 deletion internal/home/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type languageJSON struct {
func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
log.Printf("home: language is %s", config.Language)

_ = aghhttp.WriteJSONResponse(w, r, &languageJSON{
aghhttp.WriteJSONResponseOK(w, r, &languageJSON{
Language: config.Language,
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/home/profilehttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func handleGetProfile(w http.ResponseWriter, r *http.Request) {
}
}()

_ = aghhttp.WriteJSONResponse(w, r, resp)
aghhttp.WriteJSONResponseOK(w, r, resp)
}

// handlePutProfile is the handler for PUT /control/profile/update endpoint.
Expand Down
2 changes: 1 addition & 1 deletion internal/home/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ func marshalTLS(w http.ResponseWriter, r *http.Request, data tlsConfig) {
data.PrivateKey = ""
}

_ = aghhttp.WriteJSONResponse(w, r, data)
aghhttp.WriteJSONResponseOK(w, r, data)
}

// registerWebHandlers registers HTTP handlers for TLS configuration.
Expand Down
8 changes: 4 additions & 4 deletions internal/next/websvc/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (svc *Service) handlePatchSettingsDNS(w http.ResponseWriter, r *http.Reques

err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
aghhttp.WriteJSONErrorResponse(w, r, fmt.Errorf("decoding: %w", err))
aghhttp.WriteJSONResponseError(w, r, fmt.Errorf("decoding: %w", err))

return
}
Expand All @@ -72,20 +72,20 @@ func (svc *Service) handlePatchSettingsDNS(w http.ResponseWriter, r *http.Reques
ctx := r.Context()
err = svc.confMgr.UpdateDNS(ctx, newConf)
if err != nil {
aghhttp.WriteJSONErrorResponse(w, r, fmt.Errorf("updating: %w", err))
aghhttp.WriteJSONResponseError(w, r, fmt.Errorf("updating: %w", err))

return
}

newSvc := svc.confMgr.DNS()
err = newSvc.Start()
if err != nil {
aghhttp.WriteJSONErrorResponse(w, r, fmt.Errorf("starting new service: %w", err))
aghhttp.WriteJSONResponseError(w, r, fmt.Errorf("starting new service: %w", err))

return
}

aghhttp.WriteJSONOKResponse(w, r, &HTTPAPIDNSSettings{
aghhttp.WriteJSONResponseOK(w, r, &HTTPAPIDNSSettings{
Addresses: newConf.Addresses,
BootstrapServers: newConf.BootstrapServers,
UpstreamServers: newConf.UpstreamServers,
Expand Down
4 changes: 2 additions & 2 deletions internal/next/websvc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Reque

err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
aghhttp.WriteJSONErrorResponse(w, r, fmt.Errorf("decoding: %w", err))
aghhttp.WriteJSONResponseError(w, r, fmt.Errorf("decoding: %w", err))

return
}
Expand All @@ -62,7 +62,7 @@ func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Reque
ForceHTTPS: svc.forceHTTPS,
}

aghhttp.WriteJSONOKResponse(w, r, &HTTPAPIHTTPSettings{
aghhttp.WriteJSONResponseOK(w, r, &HTTPAPIHTTPSettings{
Addresses: newConf.Addresses,
SecureAddresses: newConf.SecureAddresses,
Timeout: aghhttp.JSONDuration(newConf.Timeout),
Expand Down
2 changes: 1 addition & 1 deletion internal/next/websvc/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (svc *Service) handleGetSettingsAll(w http.ResponseWriter, r *http.Request)
httpConf := webSvc.Config()

// TODO(a.garipov): Add all currently supported parameters.
aghhttp.WriteJSONOKResponse(w, r, &RespGetV1SettingsAll{
aghhttp.WriteJSONResponseOK(w, r, &RespGetV1SettingsAll{
DNS: &HTTPAPIDNSSettings{
Addresses: dnsConf.Addresses,
BootstrapServers: dnsConf.BootstrapServers,
Expand Down
2 changes: 1 addition & 1 deletion internal/next/websvc/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type RespGetV1SystemInfo struct {
// handleGetV1SystemInfo is the handler for the GET /api/v1/system/info HTTP
// API.
func (svc *Service) handleGetV1SystemInfo(w http.ResponseWriter, r *http.Request) {
aghhttp.WriteJSONOKResponse(w, r, &RespGetV1SystemInfo{
aghhttp.WriteJSONResponseOK(w, r, &RespGetV1SystemInfo{
Arch: runtime.GOARCH,
Channel: version.Channel(),
OS: runtime.GOOS,
Expand Down
Loading

0 comments on commit 9b77087

Please sign in to comment.