-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.go
71 lines (60 loc) · 2.17 KB
/
site.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package peerdb
import (
"encoding/json"
"io"
"strings"
"github.com/alecthomas/kong"
"github.com/olivere/elastic/v7"
"gitlab.com/tozd/go/errors"
"gitlab.com/tozd/waf"
"gopkg.in/yaml.v3"
"gitlab.com/peerdb/peerdb/coordinator"
"gitlab.com/peerdb/peerdb/document"
"gitlab.com/peerdb/peerdb/internal/types"
"gitlab.com/peerdb/peerdb/storage"
"gitlab.com/peerdb/peerdb/store"
)
type Build struct {
Version string `json:"version,omitempty"`
BuildTimestamp string `json:"buildTimestamp,omitempty"`
Revision string `json:"revision,omitempty"`
}
type Site struct {
waf.Site `yaml:",inline"`
Build *Build `json:"build,omitempty" yaml:"-"`
Index string `json:"index,omitempty" yaml:"index,omitempty"`
Schema string `json:"schema,omitempty" yaml:"schema,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
SizeField bool `json:"-" yaml:"sizeField,omitempty"`
// Data for Store is on purpose not document.D so that we can serve it directly without doing first JSON unmarshal just to marshal it again immediately.
store *store.Store[json.RawMessage, *types.DocumentMetadata, *types.NoMetadata, *types.NoMetadata, *types.NoMetadata, document.Changes]
coordinator *coordinator.Coordinator[json.RawMessage, *types.DocumentBeginMetadata, *types.DocumentEndMetadata, *types.DocumentChangeMetadata]
storage *storage.Storage
esProcessor *elastic.BulkProcessor
// TODO: How to keep propertiesTotal in sync with the number of properties available, if they are added or removed after initialization?
propertiesTotal int64
}
func (s *Site) Decode(ctx *kong.DecodeContext) error {
var value string
err := ctx.Scan.PopValueInto("value", &value)
if err != nil {
return errors.WithStack(err)
}
decoder := yaml.NewDecoder(strings.NewReader(value))
decoder.KnownFields(true)
err = decoder.Decode(s) //nolint:musttag
if err != nil {
var yamlErr *yaml.TypeError
if errors.As(err, &yamlErr) {
e := "error"
if len(yamlErr.Errors) > 1 {
e = "errors"
}
return errors.Errorf("yaml: unmarshal %s: %s", e, strings.Join(yamlErr.Errors, "; "))
} else if errors.Is(err, io.EOF) {
return nil
}
return errors.WithStack(err)
}
return nil
}