Replace SQLite with BadgerDB

This commit is contained in:
David Stotijn
2022-01-21 11:45:54 +01:00
parent 8a3b3cbf02
commit d84d2d0905
49 changed files with 2496 additions and 2677 deletions

View File

@ -1,24 +1,16 @@
package scope
import (
"context"
"encoding/json"
"errors"
"fmt"
"bytes"
"encoding/gob"
"net/http"
"regexp"
"sync"
"github.com/dstotijn/hetty/pkg/proj"
)
const moduleName = "scope"
type Scope struct {
rules []Rule
repo Repository
mu sync.RWMutex
mu sync.RWMutex
}
type Rule struct {
@ -32,30 +24,6 @@ type Header struct {
Value *regexp.Regexp
}
func New(repo Repository, projService proj.Service) *Scope {
s := &Scope{
repo: repo,
}
projService.OnProjectOpen(func(_ string) error {
err := s.load(context.Background())
if errors.Is(err, proj.ErrNoSettings) {
return nil
}
if err != nil {
return fmt.Errorf("scope: could not load scope: %w", err)
}
return nil
})
projService.OnProjectClose(func(_ string) error {
s.unload()
return nil
})
return s
}
func (s *Scope) Rules() []Rule {
s.mu.RLock()
defer s.mu.RUnlock()
@ -63,41 +31,11 @@ func (s *Scope) Rules() []Rule {
return s.rules
}
func (s *Scope) load(ctx context.Context) error {
func (s *Scope) SetRules(rules []Rule) {
s.mu.Lock()
defer s.mu.Unlock()
var rules []Rule
err := s.repo.FindSettingsByModule(ctx, moduleName, &rules)
if errors.Is(err, proj.ErrNoSettings) {
return err
} else if err != nil {
return fmt.Errorf("scope: could not load scope settings: %w", err)
}
s.rules = rules
return nil
}
func (s *Scope) unload() {
s.mu.Lock()
defer s.mu.Unlock()
s.rules = nil
}
func (s *Scope) SetRules(ctx context.Context, rules []Rule) error {
s.mu.Lock()
defer s.mu.Unlock()
if err := s.repo.UpsertSettings(ctx, moduleName, rules); err != nil {
return fmt.Errorf("scope: cannot set rules in repository: %w", err)
}
s.rules = rules
return nil
}
func (s *Scope) Match(req *http.Request, body []byte) bool {
@ -158,48 +96,54 @@ func (r Rule) Match(req *http.Request, body []byte) bool {
return false
}
// MarshalJSON implements json.Marshaler.
func (r Rule) MarshalJSON() ([]byte, error) {
type (
headerDTO struct {
Key string
Value string
}
ruleDTO struct {
URL string
Header headerDTO
Body string
}
)
dto := ruleDTO{
URL: regexpToString(r.URL),
Header: headerDTO{
Key: regexpToString(r.Header.Key),
Value: regexpToString(r.Header.Value),
},
Body: regexpToString(r.Body),
func regexpToString(r *regexp.Regexp) string {
if r == nil {
return ""
}
return json.Marshal(dto)
return r.String()
}
// UnmarshalJSON implements json.Unmarshaler.
func (r *Rule) UnmarshalJSON(data []byte) error {
type (
headerDTO struct {
Key string
Value string
}
ruleDTO struct {
URL string
Header headerDTO
Body string
}
)
func stringToRegexp(s string) (*regexp.Regexp, error) {
if s == "" {
return nil, nil
}
var dto ruleDTO
if err := json.Unmarshal(data, &dto); err != nil {
return regexp.Compile(s)
}
type ruleDTO struct {
URL string
Header struct {
Key string
Value string
}
Body string
}
func (r Rule) MarshalBinary() ([]byte, error) {
dto := ruleDTO{
URL: regexpToString(r.URL),
Body: regexpToString(r.Body),
}
dto.Header.Key = regexpToString(r.Header.Key)
dto.Header.Value = regexpToString(r.Header.Value)
buf := bytes.Buffer{}
err := gob.NewEncoder(&buf).Encode(dto)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (r *Rule) UnmarshalBinary(data []byte) error {
dto := ruleDTO{}
err := gob.NewDecoder(bytes.NewReader(data)).Decode(&dto)
if err != nil {
return err
}
@ -234,19 +178,3 @@ func (r *Rule) UnmarshalJSON(data []byte) error {
return nil
}
func regexpToString(r *regexp.Regexp) string {
if r == nil {
return ""
}
return r.String()
}
func stringToRegexp(s string) (*regexp.Regexp, error) {
if s == "" {
return nil, nil
}
return regexp.Compile(s)
}