1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
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)
}
}
|