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
cmd/README.md Normal file
View File

@@ -0,0 +1,3 @@
# Cmd
These are examples that can be built using EV. Because they are modular the apps can be mixed an matched by including the different source files linked from `cmd/ev`.

32
cmd/ev/app.msgbus.go Normal file
View File

@@ -0,0 +1,32 @@
package main
import (
"context"
"fmt"
"github.com/sour-is/ev"
"github.com/sour-is/ev/app/msgbus"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/service"
"github.com/sour-is/ev/pkg/slice"
)
var _ = apps.Register(50, func(ctx context.Context, svc *service.Harness) error {
ctx, span := lg.Span(ctx)
defer span.End()
span.AddEvent("Enable Msgbus")
eventstore, ok := slice.Find[*ev.EventStore](svc.Services...)
if !ok {
return fmt.Errorf("*es.EventStore not found in services")
}
msgbus, err := msgbus.New(ctx, eventstore)
if err != nil {
span.RecordError(err)
return err
}
svc.Add(msgbus)
return nil
})

43
cmd/ev/app.peerfinder.go Normal file
View File

@@ -0,0 +1,43 @@
package main
import (
"context"
"fmt"
"github.com/sour-is/ev"
"github.com/sour-is/ev/app/peerfinder"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/env"
"github.com/sour-is/ev/pkg/es/driver/projecter"
"github.com/sour-is/ev/pkg/service"
"github.com/sour-is/ev/pkg/slice"
)
var _ = apps.Register(50, func(ctx context.Context, svc *service.Harness) error {
ctx, span := lg.Span(ctx)
defer span.End()
span.AddEvent("Enable Peers")
eventstore, ok := slice.Find[*ev.EventStore](svc.Services...)
if !ok {
return fmt.Errorf("*es.EventStore not found in services")
}
eventstore.Option(projecter.New(ctx, peerfinder.Projector))
peers, err := peerfinder.New(ctx, eventstore, env.Secret("PEER_STATUS", "").Secret())
if err != nil {
span.RecordError(err)
return err
}
svc.RunOnce(ctx, peers.RefreshJob)
svc.NewCron("0,15,30,45", peers.RefreshJob)
svc.RunOnce(ctx, peers.CleanJob)
svc.NewCron("0 1", peers.CleanJob)
svc.OnStart(peers.Run)
svc.OnStop(peers.Stop)
svc.Add(peers)
return nil
})

46
cmd/ev/app.salty.go Normal file
View File

@@ -0,0 +1,46 @@
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/sour-is/ev"
"github.com/sour-is/ev/app/salty"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/env"
"github.com/sour-is/ev/pkg/service"
"github.com/sour-is/ev/pkg/slice"
)
var _ = apps.Register(50, func(ctx context.Context, svc *service.Harness) error {
ctx, span := lg.Span(ctx)
defer span.End()
span.AddEvent("Enable Salty")
eventstore, ok := slice.Find[*ev.EventStore](svc.Services...)
if !ok {
return fmt.Errorf("*es.EventStore not found in services")
}
addr := "localhost"
if ht, ok := slice.Find[*http.Server](svc.Services...); ok {
addr = ht.Addr
}
base, err := url.JoinPath(env.Default("SALTY_BASE_URL", "http://"+addr), "inbox")
if err != nil {
span.RecordError(err)
return err
}
salty, err := salty.New(ctx, eventstore, base)
if err != nil {
span.RecordError(err)
return err
}
svc.Add(salty)
return nil
})

37
cmd/ev/main.go Normal file
View File

@@ -0,0 +1,37 @@
package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"os/signal"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/service"
)
var apps service.Apps
var appName, version = service.AppName()
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
go func() {
<-ctx.Done()
defer cancel() // restore interrupt function
}()
svc := &service.Harness{}
ctx, stop := lg.Init(ctx, appName)
svc.OnStop(stop)
svc.Add(lg.NewHTTP(ctx))
svc.Setup(ctx, apps.Apps()...)
// Run application
if err := svc.Run(ctx, appName, version); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}

54
cmd/ev/svc.es.go Normal file
View File

