-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (47 loc) · 1.35 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
package main
import (
"flag"
"log"
"net/http"
"github.com/shubham14bajpai/dist-kv/config"
"github.com/shubham14bajpai/dist-kv/db"
"github.com/shubham14bajpai/dist-kv/web"
)
var (
dbLocation = flag.String("db-location", "", "path for bolt db location")
httpAddr = flag.String("http-addr", ":8080", "http host and port")
configFile = flag.String("config-file", "sharding.toml", "config file for static sharding")
shard = flag.String("shard", "", "the name for the shard for the data")
)
func parseFlags() {
flag.Parse()
if *dbLocation == "" {
log.Fatal("must provide db location")
}
if *shard == "" {
log.Fatal("must provide shard name")
}
}
func main() {
parseFlags()
c, err := config.ParseFile(*configFile)
if err != nil {
log.Fatalf("failed to parse config file: %v", err)
}
shards, err := config.ParseShards(c.Shards, *shard)
if err != nil {
log.Fatalf("failed to parse shards: %v", err)
}
log.Printf("Shard count: %d and shard index: %d",
shards.Count, shards.CurrIdx)
db, close, err := db.NewDatabase(*dbLocation)
if err != nil {
log.Fatalf("failed to initialize db: %v", err)
}
defer close()
srv := web.NewServer(db, shards)
http.HandleFunc("/get", srv.GetHandler)
http.HandleFunc("/set", srv.SetHandler)
http.HandleFunc("/purge", srv.DeleteExtraKeysHandler)
log.Fatal(http.ListenAndServe(*httpAddr, nil))
}