all: imp response; fmt
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/AdGuardHome/internal/version"
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,3 +29,9 @@ func Error(r *http.Request, w http.ResponseWriter, code int, format string, args
|
|||||||
log.Error("%s %s: %s", r.Method, r.URL, text)
|
log.Error("%s %s: %s", r.Method, r.URL, text)
|
||||||
http.Error(w, text, code)
|
http.Error(w, text, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserAgent returns the ID of the service as a User-Agent string. It can also
|
||||||
|
// be used as the value of the Server HTTP header.
|
||||||
|
func UserAgent() (ua string) {
|
||||||
|
return fmt.Sprintf("AdGuardDNS/%s", version.Version())
|
||||||
|
}
|
||||||
|
|||||||
21
internal/aghhttp/header.go
Normal file
21
internal/aghhttp/header.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package aghhttp
|
||||||
|
|
||||||
|
// HTTP Headers
|
||||||
|
|
||||||
|
// HTTP header name constants.
|
||||||
|
//
|
||||||
|
// TODO(a.garipov): Remove unused.
|
||||||
|
const (
|
||||||
|
HdrNameAcceptEncoding = "Accept-Encoding"
|
||||||
|
HdrNameAccessControlAllowOrigin = "Access-Control-Allow-Origin"
|
||||||
|
HdrNameContentType = "Content-Type"
|
||||||
|
HdrNameContentEncoding = "Content-Encoding"
|
||||||
|
HdrNameServer = "Server"
|
||||||
|
HdrNameTrailer = "Trailer"
|
||||||
|
HdrNameUserAgent = "User-Agent"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HTTP header value constants.
|
||||||
|
const (
|
||||||
|
HdrValApplicationJSON = "application/json"
|
||||||
|
)
|
||||||
@@ -8,11 +8,15 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// JSON Utilities
|
// JSON Utilities
|
||||||
|
|
||||||
|
// nsecPerMsec is the number of nanoseconds in a millisecond.
|
||||||
|
const nsecPerMsec = float64(time.Millisecond / time.Nanosecond)
|
||||||
|
|
||||||
// JSONDuration is a time.Duration that can be decoded from JSON and encoded
|
// JSONDuration is a time.Duration that can be decoded from JSON and encoded
|
||||||
// into JSON according to our API conventions.
|
// into JSON according to our API conventions.
|
||||||
type JSONDuration time.Duration
|
type JSONDuration time.Duration
|
||||||
@@ -55,9 +59,6 @@ type JSONTime time.Time
|
|||||||
// type check
|
// type check
|
||||||
var _ json.Marshaler = JSONTime{}
|
var _ json.Marshaler = JSONTime{}
|
||||||
|
|
||||||
// nsecPerMsec is the number of nanoseconds in a millisecond.
|
|
||||||
const nsecPerMsec = float64(time.Millisecond / time.Nanosecond)
|
|
||||||
|
|
||||||
// MarshalJSON implements the json.Marshaler interface for JSONTime. err is
|
// MarshalJSON implements the json.Marshaler interface for JSONTime. err is
|
||||||
// always nil.
|
// always nil.
|
||||||
func (t JSONTime) MarshalJSON() (b []byte, err error) {
|
func (t JSONTime) MarshalJSON() (b []byte, err error) {
|
||||||
@@ -88,7 +89,12 @@ func (t *JSONTime) UnmarshalJSON(b []byte) (err error) {
|
|||||||
|
|
||||||
// writeJSONResponse encodes v into w and logs any errors it encounters. r is
|
// writeJSONResponse encodes v into w and logs any errors it encounters. r is
|
||||||
// used to get additional information from the request.
|
// used to get additional information from the request.
|
||||||
func writeJSONResponse(w io.Writer, r *http.Request, v any) {
|
func writeJSONResponse(w http.ResponseWriter, r *http.Request, v any) {
|
||||||
|
// TODO(a.garipov): Put some of these to a middleware.
|
||||||
|
h := w.Header()
|
||||||
|
h.Set(aghhttp.HdrNameContentType, aghhttp.HdrValApplicationJSON)
|
||||||
|
h.Set(aghhttp.HdrNameServer, aghhttp.UserAgent())
|
||||||
|
|
||||||
err := json.NewEncoder(w).Encode(v)
|
err := json.NewEncoder(w).Encode(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("websvc: writing resp to %s %s: %s", r.Method, r.URL.Path, err)
|
log.Error("websvc: writing resp to %s %s: %s", r.Method, r.URL.Path, err)
|
||||||
|
|||||||
@@ -63,14 +63,6 @@ func Version() (v string) {
|
|||||||
return version
|
return version
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constants defining the format of module information string.
|
|
||||||
const (
|
|
||||||
modInfoAtSep = "@"
|
|
||||||
modInfoDevSep = " "
|
|
||||||
modInfoSumLeft = " (sum: "
|
|
||||||
modInfoSumRight = ")"
|
|
||||||
)
|
|
||||||
|
|
||||||
// fmtModule returns formatted information about module. The result looks like:
|
// fmtModule returns formatted information about module. The result looks like:
|
||||||
//
|
//
|
||||||
// github.com/Username/module@v1.2.3 (sum: someHASHSUM=)
|
// github.com/Username/module@v1.2.3 (sum: someHASHSUM=)
|
||||||
@@ -87,14 +79,16 @@ func fmtModule(m *debug.Module) (formatted string) {
|
|||||||
|
|
||||||
stringutil.WriteToBuilder(b, m.Path)
|
stringutil.WriteToBuilder(b, m.Path)
|
||||||
if ver := m.Version; ver != "" {
|
if ver := m.Version; ver != "" {
|
||||||
sep := modInfoAtSep
|
sep := "@"
|
||||||
if ver == "(devel)" {
|
if ver == "(devel)" {
|
||||||
sep = modInfoDevSep
|
sep = " "
|
||||||
}
|
}
|
||||||
|
|
||||||
stringutil.WriteToBuilder(b, sep, ver)
|
stringutil.WriteToBuilder(b, sep, ver)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sum := m.Sum; sum != "" {
|
if sum := m.Sum; sum != "" {
|
||||||
stringutil.WriteToBuilder(b, modInfoSumLeft, sum, modInfoSumRight)
|
stringutil.WriteToBuilder(b, "(sum: ", sum, ")")
|
||||||
}
|
}
|
||||||
|
|
||||||
return b.String()
|
return b.String()
|
||||||
|
|||||||
Reference in New Issue
Block a user