-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
72 lines (57 loc) · 1.87 KB
/
main.go
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
69
70
71
72
package main
import (
"flag"
"log"
"net/http"
"github.com/YuriyNasretdinov/distribkv/config"
"github.com/YuriyNasretdinov/distribkv/db"
"github.com/YuriyNasretdinov/distribkv/replication"
"github.com/YuriyNasretdinov/distribkv/web"
)
var (
dbLocation = flag.String("db-location", "", "The path to the bolt db database")
httpAddr = flag.String("http-addr", "127.0.0.1:8080", "HTTP host and port")
configFile = flag.String("config-file", "sharding.toml", "Config file for static sharding")
shard = flag.String("shard", "", "The name of the shard for the data")
replica = flag.Bool("replica", false, "Whether or not run as a read-only replica")
)
func parseFlags() {
flag.Parse()
if *dbLocation == "" {
log.Fatalf("Must provide db-location")
}
if *shard == "" {
log.Fatalf("Must provide shard")
}
}
func main() {
parseFlags()
c, err := config.ParseFile(*configFile)
if err != nil {
log.Fatalf("Error parsing config %q: %v", *configFile, err)
}
shards, err := config.ParseShards(c.Shards, *shard)
if err != nil {
log.Fatalf("Error parsing shards config: %v", err)
}
log.Printf("Shard count is %d, current shard: %d", shards.Count, shards.CurIdx)
db, close, err := db.NewDatabase(*dbLocation, *replica)
if err != nil {
log.Fatalf("Error creating %q: %v", *dbLocation, err)
}
defer close()
if *replica {
leaderAddr, ok := shards.Addrs[shards.CurIdx]
if !ok {
log.Fatalf("Could not find address for leader for shard %d", shards.CurIdx)
}
go replication.ClientLoop(db, leaderAddr)
}
srv := web.NewServer(db, shards)
http.HandleFunc("/get", srv.GetHandler)
http.HandleFunc("/set", srv.SetHandler)
http.HandleFunc("/purge", srv.DeleteExtraKeysHandler)
http.HandleFunc("/next-replication-key", srv.GetNextKeyForReplication)
http.HandleFunc("/delete-replication-key", srv.DeleteReplicationKey)
log.Fatal(http.ListenAndServe(*httpAddr, nil))
}