initial commit
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
|
||||
"github.com/owen/vibedns/internal/auditlog"
|
||||
"github.com/owen/vibedns/internal/cache"
|
||||
)
|
||||
|
||||
// CacheStats returns the cache counters for the dashboard and cache page.
|
||||
func (a *App) CacheStats() cache.Stats { return a.Cache.Stats() }
|
||||
|
||||
// CacheEntries browses the cache.
|
||||
func (a *App) CacheEntries(search string, limit, offset int) ([]cache.EntryView, int) {
|
||||
return a.Cache.Entries(search, limit, offset)
|
||||
}
|
||||
|
||||
// FlushCache empties the resolver cache.
|
||||
func (a *App) FlushCache(ctx context.Context, actor auditlog.Actor) (int, error) {
|
||||
n := a.Cache.Flush()
|
||||
a.Audit.Record(ctx, actor, "cache.flush", auditlog.ObjectCache, "", "resolver cache",
|
||||
auditlog.Changes("entries", fmt.Sprint(n)))
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// FlushCacheName removes every cached entry for one name.
|
||||
func (a *App) FlushCacheName(ctx context.Context, actor auditlog.Actor, name string) (int, error) {
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" {
|
||||
return 0, Invalid("Enter a name to remove from the cache.")
|
||||
}
|
||||
n := a.Cache.FlushName(name)
|
||||
if n == 0 {
|
||||
return 0, NotFound("%s is not in the cache.", strings.TrimSuffix(dns.Fqdn(name), "."))
|
||||
}
|
||||
a.Audit.Record(ctx, actor, "cache.flush_name", auditlog.ObjectCache, "", name,
|
||||
auditlog.Changes("entries", fmt.Sprint(n)))
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// DeleteCacheEntry removes one specific cached response.
|
||||
func (a *App) DeleteCacheEntry(ctx context.Context, actor auditlog.Actor, name, qtype string, do bool) error {
|
||||
name = strings.ToLower(dns.Fqdn(strings.TrimSpace(name)))
|
||||
t, ok := dns.StringToType[strings.ToUpper(strings.TrimSpace(qtype))]
|
||||
if !ok {
|
||||
return Invalid("%q is not a known record type.", qtype)
|
||||
}
|
||||
key := cache.Key{Name: name, Type: t, Class: dns.ClassINET, DO: do}
|
||||
if !a.Cache.Delete(key) {
|
||||
return NotFound("%s %s is not in the cache.", strings.TrimSuffix(name, "."), strings.ToUpper(qtype))
|
||||
}
|
||||
a.Audit.Record(ctx, actor, "cache.delete_entry", auditlog.ObjectCache, "", key.String(), "")
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResetStats zeroes the runtime counters.
|
||||
func (a *App) ResetStats(ctx context.Context, actor auditlog.Actor) {
|
||||
a.Metrics.Reset()
|
||||
a.Cache.ResetStats()
|
||||
a.Audit.Record(ctx, actor, "stats.reset", auditlog.ObjectCache, "", "statistics", "")
|
||||
}
|
||||
Reference in New Issue
Block a user