forked from rand0x0m/chord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
48 lines (35 loc) · 816 Bytes
/
server.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
package chord
import (
"context"
"net"
"net/http"
"net/rpc"
)
type Server struct {
httpServer *http.Server
}
func newServer(bindAddr net.TCPAddr, n Node) (*Server, error) {
RPCserver := rpc.NewServer()
err := RPCserver.Register(&n)
if err != nil {
return nil, err
}
oldMux := http.DefaultServeMux
mux := http.NewServeMux()
http.DefaultServeMux = mux
RPCserver.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
http.DefaultServeMux = oldMux
listener, err := net.Listen(bindAddr.Network(), bindAddr.String())
if err != nil {
return nil, err
}
server := new(Server)
server.httpServer = new(http.Server)
server.httpServer.Handler = mux
go server.httpServer.Serve(listener)
go func() {
<-n.Shutdown
server.httpServer.Shutdown(context.Background())
}()
return server, nil
}