ev/main.go

246 lines
5.3 KiB
Go
Raw Normal View History

2022-08-04 14:37:51 -06:00
package main
import (
"context"
"errors"
2022-08-04 14:37:51 -06:00
"log"
"net/http"
"net/url"
2022-08-04 14:37:51 -06:00
"os"
"os/signal"
2022-11-20 10:28:50 -07:00
"runtime/debug"
2022-08-23 21:24:13 -06:00
"strings"
2022-08-04 14:37:51 -06:00
"time"
2022-11-20 10:28:50 -07:00
"go.opentelemetry.io/otel/attribute"
"go.uber.org/multierr"
2022-08-10 21:18:57 -06:00
"golang.org/x/sync/errgroup"
2022-08-10 19:18:42 -06:00
"github.com/sour-is/ev/app/gql"
"github.com/sour-is/ev/app/msgbus"
2022-08-23 21:24:13 -06:00
"github.com/sour-is/ev/app/peerfinder"
"github.com/sour-is/ev/app/salty"
"github.com/sour-is/ev/internal/lg"
2022-11-20 10:28:50 -07:00
"github.com/sour-is/ev/pkg/cron"
2022-08-04 14:37:51 -06:00
"github.com/sour-is/ev/pkg/es"
2022-08-06 09:52:36 -06:00
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"
2022-10-30 09:18:08 -06:00
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/gql/resolver"
"github.com/sour-is/ev/pkg/set"
2022-08-04 14:37:51 -06:00
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
go func() {
<-ctx.Done()
defer cancel() // restore interrupt function
2022-08-04 14:37:51 -06:00
}()
2022-08-12 16:27:54 -06:00
// Initialize logger
2022-11-20 10:28:50 -07:00
ctx, stop := lg.Init(ctx, appName)
2022-08-12 16:27:54 -06:00
defer stop()
// Run application
if err := run(ctx); err != nil && !errors.Is(err, http.ErrServerClosed) {
2022-08-14 10:04:15 -06:00
log.Fatal(err)
2022-08-04 14:37:51 -06:00
}
}
func run(ctx context.Context) error {
g, ctx := errgroup.WithContext(ctx)
stop := &stopFns{}
2022-08-04 14:37:51 -06:00
2022-11-20 10:28:50 -07:00
cron := cron.New(cron.DefaultGranularity)
2022-08-16 16:06:25 -06:00
{
ctx, span := lg.Span(ctx)
2022-11-20 10:28:50 -07:00
log.Println(appName, version)
span.SetAttributes(
attribute.String("app", appName),
attribute.String("version", version),
)
err := multierr.Combine(
es.Init(ctx),
event.Init(ctx),
diskstore.Init(ctx),
memstore.Init(ctx),
)
if err != nil {
span.RecordError(err)
return err
}
2022-09-08 10:33:02 -06:00
es, err := es.Open(
2022-10-30 09:18:08 -06:00
ctx,
env("EV_DATA", "mem:"),
2022-11-20 10:28:50 -07:00
resolvelinks.New(),
2022-10-30 09:18:08 -06:00
streamer.New(ctx),
projecter.New(
ctx,
projecter.DefaultProjection,
),
2022-09-08 10:33:02 -06:00
)
if err != nil {
2022-08-16 16:06:25 -06:00
span.RecordError(err)
return err
}
s := http.Server{
Addr: env("EV_HTTP", ":8080"),
}
2022-08-23 21:24:13 -06:00
if strings.HasPrefix(s.Addr, ":") {
s.Addr = "[::]" + s.Addr
2022-08-16 16:06:25 -06:00
}
enable := set.New(strings.Fields(env("EV_ENABLE", "salty msgbus gql peers"))...)
2022-08-23 21:24:13 -06:00
var svcs []interface{ RegisterHTTP(*http.ServeMux) }
var res []resolver.IsResolver
2022-08-23 21:24:13 -06:00
res = append(res, es)
2022-09-04 08:34:22 -06:00
2022-08-23 21:24:13 -06:00
if enable.Has("salty") {
span.AddEvent("Enable Salty")
base, err := url.JoinPath(env("EV_BASE_URL", "http://"+s.Addr), "inbox")
if err != nil {
span.RecordError(err)
return err
}
salty, err := salty.New(ctx, es, base)
2022-08-23 21:24:13 -06:00
if err != nil {
span.RecordError(err)
return err
}
svcs = append(svcs, salty)
res = append(res, salty)
2022-08-16 16:06:25 -06:00
}
2022-08-23 21:24:13 -06:00
if enable.Has("msgbus") {
span.AddEvent("Enable Msgbus")
msgbus, err := msgbus.New(ctx, es)
if err != nil {
span.RecordError(err)
return err
}
svcs = append(svcs, msgbus)
res = append(res, msgbus)
2022-08-23 21:24:13 -06:00
}
if enable.Has("peers") {
span.AddEvent("Enable Peers")
2022-12-19 10:50:38 -07:00
es.Option(projecter.New(ctx, peerfinder.Projector))
2022-11-20 10:28:50 -07:00
peers, err := peerfinder.New(ctx, es, env("PEER_STATUS", ""))
2022-08-23 21:24:13 -06:00
if err != nil {
span.RecordError(err)
return err
}
svcs = append(svcs, peers)
2022-11-20 10:28:50 -07:00
cron.Once(ctx, peers.RefreshJob)
cron.NewJob("0,15,30,45", peers.RefreshJob)
cron.Once(ctx, peers.CleanJob)
cron.NewJob("0 1", peers.CleanJob)
g.Go(func() error {
return peers.Run(ctx)
})
stop.add(peers.Stop)
2022-08-16 16:06:25 -06:00
}
2022-08-23 21:24:13 -06:00
if enable.Has("gql") {
span.AddEvent("Enable GraphQL")
gql, err := resolver.New(ctx, &gql.Resolver{}, res...)
2022-08-23 21:24:13 -06:00
if err != nil {
span.RecordError(err)
return err
}
svcs = append(svcs, gql)
}
2022-10-30 09:18:08 -06:00
svcs = append(svcs, lg.NewHTTP(ctx), RegisterHTTP(func(mux *http.ServeMux) {
mux.Handle("/", http.RedirectHandler("/playground", http.StatusTemporaryRedirect))
}))
2022-08-23 21:24:13 -06:00
s.Handler = httpMux(svcs...)
2022-08-16 16:06:25 -06:00
log.Print("Listen on ", s.Addr)
span.AddEvent("begin listen and serve on " + s.Addr)
2022-08-16 16:06:25 -06:00
Mup, err := lg.Meter(ctx).SyncInt64().UpDownCounter("up")
if err != nil {
return err
}
Mup.Add(ctx, 1)
2022-08-16 16:06:25 -06:00
g.Go(s.ListenAndServe)
stop.add(s.Shutdown)
2022-08-16 16:06:25 -06:00
span.End()
}
g.Go(func() error {
<-ctx.Done()
// shutdown jobs
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return stop.stop(ctx)
})
g.Go(func() error {
return cron.Run(ctx)
})
2022-11-20 10:28:50 -07:00
if err := g.Wait(); err != nil && err != http.ErrServerClosed {
return err
}
return nil
2022-08-04 14:37:51 -06:00
}
2022-08-06 09:52:36 -06:00
func env(name, defaultValue string) string {
2022-08-23 21:24:13 -06:00
name = strings.TrimSpace(name)
defaultValue = strings.TrimSpace(defaultValue)
if v := strings.TrimSpace(os.Getenv(name)); v != "" {
log.Println("#", name, "=", v)
2022-08-06 09:52:36 -06:00
return v
}
2022-08-23 21:24:13 -06:00
log.Println("#", name, "=", defaultValue, "(default)")
2022-08-06 09:52:36 -06:00
return defaultValue
}
2022-11-20 10:28:50 -07:00
var appName, version = func() (string, string) {
if info, ok := debug.ReadBuildInfo(); ok {
_, name, _ := strings.Cut(info.Main.Path, "/")
name = strings.Replace(name, "-", ".", -1)
name = strings.Replace(name, "/", "-", -1)
return name, info.Main.Version
}
return "sour.is-ev", "(devel)"
}()
2022-12-19 10:50:38 -07:00
type application interface {
Setup(context.Context) error
}
type stopFns struct {
fns []func(context.Context) error
}
func (s *stopFns) add(fn func(context.Context) error) {
s.fns = append(s.fns, fn)
}
func (s *stopFns) stop(ctx context.Context) error {
g, _ := errgroup.WithContext(ctx)
for i := range s.fns {
fn := s.fns[i]
g.Go(func() error {
return fn(ctx)
})
}
return g.Wait()
}