-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
150 lines (127 loc) · 3.25 KB
/
server.go
File metadata and controls
150 lines (127 loc) · 3.25 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/json"
"github.com/boltdb/bolt"
"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
"github.com/hashicorp/raft"
"io/ioutil"
"log"
"net/http"
"time"
)
//TODO: Encrypt
//Global Server
var localServer *KVRaftServer
//Global Logger
var ServerLogger *log.Logger
// Local Cache
var ShCache *ShareCache
type ShareCache struct {
LeaderRpcAddr string
LeaderRpcPort string
LeaderHTTPAddr string
LeaderHTTPPort string
}
func InitShareCache() {
ShCache = &ShareCache{}
}
type KVRaftServer struct {
Router *gin.Engine
Db StorageDB
Log *log.Logger
FSM *RaftFSM
Raft *raft.Raft
Trans raft.Transport
Config *ServerConfig
PeerStorage raft.PeerStore
}
func (s *KVRaftServer) ShareServerConfig() {
for {
time.Sleep(time.Second * 3)
//Get Leader, if leader is self , notify others
if s.Config.RaftAddrString() == s.Raft.Leader() {
// update leader address info
opReq := OpRequest{
Op: CmdShare,
Key: "",
Value: []byte(s.Config.RpcAddrString()),
}
ShCache.LeaderRpcAddr, ShCache.LeaderRpcPort = s.Config.RpcAddr, s.Config.RpcPort
opReq.Value, _ = json.Marshal(ShCache)
cmd, _ := json.Marshal(opReq)
err := s.Raft.Apply(cmd, 3)
if err != nil && err.Error() != nil {
ServerLogger.Println("Share config apply error: ", err)
continue
}
time.Sleep(time.Second * 5)
}
s.Config.LeaderAddr, s.Config.LeaderPort = ShCache.LeaderRpcAddr, ShCache.LeaderRpcPort
}
}
func SetHandler(ctx *gin.Context) {
bucketName := ctx.Param("bucket")
key := ctx.Param("key")
value, err := ioutil.ReadAll(ctx.Request.Body)
if bucketName == "" || key == "" {
ctx.JSON(http.StatusBadRequest, "lack of bucket or key")
return
}
if err != nil || value == nil || len(value) == 0 {
ctx.JSON(http.StatusBadRequest, "lack of data binary")
return
}
rpcCmd := RpcCmd{
Op: CmdSet,
Key: []byte(key),
Bucket: []byte(bucketName),
Value: value,
}
err = rpcCmd.DoSet()
if err != nil {
if err == bolt.ErrBucketNotFound {
ctx.JSON(http.StatusNotFound, "no target bucket:"+bucketName)
} else {
ctx.JSON(http.StatusInternalServerError, "error in set value:"+err.Error())
}
return
}
ctx.JSON(http.StatusOK, "set value success")
}
func GetHandler(ctx *gin.Context) {
bucketName := ctx.Param("bucket")
key := ctx.Param("key")
rpcCmd := RpcCmd{
Op: CmdGet,
Key: []byte(key),
Bucket: []byte(bucketName),
}
if bucketName == "" || key == "" {
ctx.JSON(http.StatusBadRequest, "lack of bucket or key")
return
}
value, err := rpcCmd.DoGet()
if err != nil {
if err == bolt.ErrBucketNotFound {
ctx.JSON(http.StatusNotFound, "no target bucket:"+bucketName)
} else {
ctx.JSON(http.StatusInternalServerError, "error in get value:"+err.Error())
}
return
}
if value == nil {
ctx.JSON(http.StatusNotFound, "no target value, key is:"+key)
return
}
ctx.JSON(http.StatusOK, string(value))
}
func (server *KVRaftServer) Start() error {
ServerLogger = server.Log
localServer = server
server.Router.PUT("/api/:bucket/:key", SetHandler)
server.Router.GET("/api/:bucket/:key", GetHandler)
//server.Router.DELETE("/api/:bucket/:key", GetHandler)
endless.ListenAndServe(":"+server.Config.ServerPort, server.Router)
return nil
}