refactor: push commands in to cmd and ev to root as library

This commit is contained in:
Jon Lundy
2023-01-09 11:30:02 -07:00
parent 250395d6b3
commit 4fc9c78dae
58 changed files with 765 additions and 376 deletions

3
app/README.md Normal file
View File

@@ -0,0 +1,3 @@
# App examples
These applications are to demonstrate how the EV library can be used.

View File

@@ -17,14 +17,14 @@ import (
"go.opentelemetry.io/otel/metric/unit"
"go.uber.org/multierr"
"github.com/sour-is/ev"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/es"
"github.com/sour-is/ev/pkg/es/event"
"github.com/sour-is/ev/pkg/gql"
)
type service struct {
es *es.EventStore
es *ev.EventStore
m_gql_posts syncint64.Counter
m_gql_post_added syncint64.Counter
@@ -38,7 +38,7 @@ type MsgbusResolver interface {
IsResolver()
}
func New(ctx context.Context, es *es.EventStore) (*service, error) {
func New(ctx context.Context, es *ev.EventStore) (*service, error) {
ctx, span := lg.Span(ctx)
defer span.End()
@@ -243,7 +243,7 @@ func (s *service) get(w http.ResponseWriter, r *http.Request) {
first = lis[0]
}
var pos, count int64 = 0, es.AllEvents
var pos, count int64 = 0, ev.AllEvents
qry := r.URL.Query()
if i, err := strconv.ParseInt(qry.Get("index"), 10, 64); err == nil && i > 1 {

View File

@@ -19,8 +19,8 @@ import (
contentnegotiation "gitlab.com/jamietanna/content-negotiation-go"
"go.opentelemetry.io/otel/attribute"
"github.com/sour-is/ev"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/es"
"github.com/sour-is/ev/pkg/es/event"
)
@@ -150,7 +150,7 @@ func (s *service) getPending(w http.ResponseWriter, r *http.Request, peerID stri
return
}
info, err := es.Upsert(ctx, s.es, aggInfo, func(ctx context.Context, agg *Info) error {
info, err := ev.Upsert(ctx, s.es, aggInfo, func(ctx context.Context, agg *Info) error {
return agg.OnUpsert() // initialize if not exists
})
if err != nil {

View File

@@ -9,8 +9,8 @@ import (
"net/http"
"time"
"github.com/sour-is/ev"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/es"
"github.com/sour-is/ev/pkg/es/event"
"github.com/sour-is/ev/pkg/set"
)
@@ -151,7 +151,7 @@ func (s *service) cleanRequests(ctx context.Context, now time.Time) error {
for {
events, err := s.es.Read(ctx, queueRequests, startPosition, 1000) // read 1000 from the top each loop.
if err != nil && !errors.Is(err, es.ErrNotFound) {
if err != nil && !errors.Is(err, ev.ErrNotFound) {
span.RecordError(err)
return err
}

View File

@@ -6,8 +6,8 @@ import (
"sync/atomic"
"time"
"github.com/sour-is/ev"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/es"
"github.com/sour-is/ev/pkg/es/event"
"github.com/sour-is/ev/pkg/locker"
"go.uber.org/multierr"
@@ -24,7 +24,7 @@ func aggRequest(id string) string { return "pf-request-" + id }
func aggPeer(id string) string { return "pf-peer-" + id }
type service struct {
es *es.EventStore
es *ev.EventStore
statusURL string
state *locker.Locked[state]
@@ -37,7 +37,7 @@ type state struct {
requests map[string]*Request
}
func New(ctx context.Context, es *es.EventStore, statusURL string) (*service, error) {
func New(ctx context.Context, es *ev.EventStore, statusURL string) (*service, error) {
ctx, span := lg.Span(ctx)
defer span.End()

View File

@@ -19,8 +19,8 @@ import (
"go.opentelemetry.io/otel/metric/unit"
"go.uber.org/multierr"
"github.com/sour-is/ev"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/es"
"github.com/sour-is/ev/pkg/es/event"
"github.com/sour-is/ev/pkg/gql"
)
@@ -31,7 +31,7 @@ type DNSResolver interface {
type service struct {
baseURL string
es *es.EventStore
es *ev.EventStore
dns DNSResolver
m_create_user syncint64.Counter
@@ -54,7 +54,7 @@ type SaltyResolver interface {
IsResolver()
}
func New(ctx context.Context, es *es.EventStore, baseURL string) (*service, error) {
func New(ctx context.Context, es *ev.EventStore, baseURL string) (*service, error) {
ctx, span := lg.Span(ctx)
defer span.End()
@@ -111,23 +111,23 @@ func New(ctx context.Context, es *es.EventStore, baseURL string) (*service, erro
return svc, errs
}
func (s *service) IsResolver() {}
func (s *service) BaseURL() string {
if s == nil {
return "http://missing.context/"
}
return s.baseURL
}
func (s *service) RegisterHTTP(mux *http.ServeMux) {
mux.Handle("/.well-known/salty/", lg.Htrace(s, "lookup"))
}
func (s *service) RegisterHTTP(mux *http.ServeMux) {}
func (s *service) RegisterAPIv1(mux *http.ServeMux) {
mux.HandleFunc("/ping", s.apiv1)
mux.HandleFunc("/register", s.apiv1)
mux.HandleFunc("/lookup/", s.apiv1)
mux.HandleFunc("/send", s.apiv1)
}
func (s *service) RegisterWellKnown(mux *http.ServeMux) {
mux.Handle("/salty/", lg.Htrace(s, "lookup"))
}
func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx, span := lg.Span(ctx)
@@ -140,7 +140,7 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
addr = strings.TrimSuffix(addr, ".json")
span.AddEvent(fmt.Sprint("find ", addr))
a, err := es.Update(ctx, s.es, addr, func(ctx context.Context, agg *SaltyUser) error { return nil })
a, err := ev.Update(ctx, s.es, addr, func(ctx context.Context, agg *SaltyUser) error { return nil })
switch {
case errors.Is(err, event.ErrShouldExist):
span.RecordError(err)
@@ -168,6 +168,16 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
span.RecordError(err)
}
}
func (s *service) IsResolver() {}
func (s *service) GetMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(gql.ToContext(r.Context(), saltyKey, s))
next.ServeHTTP(w, r)
})
}
}
func (s *service) CreateSaltyUser(ctx context.Context, nick string, pub string) (*SaltyUser, error) {
ctx, span := lg.Span(ctx)
defer span.End()
@@ -191,11 +201,11 @@ func (s *service) createSaltyUser(ctx context.Context, streamID, pub string) (*S
return nil, err
}
a, err := es.Create(ctx, s.es, streamID, func(ctx context.Context, agg *SaltyUser) error {
a, err := ev.Create(ctx, s.es, streamID, func(ctx context.Context, agg *SaltyUser) error {
return agg.OnUserRegister(key)
})
switch {
case errors.Is(err, es.ErrShouldNotExist):
case errors.Is(err, ev.ErrShouldNotExist):
span.RecordError(err)
return nil, fmt.Errorf("user exists: %w", err)
@@ -217,9 +227,9 @@ func (s *service) SaltyUser(ctx context.Context, nick string) (*SaltyUser, error
streamID := fmt.Sprintf("saltyuser-%x", sha256.Sum256([]byte(strings.ToLower(nick))))
span.AddEvent(streamID)
a, err := es.Update(ctx, s.es, streamID, func(ctx context.Context, agg *SaltyUser) error { return nil })
a, err := ev.Update(ctx, s.es, streamID, func(ctx context.Context, agg *SaltyUser) error { return nil })
switch {
case errors.Is(err, es.ErrShouldExist):
case errors.Is(err, ev.ErrShouldExist):
span.RecordError(err)
return nil, fmt.Errorf("user not found")
@@ -230,14 +240,6 @@ func (s *service) SaltyUser(ctx context.Context, nick string) (*SaltyUser, error
return a, err
}
func (s *service) GetMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(gql.ToContext(r.Context(), saltyKey, s))
next.ServeHTTP(w, r)
})
}
}
func (s *service) apiv1(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

View File

@@ -0,0 +1 @@
package webfinger