diff --git a/app/peerfinder/ev-info.go b/app/peerfinder/ev-info.go index d5f6b28..fc2aa30 100644 --- a/app/peerfinder/ev-info.go +++ b/app/peerfinder/ev-info.go @@ -2,7 +2,6 @@ package peerfinder import ( "bytes" - "encoding/json" "github.com/tj/go-semver" @@ -12,7 +11,7 @@ import ( type Info struct { ScriptVersion string `json:"script_version"` - event.AggregateRoot + event.IsAggregate } var _ event.Aggregate = (*Info)(nil) @@ -55,10 +54,3 @@ type VersionChanged struct { } var _ event.Event = (*VersionChanged)(nil) - -func (e *VersionChanged) MarshalBinary() (text []byte, err error) { - return json.Marshal(e) -} -func (e *VersionChanged) UnmarshalBinary(b []byte) error { - return json.Unmarshal(b, e) -} diff --git a/app/peerfinder/ev-peer.go b/app/peerfinder/ev-peer.go index 8420651..9f55572 100644 --- a/app/peerfinder/ev-peer.go +++ b/app/peerfinder/ev-peer.go @@ -94,7 +94,7 @@ func (p *Peer) CanSupport(ip string) bool { type PeerResults struct { set.Set[string] - event.AggregateRoot + event.IsAggregate } func (p *PeerResults) ApplyEvent(lis ...event.Event) { diff --git a/app/peerfinder/ev-request.go b/app/peerfinder/ev-request.go index 900a401..3c878ee 100644 --- a/app/peerfinder/ev-request.go +++ b/app/peerfinder/ev-request.go @@ -14,7 +14,7 @@ import ( ) type Request struct { - event.AggregateRoot + event.IsAggregate RequestID string `json:"req_id"` RequestIP string `json:"req_ip"` @@ -213,12 +213,6 @@ func (r *ResultSubmitted) Created() time.Time { var _ event.Event = (*ResultSubmitted)(nil) -func (e *ResultSubmitted) MarshalBinary() (text []byte, err error) { - return json.Marshal(e) -} -func (e *ResultSubmitted) UnmarshalBinary(b []byte) error { - return json.Unmarshal(b, e) -} func (e *ResultSubmitted) String() string { return fmt.Sprintf("id: %s\npeer: %s\nversion: %s\nlatency: %0.4f", e.RequestID, e.PeerID, e.PeerVersion, e.Latency) } @@ -231,12 +225,6 @@ type RequestTruncated struct { var _ event.Event = (*RequestTruncated)(nil) -func (e *RequestTruncated) MarshalBinary() (text []byte, err error) { - return json.Marshal(e) -} -func (e *RequestTruncated) UnmarshalBinary(b []byte) error { - return json.Unmarshal(b, e) -} func (e *RequestTruncated) String() string { return fmt.Sprintf("request truncated id: %s\n", e.RequestID) } diff --git a/app/salty/salty-user.go b/app/salty/salty-user.go index 2d79cd0..7b35659 100644 --- a/app/salty/salty-user.go +++ b/app/salty/salty-user.go @@ -20,7 +20,7 @@ type SaltyUser struct { pubkey *keys.EdX25519PublicKey inbox ulid.ULID - event.AggregateRoot + event.IsAggregate } var _ event.Aggregate = (*SaltyUser)(nil) diff --git a/app/webfinger/events.go b/app/webfinger/events.go index 643bed0..cb9be8d 100644 --- a/app/webfinger/events.go +++ b/app/webfinger/events.go @@ -1,8 +1,6 @@ package webfinger import ( - "encoding/json" - "go.sour.is/ev/pkg/es/event" ) @@ -14,13 +12,6 @@ type SubjectSet struct { event.IsEvent } -func (e *SubjectSet) MarshalBinary() (text []byte, err error) { - return json.Marshal(e) -} -func (e *SubjectSet) UnmarshalBinary(b []byte) error { - return json.Unmarshal(b, e) -} - var _ event.Event = (*SubjectSet)(nil) type SubjectDeleted struct { @@ -29,13 +20,6 @@ type SubjectDeleted struct { event.IsEvent } -func (e *SubjectDeleted) MarshalBinary() (text []byte, err error) { - return json.Marshal(e) -} -func (e *SubjectDeleted) UnmarshalBinary(b []byte) error { - return json.Unmarshal(b, e) -} - var _ event.Event = (*SubjectDeleted)(nil) type LinkSet struct { @@ -48,13 +32,6 @@ type LinkSet struct { event.IsEvent } -func (e *LinkSet) MarshalBinary() (text []byte, err error) { - return json.Marshal(e) -} -func (e *LinkSet) UnmarshalBinary(b []byte) error { - return json.Unmarshal(b, e) -} - var _ event.Event = (*LinkSet)(nil) type LinkDeleted struct { @@ -63,11 +40,4 @@ type LinkDeleted struct { event.IsEvent } -func (e *LinkDeleted) MarshalBinary() (text []byte, err error) { - return json.Marshal(e) -} -func (e *LinkDeleted) UnmarshalBinary(b []byte) error { - return json.Unmarshal(b, e) -} - var _ event.Event = (*LinkDeleted)(nil) diff --git a/app/webfinger/jrd.go b/app/webfinger/jrd.go index be81a9a..7e6a5bf 100644 --- a/app/webfinger/jrd.go +++ b/app/webfinger/jrd.go @@ -28,8 +28,8 @@ type JRD struct { Properties map[string]*string `json:"properties,omitempty" yaml:"properties,omitempty"` Links Links `json:"links,omitempty" yaml:"links,omitempty"` - deleted bool - event.AggregateRoot `yaml:"-"` + deleted bool + event.IsAggregate `yaml:"-"` } func (a *JRD) CloneValues() *JRD { diff --git a/app/webfinger/webfinger.go b/app/webfinger/webfinger.go index 8fd48b4..682eff1 100644 --- a/app/webfinger/webfinger.go +++ b/app/webfinger/webfinger.go @@ -231,14 +231,13 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - host, _ := splitHostPort(r.Host) - - if u := Parse(resource); u != nil && !s.self.Has(host) { + if u := Parse(resource); u != nil && !s.self.Has(u.URL.Host) { redirect := &url.URL{} - redirect.Scheme = u.URL.Scheme + redirect.Scheme = "https" redirect.Host = u.URL.Host redirect.RawQuery = r.URL.RawQuery redirect.Path = "/.well-known/webfinger" + fmt.Println(redirect) w.Header().Set("location", redirect.String()) w.WriteHeader(http.StatusSeeOther) return diff --git a/cmd/webfinger/webfinger_e2e_test.go b/cmd/webfinger/webfinger_e2e_test.go index d34ba37..2799c16 100644 --- a/cmd/webfinger/webfinger_e2e_test.go +++ b/cmd/webfinger/webfinger_e2e_test.go @@ -26,7 +26,7 @@ func TestMain(m *testing.M) { os.Setenv("EV_DATA", "mem:") os.Setenv("EV_HTTP", "[::1]:61234") - os.Setenv("WEBFINGER_DOMAINS", "::1") + os.Setenv("WEBFINGER_DOMAINS", "sour.is") ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -76,7 +76,7 @@ func TestCreateResource(t *testing.T) { is.NoErr(err) jrd := &webfinger.JRD{ - Subject: "me@sour.is", + Subject: "acct:me@sour.is", Properties: map[string]*string{ "foo": ptr("bar"), }, @@ -104,7 +104,7 @@ func TestCreateResource(t *testing.T) { is.Equal(res.StatusCode, http.StatusAlreadyReported) // fetch - req, err = http.NewRequest(http.MethodGet, "http://[::1]:61234/.well-known/webfinger?resource=me@sour.is", nil) + req, err = http.NewRequest(http.MethodGet, "http://[::1]:61234/.well-known/webfinger?resource=acct:me@sour.is", nil) is.NoErr(err) res, err = http.DefaultClient.Do(req) diff --git a/ev_test.go b/ev_test.go index bc79a6d..95a051e 100644 --- a/ev_test.go +++ b/ev_test.go @@ -2,7 +2,6 @@ package ev_test import ( "context" - "encoding/json" "errors" "fmt" "testing" @@ -29,7 +28,7 @@ type Thing struct { Name string Value string - event.AggregateRoot + event.IsAggregate } func (a *Thing) ApplyEvent(lis ...event.Event) { @@ -48,14 +47,7 @@ func (a *Thing) OnSetValue(value string) error { type ValueSet struct { Value string - event.IsEvent -} - -func (e *ValueSet) MarshalBinary() ([]byte, error) { - return json.Marshal(e) -} -func (e *ValueSet) UnmarshalBinary(b []byte) error { - return json.Unmarshal(b, e) + event.IsEvent } func TestES(t *testing.T) { diff --git a/pkg/es/event/aggregate.go b/pkg/es/event/aggregate.go index 4f28cf9..30cec97 100644 --- a/pkg/es/event/aggregate.go +++ b/pkg/es/event/aggregate.go @@ -12,7 +12,7 @@ type Aggregate interface { // ApplyEvent applies the event to the aggrigate state ApplyEvent(...Event) - AggregateRootInterface + AggregateRoot } func Start(a Aggregate, i uint64) { @@ -49,7 +49,7 @@ func ShouldExist(a Aggregate) error { return nil } -type AggregateRootInterface interface { +type AggregateRoot interface { // Events returns the aggregate events // pass true for only uncommitted events Events(bool) Events @@ -68,9 +68,9 @@ type AggregateRootInterface interface { Commit() } -var _ AggregateRootInterface = &AggregateRoot{} +var _ AggregateRoot = &IsAggregate{} -type AggregateRoot struct { +type IsAggregate struct { events Events streamID string firstIndex uint64 @@ -79,12 +79,12 @@ type AggregateRoot struct { mu sync.RWMutex } -func (a *AggregateRoot) Commit() { a.lastIndex = uint64(len(a.events)) } -func (a *AggregateRoot) StreamID() string { return a.streamID } -func (a *AggregateRoot) SetStreamID(streamID string) { a.streamID = streamID } -func (a *AggregateRoot) StreamVersion() uint64 { return a.lastIndex } -func (a *AggregateRoot) Version() uint64 { return a.firstIndex + uint64(len(a.events)) } -func (a *AggregateRoot) Events(new bool) Events { +func (a *IsAggregate) Commit() { a.lastIndex = uint64(len(a.events)) } +func (a *IsAggregate) StreamID() string { return a.streamID } +func (a *IsAggregate) SetStreamID(streamID string) { a.streamID = streamID } +func (a *IsAggregate) StreamVersion() uint64 { return a.lastIndex } +func (a *IsAggregate) Version() uint64 { return a.firstIndex + uint64(len(a.events)) } +func (a *IsAggregate) Events(new bool) Events { a.mu.RLock() defer a.mu.RUnlock() @@ -99,13 +99,13 @@ func (a *AggregateRoot) Events(new bool) Events { return lis } -func (a *AggregateRoot) start(i uint64) { +func (a *IsAggregate) start(i uint64) { a.firstIndex = i a.lastIndex = i } //lint:ignore U1000 is called by embeded interface -func (a *AggregateRoot) raise(lis ...Event) { //nolint +func (a *IsAggregate) raise(lis ...Event) { //nolint a.mu.Lock() defer a.mu.Unlock() @@ -115,7 +115,7 @@ func (a *AggregateRoot) raise(lis ...Event) { //nolint } //lint:ignore U1000 is called by embeded interface -func (a *AggregateRoot) append(lis ...Event) { +func (a *IsAggregate) append(lis ...Event) { a.mu.Lock() defer a.mu.Unlock() @@ -125,7 +125,7 @@ func (a *AggregateRoot) append(lis ...Event) { a.lastIndex += uint64(len(lis)) } -func (a *AggregateRoot) posStartAt(lis ...Event) { +func (a *IsAggregate) posStartAt(lis ...Event) { for i, e := range lis { m := e.EventMeta() m.Position = a.lastIndex + uint64(i) + 1 diff --git a/pkg/es/event/aggregate_test.go b/pkg/es/event/aggregate_test.go index 5abe50c..4fbfab1 100644 --- a/pkg/es/event/aggregate_test.go +++ b/pkg/es/event/aggregate_test.go @@ -1,7 +1,6 @@ package event_test import ( - "encoding/json" "testing" "go.sour.is/ev/pkg/es/event" @@ -10,7 +9,7 @@ import ( type Agg struct { Value string - event.AggregateRoot + event.IsAggregate } var _ event.Aggregate = (*Agg)(nil) @@ -38,13 +37,6 @@ type ValueApplied struct { var _ event.Event = (*ValueApplied)(nil) -func (e *ValueApplied) MarshalBinary() ([]byte, error) { - return json.Marshal(e) -} -func (e *ValueApplied) UnmarshalBinary(b []byte) error { - return json.Unmarshal(b, e) -} - func TestAggregate(t *testing.T) { agg := &Agg{} event.Append(agg, &ValueApplied{Value: "one"}) diff --git a/pkg/es/event/events.go b/pkg/es/event/events.go index 51c6abe..3e83b05 100644 --- a/pkg/es/event/events.go +++ b/pkg/es/event/events.go @@ -4,7 +4,6 @@ package event import ( "context" "crypto/rand" - "encoding" "fmt" "io" "strconv" @@ -32,9 +31,6 @@ func getULID() ulid.ULID { type Event interface { EventMeta() Meta SetEventMeta(Meta) - - encoding.BinaryMarshaler - encoding.BinaryUnmarshaler } // Events is a list of events diff --git a/pkg/es/event/reflect.go b/pkg/es/event/reflect.go index d06f724..b455fc0 100644 --- a/pkg/es/event/reflect.go +++ b/pkg/es/event/reflect.go @@ -3,6 +3,7 @@ package event import ( "bytes" "context" + "encoding" "encoding/json" "fmt" "net/url" @@ -151,7 +152,8 @@ func GetContainer(ctx context.Context, s string) Event { return e } -func MarshalBinary(e Event) (txt []byte, err error) { +func MarshalBinary(e Event) ([]byte, error) { + var err error b := &bytes.Buffer{} m := e.EventMeta() @@ -167,10 +169,22 @@ func MarshalBinary(e Event) (txt []byte, err error) { return nil, err } b.WriteRune('\t') - if txt, err = e.MarshalBinary(); err != nil { - return nil, err + switch e := e.(type) { + case encoding.BinaryMarshaler: + var txt []byte + if txt, err = e.MarshalBinary(); err != nil { + return nil, err + } + _, err = b.Write(txt) + case encoding.TextMarshaler: + var txt []byte + if txt, err = e.MarshalText(); err != nil { + return nil, err + } + _, err = b.Write(txt) + default: + err = json.NewEncoder(b).Encode(e) } - _, err = b.Write(txt) return b.Bytes(), err } @@ -200,12 +214,23 @@ func UnmarshalBinary(ctx context.Context, txt []byte, pos uint64) (e Event, err eventType := string(sp[2]) e = GetContainer(ctx, eventType) span.AddEvent(fmt.Sprintf("%s == %T", eventType, e)) - - if err = e.UnmarshalBinary(sp[3]); err != nil { - span.RecordError(err) - return nil, err + switch e := e.(type) { + case encoding.BinaryUnmarshaler: + if err = e.UnmarshalBinary(sp[3]); err != nil { + span.RecordError(err) + return nil, err + } + case encoding.TextUnmarshaler: + if err = e.UnmarshalText(sp[3]); err != nil { + span.RecordError(err) + return nil, err + } + default: + if err = json.Unmarshal(sp[3], e); err != nil { + span.RecordError(err) + return nil, err + } } - e.SetEventMeta(m) return e, nil diff --git a/pkg/es/graph.go b/pkg/es/graph.go index 622bbe3..4c73746 100644 --- a/pkg/es/graph.go +++ b/pkg/es/graph.go @@ -2,6 +2,8 @@ package es import ( "context" + "encoding" + "encoding/json" "errors" "fmt" "net/http" @@ -164,8 +166,17 @@ func (e *GQLEvent) Values() map[string]interface{} { return event.Values(e.e) } func (e *GQLEvent) Bytes() (string, error) { - b, err := e.e.MarshalBinary() - return string(b), err + switch e := e.e.(type) { + case encoding.BinaryMarshaler: + b, err := e.MarshalBinary() + return string(b), err + case encoding.TextMarshaler: + b, err := e.MarshalText() + return string(b), err + default: + b, err := json.Marshal(e) + return string(b), err + } } func (e *GQLEvent) Meta() *event.Meta { meta := e.e.EventMeta() diff --git a/pkg/gql/graphiql/playground.go b/pkg/gql/graphiql/playground.go index c95c019..1c23d36 100644 --- a/pkg/gql/graphiql/playground.go +++ b/pkg/gql/graphiql/playground.go @@ -82,10 +82,10 @@ func Handler(title string, endpoint string) http.HandlerFunc { "endpoint": endpoint, "endpointIsAbsolute": endpointHasScheme(endpoint), "subscriptionEndpoint": getSubscriptionEndpoint(endpoint), - "version": "2.0.13", + "version": "2.4.1", "reactVersion": "17.0.2", - "cssSRI": "sha256-qKvndYgkAMQOBoa1SZF9NlbIig+kQ3Fk4f8wlrEqBLw=", - "jsSRI": "sha256-dExtzxjgqXfOgQ94xw079jAjd4dPAFrO2Qz6I3Yd9Ko=", + "cssSRI": "sha256-bGeEsMhcAqeXBjh2w0eQzBTFAxwlxhM0PKIKqMshlnk=", + "jsSRI": "sha256-s+f7CFAPSUIygFnRC2nfoiEKd3liCUy+snSdYFAoLUc=", "reactSRI": "sha256-Ipu/TQ50iCCVZBUsZyNJfxrDk0E2yhaEIz0vqI+kFG8=", "reactDOMSRI": "sha256-nbMykgB6tsOFJ7OdVmPpdqMFVk4ZsqWocT6issAPUF0=", })