2022-08-04 14:37:51 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2022-08-07 11:55:49 -06:00
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
|
|
|
"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-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-07 11:55:49 -06:00
|
|
|
"github.com/sour-is/ev/pkg/es/service"
|
|
|
|
"github.com/sour-is/ev/pkg/playground"
|
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()
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := run(ctx); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-06 09:52:36 -06:00
|
|
|
es, err := es.Open(ctx, env("EV_DATA", "file:data"))
|
2022-08-04 14:37:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-07 11:55:49 -06:00
|
|
|
svc, err := service.New(ctx, es)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
|
|
|
|
2022-08-07 11:55:49 -06:00
|
|
|
res := graph.New(gql_ev.New(es))
|
|
|
|
gql := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: res}))
|
|
|
|
|
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-07 11:55:49 -06:00
|
|
|
http.Handle("/", playground.Handler("GraphQL playground", "/gql"))
|
|
|
|
http.Handle("/gql", res.ChainMiddlewares(gql))
|
|
|
|
http.Handle("/event/", http.StripPrefix("/event/", svc))
|
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
|
|
|
|
}
|