-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
43 lines (36 loc) · 987 Bytes
/
Copy pathmain.go
File metadata and controls
43 lines (36 loc) · 987 Bytes
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
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
// load config
config, err := LoadConfig("config.yaml")
if err != nil {
log.Fatalf("failed to load config: %v", err)
}
// get this node's own address from environment
id := os.Getenv("NODE_ID")
if id == "" {
log.Fatal("NODE_ID environment variable is required")
}
// boot the node
node := NewNode(id, config)
// register HTTP endpoints
http.HandleFunc("/append-entries", node.handleAppendEntries)
http.HandleFunc("/request-vote", node.handleRequestVote)
http.HandleFunc("/write", node.handleWrite)
http.HandleFunc("/read", node.handleRead)
// start background goroutines
go node.runElectionTimeout()
listenAddr := os.Getenv("LISTEN_ADDR")
if listenAddr == "" {
log.Fatal("LISTEN_ADDR environment variable is required")
}
fmt.Printf("node %s listening on %s\n", id, listenAddr)
if err := http.ListenAndServe(listenAddr, nil); err != nil {
log.Fatalf("server failed: %v", err)
}
}