2022-08-04 14:37:51 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-10 21:18:57 -06:00
|
|
|
"fmt"
|
2022-08-04 14:37:51 -06:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
|
|
|
|
2022-08-07 11:55:49 -06:00
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
2022-08-12 15:53:16 -06:00
|
|
|
"github.com/ravilushqa/otelgqlgen"
|
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-07 11:55:49 -06:00
|
|
|
"github.com/sour-is/ev/api/gql_ev"
|
|
|
|
"github.com/sour-is/ev/internal/graph"
|
|
|
|
"github.com/sour-is/ev/internal/graph/generated"
|
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"
|
|
|
|
"github.com/sour-is/ev/pkg/msgbus"
|
2022-08-07 11:55:49 -06:00
|
|
|
"github.com/sour-is/ev/pkg/playground"
|
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-13 13:34:13 -06:00
|
|
|
Mup, err := logz.Meter(ctx).SyncInt64().UpDownCounter("up")
|
2022-08-12 15:53:16 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-08-13 13:34:13 -06:00
|
|
|
Mup.Add(ctx, 1)
|
2022-08-04 14:37:51 -06:00
|
|
|
|
|
|
|
if err := run(ctx); err != nil {
|
2022-08-12 16:27:54 -06:00
|
|
|
log.Println(err)
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func run(ctx context.Context) error {
|
2022-08-04 21:07:10 -06:00
|
|
|
diskstore.Init(ctx)
|
2022-08-06 09:52:36 -06:00
|
|
|
memstore.Init(ctx)
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
es, err := es.Open(ctx, env("EV_DATA", "file:data"), streamer.New(ctx))
|
2022-08-04 14:37:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
svc, err := msgbus.New(ctx, es)
|
2022-08-07 11:55:49 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:34:13 -06:00
|
|
|
r, err := gql_ev.New(es)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res := graph.New(r)
|
2022-08-07 11:55:49 -06:00
|
|
|
gql := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: res}))
|
2022-08-12 15:53:16 -06:00
|
|
|
gql.Use(otelgqlgen.Middleware())
|
2022-08-07 11:55:49 -06:00
|
|
|
|
2022-08-04 14:37:51 -06:00
|
|
|
s := http.Server{
|
2022-08-06 09:52:36 -06:00
|
|
|
Addr: env("EV_HTTP", ":8080"),
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
2022-08-10 19:18:42 -06:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
mux.Handle("/", playground.Handler("GraphQL playground", "/gql"))
|
2022-08-13 13:34:13 -06:00
|
|
|
mux.Handle("/gql", logz.Htrace(res.ChainMiddlewares(gql), "gql"))
|
|
|
|
mux.Handle("/inbox/", logz.Htrace(http.StripPrefix("/inbox/", svc), "inbox"))
|
|
|
|
mux.Handle("/metrics", logz.PromHTTP(ctx))
|
2022-08-10 19:18:42 -06:00
|
|
|
|
|
|
|
wk := http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, `{
|
|
|
|
"endpoint": "https://ev.sour.is/inbox/01GA4Q3NDX4TPAZ2EZ8E92CQE6",
|
|
|
|
"key": "kex1pqwqatj6sge7qaqrsvk4u4yhue4x3vej8znetkwj6a5k0xds2fmqqe3plh"
|
|
|
|
}`)
|
2022-08-10 21:18:57 -06:00
|
|
|
},
|
2022-08-10 19:18:42 -06:00
|
|
|
)
|
|
|
|
mux.Handle("/.well-known/salty/0ce550020ce36a9932b286b141edd515d33c2b0f51c715445de89ae106345993.json", wk)
|
|
|
|
|
|
|
|
s.Handler = cors.AllowAll().Handler(mux)
|
2022-08-04 14:37:51 -06:00
|
|
|
|
|
|
|
log.Print("Listen on ", s.Addr)
|
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
return g.Wait()
|
|
|
|
}
|
2022-08-06 09:52:36 -06:00
|
|
|
func env(name, defaultValue string) string {
|
|
|
|
if v := os.Getenv(name); v != "" {
|
|
|
|
log.Println("# ", name, " = ", v)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|