func AddRule(rule *models.ProxyRule) error {
	rules := Get(rule.Host)
	return datastore.Put(datastore.BucketProxies, rule.Host, append(rules, rule))
}
AddRule methods adds new proxy rule for the host.
package proxy
import (
	"strings"
	"thumbai/app/datastore"
	"thumbai/app/models"
	"aahframe.work"
)
func All() map[string][]*models.ProxyRule {
	keys := datastore.BucketKeys(datastore.BucketProxies)
	proxies := map[string][]*models.ProxyRule{}
	for _, k := range keys {
		rules := make([]*models.ProxyRule, 0)
		_ = datastore.Get(datastore.BucketProxies, k, &rules)
		proxies[k] = rules
	}
	return proxies
}
func Stats() map[string]int {
	stats := make(map[string]int)
	proxies := All()
	stats["Host"] = len(proxies)
	c := 0
	for _, v := range proxies {
		c += len(v)
	}
	stats["ProxyRules"] = c
	return stats
}
func Import(configs map[string][]*models.ProxyRule) {
	for k, p := range configs {
		if err := DelHost(k); err != nil && err != datastore.ErrRecordNotFound {
			continue
		}
		if err := datastore.Put(datastore.BucketProxies, k, p); err != nil {
			aah.App().Log().Errorf("Unable to import proxy config for host: %s, error: %v", k, err)
		}
	}
	Load(nil)
}
func AddHost(proxyInfo *models.FormTargetURL) error {
	proxyInfo.Host = strings.ToLower(proxyInfo.Host)
	if datastore.IsKeyExists(datastore.BucketProxies, proxyInfo.Host) {
		return datastore.ErrRecordAlreadyExists
	}
	proxyRule := &models.ProxyRule{Host: proxyInfo.Host, TargetURL: proxyInfo.TargetURL}
	if err := datastore.Put(datastore.BucketProxies, proxyRule.Host, append([]*models.ProxyRule{}, proxyRule)); err != nil {
		return err
	}
	h := Thumbai.AddHost(proxyInfo.Host)
	return h.AddProxyRule(proxyRule)
}
func DelHost(hostName string) error {
	return datastore.Del(datastore.BucketProxies, strings.ToLower(hostName))
}
func Get(host string) []*models.ProxyRule {
	host = strings.ToLower(host)
	rules := make([]*models.ProxyRule, 0)
	_ = datastore.Get(datastore.BucketProxies, host, &rules)
	return rules
}
func GetRule(host, targetURL string) *models.ProxyRule {
	rules := Get(host)
	for _, rule := range rules {
		if rule.TargetURL == targetURL {
			return rule
		}
	}
	return nil
}