package main import ( "context" "flag" "log" "net/http" "os" "os/signal" "go.neonxp.dev/djson/internal/api" "go.neonxp.dev/djson/internal/storage" "go.neonxp.dev/djson/internal/tree" "golang.org/x/sync/errgroup" ) var ( cluster = flag.String("cluster", "", "Master node address (ex: 192.168.1.10:5657)") clusterAddr = flag.String("cluster_addr", ":5657", "Self address for cluster") apiAddr = flag.String("api_addr", ":5656", "API address") ) func main() { flag.Parse() ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) defer cancel() if cluster != nil { log.Printf("Connecting to cluster %s", *cluster) } else { log.Printf("Creating cluster at %s", *clusterAddr) } storage, err := storage.NewFileStorage("test.wal") if err != nil { log.Fatal(err) } service := tree.New( storage, ) server := http.Server{ Addr: *apiAddr, Handler: api.NewHandler(service), } eg, ctx := errgroup.WithContext(ctx) eg.Go(func() error { return service.Run(ctx) }) eg.Go(func() error { log.Printf("Server started at %s", *apiAddr) if err := server.ListenAndServe(); err != http.ErrServerClosed { return err } return nil }) eg.Go(func() error { <-ctx.Done() return server.Close() }) if err := eg.Wait(); err != nil { log.Fatal(err) } }