-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
46 lines (39 loc) · 1.34 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
package main
import (
"flag"
"github.com/arpith/mmapd/db"
"github.com/arpith/mmapd/raft"
"github.com/julienschmidt/httprouter"
"net/http"
"os"
"strings"
)
func main() {
dbFilenamePtr := flag.String("db", "db.json", "database filename")
logFilenamePtr := flag.String("log", "log.json", "log filename")
configFilenamePtr := flag.String("config", "config.json", "config file name")
portPtr := flag.String("port", "3001", "port to listen on")
ipPtr := flag.String("ip", "localhost", "ip that the server is running on")
flag.Parse()
port := *portPtr
if port == "3001" {
envPort := strings.TrimSpace(os.Getenv("PORT"))
if envPort != "" {
port = envPort
}
}
id := *ipPtr + ":" + port
DB := db.Init(*dbFilenamePtr, *logFilenamePtr)
server := raft.Init(id, *configFilenamePtr, DB)
appendEntryHandler := raft.NewHandler(server, "Append Entry")
requestForVoteHandler := raft.NewHandler(server, "Request For Vote")
clientRequestHandler := raft.NewHandler(server, "Client Request")
statusRequestHandler := raft.NewHandler(server, "Status Request")
router := httprouter.New()
router.POST("/append", appendEntryHandler)
router.POST("/votes", requestForVoteHandler)
router.GET("/get/:key", clientRequestHandler)
router.POST("/set/:key", clientRequestHandler)
router.GET("/status", statusRequestHandler)
http.ListenAndServe(":"+port, router)
}