@@ -0,0 +1,54 @@
package main
import (
"context"
"github.com/sour-is/ev"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/env"
"github.com/sour-is/ev/pkg/es"
diskstore "github.com/sour-is/ev/pkg/es/driver/disk-store"
memstore "github.com/sour-is/ev/pkg/es/driver/mem-store"
"github.com/sour-is/ev/pkg/es/driver/projecter"
resolvelinks "github.com/sour-is/ev/pkg/es/driver/resolve-links"
"github.com/sour-is/ev/pkg/es/driver/streamer"
"github.com/sour-is/ev/pkg/es/event"
"github.com/sour-is/ev/pkg/service"
"go.uber.org/multierr"
)
var _ = apps.Register(10, func(ctx context.Context, svc *service.Harness) error {
ctx, span := lg.Span(ctx)
defer span.End()
// setup eventstore
err := multierr.Combine(
ev.Init(ctx),
event.Init(ctx),
diskstore.Init(ctx),
memstore.Init(ctx),
)
if err != nil {
span.RecordError(err)
return err
}
eventstore, err := ev.Open(
ctx,
env.Default("EV_DATA", "mem:"),
resolvelinks.New(),
streamer.New(ctx),
projecter.New(
ctx,
projecter.DefaultProjection,
),
)
if err != nil {
span.RecordError(err)
return err
}
svc.Add(eventstore)
svc.Add(&es.EventStore{EventStore: eventstore})
return nil
})

30
cmd/ev/svc.gql.go Normal file
View File

@@ -0,0 +1,30 @@
package main
import (
"context"
"net/http"
"github.com/sour-is/ev/app/gql"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/gql/resolver"
"github.com/sour-is/ev/pkg/mux"
"github.com/sour-is/ev/pkg/service"
"github.com/sour-is/ev/pkg/slice"
)
var _ = apps.Register(90, func(ctx context.Context, svc *service.Harness) error {
ctx, span := lg.Span(ctx)
defer span.End()
span.AddEvent("Enable GraphQL")
gql, err := resolver.New(ctx, &gql.Resolver{}, slice.FilterType[resolver.IsResolver](svc.Services...)...)
if err != nil {
span.RecordError(err)
return err
}
svc.Add(gql, mux.RegisterHTTP(func(mux *http.ServeMux) {
mux.Handle("/", http.RedirectHandler("/playground", http.StatusTemporaryRedirect))
}))
return nil
})

41
cmd/ev/svc.http.go Normal file
View File

@@ -0,0 +1,41 @@
package main
import (
"context"
"log"
"net/http"
"strings"
"github.com/rs/cors"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/env"
"github.com/sour-is/ev/pkg/mux"
"github.com/sour-is/ev/pkg/service"
"github.com/sour-is/ev/pkg/slice"
)
var _ = apps.Register(20, func(ctx context.Context, svc *service.Harness) error {
s := &http.Server{}
svc.Add(s)
mux := mux.New()
s.Handler = cors.AllowAll().Handler(mux)
s.Addr = env.Default("EV_HTTP", ":8080")
if strings.HasPrefix(s.Addr, ":") {
s.Addr = "[::]" + s.Addr
}
svc.OnStart(func(ctx context.Context) error {
_, span := lg.Span(ctx)
defer span.End()
log.Print("Listen on ", s.Addr)
span.AddEvent("begin listen and serve on " + s.Addr)
mux.Add(slice.FilterType[interface{ RegisterHTTP(*http.ServeMux) }](svc.Services...)...)
return s.ListenAndServe()
})
svc.OnStop(s.Shutdown)
return nil
})

1
cmd/msgbus/app.msgbus.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/app.msgbus.go

1
cmd/msgbus/main.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/main.go

1
cmd/msgbus/svc.es.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/svc.es.go

1
cmd/msgbus/svc.gql.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/svc.gql.go

1
cmd/msgbus/svc.http.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/svc.http.go

1
cmd/peers/app.peerfinder.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/app.peerfinder.go

1
cmd/peers/main.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/main.go

1
cmd/peers/svc.es.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/svc.es.go

1
cmd/peers/svc.http.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/svc.http.go

1
cmd/salty/app.msgbus.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/app.msgbus.go

1
cmd/salty/app.salty.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/app.salty.go

1
cmd/salty/main.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/main.go

1
cmd/salty/svc.es.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/svc.es.go

1
cmd/salty/svc.http.go Symbolic link
View File

@@ -0,0 +1 @@
../ev/svc.http.go