-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
77 lines (71 loc) · 1.9 KB
/
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
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
package main
import (
"flag"
"io"
"log"
"net"
pb "github.com/cpjudge/proto/leaderboard"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/testdata"
)
var (
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
certFile = flag.String("cert_file", "", "The TLS cert file")
keyFile = flag.String("key_file", "", "The TLS key file")
jsonDBFile = flag.String("json_db_file", "", "A json file containing a list of features")
serverAddr = flag.String("server_addr", "172.17.0.1:11000", "The server address in the format of host:port")
)
type leaderboardServer struct{}
func (s leaderboardServer) GetLeaderboard(srv pb.Leaderboard_GetLeaderboardServer) error {
ctx := srv.Context()
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
contest, err := srv.Recv()
if err == io.EOF {
// return will close stream from server side
log.Println("exit")
return nil
}
if err != nil {
log.Printf("receive error %v", err)
continue
}
if contest != nil {
log.Println("Server received", contest.String())
participants := processLeaderboard(contest.ContestId)
if err := srv.Send(participants); err != nil {
log.Printf("send error %v", err)
}
}
}
}
func main() {
flag.Parse()
connectToDatabase()
lis, err := net.Listen("tcp", *serverAddr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
var opts []grpc.ServerOption
if *tls {
if *certFile == "" {
*certFile = testdata.Path("server1.pem")
}
if *keyFile == "" {
*keyFile = testdata.Path("server1.key")
}
creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
if err != nil {
log.Fatalf("Failed to generate credentials %v", err)
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
}
grpcServer := grpc.NewServer(opts...)
pb.RegisterLeaderboardServer(grpcServer, &leaderboardServer{})
grpcServer.Serve(lis)
}