2022-08-04 14:37:51 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-23 21:24:13 -06:00
|
|
|
"bytes"
|
2022-08-04 14:37:51 -06:00
|
|
|
"context"
|
2022-08-23 21:24:13 -06:00
|
|
|
"fmt"
|
2022-08-04 14:37:51 -06:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2022-08-19 12:26:42 -06:00
|
|
|
"path"
|
2022-08-23 21:24:13 -06:00
|
|
|
"strings"
|
2022-08-04 14:37:51 -06:00
|
|
|
"time"
|
|
|
|
|
2022-08-10 19:18:42 -06:00
|
|
|
"github.com/rs/cors"
|
2022-08-10 21:18:57 -06:00
|
|
|
"golang.org/x/sync/errgroup"
|
2022-08-10 19:18:42 -06:00
|
|
|
|
2022-08-19 13:57:07 -06:00
|
|
|
"github.com/sour-is/ev/app/gql"
|
2022-08-19 12:26:42 -06:00
|
|
|
"github.com/sour-is/ev/app/msgbus"
|
2022-08-23 21:24:13 -06:00
|
|
|
"github.com/sour-is/ev/app/peerfinder"
|
2022-08-19 12:26:42 -06:00
|
|
|
"github.com/sour-is/ev/app/salty"
|
2022-08-13 13:34:13 -06:00
|
|
|
"github.com/sour-is/ev/internal/logz"
|
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"
|
2022-08-09 16:23:33 -06:00
|
|
|
"github.com/sour-is/ev/pkg/es/driver/streamer"
|
2022-08-04 14:37:51 -06:00
|
|
|
)
|
|
|
|
|
2022-08-13 13:34:13 -06:00
|
|
|
const AppName string = "sour.is-ev"
|
2022-08-12 15:53:16 -06:00
|
|
|
|
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()
|
|
|
|
}()
|
2022-08-12 16:27:54 -06:00
|
|
|
|
2022-08-13 13:34:13 -06:00
|
|
|
ctx, stop := logz.Init(ctx, AppName)
|
2022-08-12 16:27:54 -06:00
|
|
|
defer stop()
|
|
|
|
|
2022-08-14 10:04:15 -06:00
|
|
|
if err := run(ctx); err != nil && err != http.ErrServerClosed {
|
|
|
|
log.Fatal(err)
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func run(ctx context.Context) error {
|
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
2022-08-16 16:06:25 -06:00
|
|
|
{
|
|
|
|
ctx, span := logz.Span(ctx)
|
|
|
|
|
|
|
|
diskstore.Init(ctx)
|
|
|
|
memstore.Init(ctx)
|
2022-08-19 12:26:42 -06:00
|
|
|
|
|
|
|
es, err := es.Open(ctx, env("EV_DATA", "file:data"), streamer.New(ctx))
|
|
|
|
if err != nil {
|
2022-08-16 16:06:25 -06:00
|
|
|
span.RecordError(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-19 12:26:42 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-23 21:24:13 -06:00
|
|
|
enable := set(strings.Fields(env("EV_ENABLE", "salty msgbus gql peers"))...)
|
|
|
|
var svcs []interface{ RegisterHTTP(*http.ServeMux) }
|
|
|
|
|
|
|
|
if enable.Has("salty") {
|
|
|
|
span.AddEvent("Enable Salty")
|
|
|
|
salty, err := salty.New(ctx, es, path.Join(env("EV_BASE_URL", "http://"+s.Addr), "inbox"))
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
svcs = append(svcs, 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if enable.Has("peers") {
|
|
|
|
span.AddEvent("Enable Peers")
|
|
|
|
peers, err := peerfinder.New(ctx, es)
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
svcs = append(svcs, peers)
|
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 := gql.New(ctx, svcs...)
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
svcs = append(svcs, gql)
|
|
|
|
}
|
|
|
|
svcs = append(svcs, logz.NewHTTP(ctx))
|
|
|
|
|
|
|
|
s.Handler = httpMux(svcs...)
|
2022-08-16 16:06:25 -06:00
|
|
|
|
|
|
|
log.Print("Listen on ", s.Addr)
|
|
|
|
span.AddEvent("begin listen and serve")
|
|
|
|
|
2022-08-19 13:57:07 -06:00
|
|
|
Mup, err := logz.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)
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
<-ctx.Done()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
return s.Shutdown(ctx)
|
|
|
|
})
|
|
|
|
|
|
|
|
span.End()
|
|
|
|
}
|
2022-08-19 13:57:07 -06: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-08-19 13:57:07 -06:00
|
|
|
func httpMux(fns ...interface{ RegisterHTTP(*http.ServeMux) }) http.Handler {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
for _, fn := range fns {
|
|
|
|
fn.RegisterHTTP(mux)
|
|
|
|
}
|
|
|
|
return cors.AllowAll().Handler(mux)
|
|
|
|
}
|
2022-08-23 21:24:13 -06:00
|
|
|
|
|
|
|
type Set[T comparable] map[T]struct{}
|
|
|
|
|
|
|
|
func set[T comparable](items ...T) Set[T] {
|
|
|
|
s := make(map[T]struct{}, len(items))
|
|
|
|
for i := range items {
|
|
|
|
s[items[i]] = struct{}{}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
func (s Set[T]) Has(v T) bool {
|
|
|
|
_, ok := (s)[v]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (s Set[T]) String() string {
|
|
|
|
if s == nil {
|
|
|
|
return "set(<nil>)"
|
|
|
|
}
|
|
|
|
lis := make([]string, 0, len(s))
|
|
|
|
for k := range s {
|
|
|
|
lis = append(lis, fmt.Sprint(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("set(")
|
|
|
|
b.WriteString(strings.Join(lis, ","))
|
|
|
|
b.WriteString(")")
|
|
|
|
return b.String()
|
|
|
|
}
|