home: imp code
This commit is contained in:
parent
4a9dc1d6c1
commit
7669419b59
|
@ -61,47 +61,6 @@ type clientJSON struct {
|
||||||
UpstreamsCacheEnabled aghalg.NullBool `json:"upstreams_cache_enabled"`
|
UpstreamsCacheEnabled aghalg.NullBool `json:"upstreams_cache_enabled"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// copySettings returns a copy of specific settings from JSON or a previous
|
|
||||||
// client.
|
|
||||||
func (j *clientJSON) copySettings(
|
|
||||||
prev *Client,
|
|
||||||
) (weekly *schedule.Weekly,
|
|
||||||
ignoreQueryLog,
|
|
||||||
ignoreStatistics,
|
|
||||||
upsCacheEnabled bool,
|
|
||||||
upsCacheSize uint32,
|
|
||||||
) {
|
|
||||||
if j.Schedule != nil {
|
|
||||||
weekly = j.Schedule.Clone()
|
|
||||||
} else if prev != nil && prev.BlockedServices != nil {
|
|
||||||
weekly = prev.BlockedServices.Schedule.Clone()
|
|
||||||
} else {
|
|
||||||
weekly = schedule.EmptyWeekly()
|
|
||||||
}
|
|
||||||
|
|
||||||
if j.IgnoreQueryLog != aghalg.NBNull {
|
|
||||||
ignoreQueryLog = j.IgnoreQueryLog == aghalg.NBTrue
|
|
||||||
} else if prev != nil {
|
|
||||||
ignoreQueryLog = prev.IgnoreQueryLog
|
|
||||||
}
|
|
||||||
|
|
||||||
if j.IgnoreStatistics != aghalg.NBNull {
|
|
||||||
ignoreStatistics = j.IgnoreStatistics == aghalg.NBTrue
|
|
||||||
} else if prev != nil {
|
|
||||||
ignoreStatistics = prev.IgnoreStatistics
|
|
||||||
}
|
|
||||||
|
|
||||||
if j.UpstreamsCacheEnabled != aghalg.NBNull {
|
|
||||||
upsCacheEnabled = j.UpstreamsCacheEnabled == aghalg.NBTrue
|
|
||||||
upsCacheSize = j.UpstreamsCacheSize
|
|
||||||
} else if prev != nil {
|
|
||||||
upsCacheEnabled = prev.UpstreamsCacheEnabled
|
|
||||||
upsCacheSize = prev.UpstreamsCacheSize
|
|
||||||
}
|
|
||||||
|
|
||||||
return weekly, ignoreQueryLog, ignoreStatistics, upsCacheEnabled, upsCacheSize
|
|
||||||
}
|
|
||||||
|
|
||||||
type runtimeClientJSON struct {
|
type runtimeClientJSON struct {
|
||||||
WHOIS *whois.Info `json:"whois_info"`
|
WHOIS *whois.Info `json:"whois_info"`
|
||||||
|
|
||||||
|
@ -158,36 +117,35 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, r *http
|
||||||
|
|
||||||
// jsonToClient converts JSON object to Client object.
|
// jsonToClient converts JSON object to Client object.
|
||||||
func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *Client, err error) {
|
func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *Client, err error) {
|
||||||
var safeSearchConf filtering.SafeSearchConfig
|
safeSearchConf := copySafeSearch(cj.SafeSearchConf, cj.SafeSearchEnabled)
|
||||||
if cj.SafeSearchConf != nil {
|
|
||||||
safeSearchConf = *cj.SafeSearchConf
|
var ignoreQueryLog bool
|
||||||
} else {
|
if cj.IgnoreQueryLog != aghalg.NBNull {
|
||||||
// TODO(d.kolyshev): Remove after cleaning the deprecated
|
ignoreQueryLog = cj.IgnoreQueryLog == aghalg.NBTrue
|
||||||
// [clientJSON.SafeSearchEnabled] field.
|
} else if prev != nil {
|
||||||
safeSearchConf = filtering.SafeSearchConfig{
|
ignoreQueryLog = prev.IgnoreQueryLog
|
||||||
Enabled: cj.SafeSearchEnabled,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set default service flags for enabled safesearch.
|
var ignoreStatistics bool
|
||||||
if safeSearchConf.Enabled {
|
if cj.IgnoreStatistics != aghalg.NBNull {
|
||||||
safeSearchConf.Bing = true
|
ignoreStatistics = cj.IgnoreStatistics == aghalg.NBTrue
|
||||||
safeSearchConf.DuckDuckGo = true
|
} else if prev != nil {
|
||||||
safeSearchConf.Google = true
|
ignoreStatistics = prev.IgnoreStatistics
|
||||||
safeSearchConf.Pixabay = true
|
|
||||||
safeSearchConf.Yandex = true
|
|
||||||
safeSearchConf.YouTube = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
weekly, ignoreQueryLog, ignoreStatistics, upsCacheEnabled, upsCacheSize := cj.copySettings(prev)
|
var upsCacheEnabled bool
|
||||||
|
var upsCacheSize uint32
|
||||||
bs := &filtering.BlockedServices{
|
if cj.UpstreamsCacheEnabled != aghalg.NBNull {
|
||||||
Schedule: weekly,
|
upsCacheEnabled = cj.UpstreamsCacheEnabled == aghalg.NBTrue
|
||||||
IDs: cj.BlockedServices,
|
upsCacheSize = cj.UpstreamsCacheSize
|
||||||
|
} else if prev != nil {
|
||||||
|
upsCacheEnabled = prev.UpstreamsCacheEnabled
|
||||||
|
upsCacheSize = prev.UpstreamsCacheSize
|
||||||
}
|
}
|
||||||
err = bs.Validate()
|
|
||||||
|
bs, err := copyBlockedServices(cj.Schedule, cj.BlockedServices, prev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("validating blocked services: %w", err)
|
return nil, fmt.Errorf("invalid blocked services: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c = &Client{
|
c = &Client{
|
||||||
|
@ -226,6 +184,63 @@ func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *C
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copySafeSearch returns safe search config copied from provided parameters.
|
||||||
|
func copySafeSearch(
|
||||||
|
conf *filtering.SafeSearchConfig,
|
||||||
|
enabled bool,
|
||||||
|
) (safeSearchConf filtering.SafeSearchConfig) {
|
||||||
|
if conf != nil {
|
||||||
|
safeSearchConf = *conf
|
||||||
|
} else {
|
||||||
|
// TODO(d.kolyshev): Remove after cleaning the deprecated
|
||||||
|
// [clientJSON.SafeSearchEnabled] field.
|
||||||
|
safeSearchConf = filtering.SafeSearchConfig{
|
||||||
|
Enabled: enabled,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set default service flags for enabled safesearch.
|
||||||
|
if safeSearchConf.Enabled {
|
||||||
|
safeSearchConf.Bing = true
|
||||||
|
safeSearchConf.DuckDuckGo = true
|
||||||
|
safeSearchConf.Google = true
|
||||||
|
safeSearchConf.Pixabay = true
|
||||||
|
safeSearchConf.Yandex = true
|
||||||
|
safeSearchConf.YouTube = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return safeSearchConf
|
||||||
|
}
|
||||||
|
|
||||||
|
// copyBlockedServices converts a json blocked services to an internal blocked
|
||||||
|
// services.
|
||||||
|
func copyBlockedServices(
|
||||||
|
sch *schedule.Weekly,
|
||||||
|
blockedSvc []string,
|
||||||
|
prev *Client,
|
||||||
|
) (bs *filtering.BlockedServices, err error) {
|
||||||
|
var weekly *schedule.Weekly
|
||||||
|
if sch != nil {
|
||||||
|
weekly = sch.Clone()
|
||||||
|
} else if prev != nil && prev.BlockedServices != nil {
|
||||||
|
weekly = prev.BlockedServices.Schedule.Clone()
|
||||||
|
} else {
|
||||||
|
weekly = schedule.EmptyWeekly()
|
||||||
|
}
|
||||||
|
|
||||||
|
bs = &filtering.BlockedServices{
|
||||||
|
Schedule: weekly,
|
||||||
|
IDs: blockedSvc,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = bs.Validate()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("validating blocked services: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return bs, nil
|
||||||
|
}
|
||||||
|
|
||||||
// clientToJSON converts Client object to JSON.
|
// clientToJSON converts Client object to JSON.
|
||||||
func clientToJSON(c *Client) (cj *clientJSON) {
|
func clientToJSON(c *Client) (cj *clientJSON) {
|
||||||
// TODO(d.kolyshev): Remove after cleaning the deprecated
|
// TODO(d.kolyshev): Remove after cleaning the deprecated
|
||||||
|
|
Loading…
Reference in New Issue