-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (82 loc) · 2.21 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
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
package main
import (
"errors"
"flag"
"fmt"
"keto/kfx/pb"
"keto/kfx/route"
"log"
"net"
"github.com/gogo/protobuf/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
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", "testdata/route_guide_db.json", "A json file containing a list of features")
port = flag.Int("port", 10000, "The server port")
)
type Router interface {
Route(stream pb.KfxService_DoWorksServer) error
}
type kfxServer struct {
routers map[string]Router
}
type AccountRouter struct {
authed bool
}
func (server *kfxServer) DoWorks(stream pb.KfxService_DoWorksServer) error {
return route.HandleStream(stream)
}
func (server *kfxServer) Route(stream pb.KfxService_DoWorksServer) error {
for {
req, err := stream.Recv()
if err != nil {
return err
}
switch pb.Codes(req.Code) {
case pb.Codes_ConfigService:
body := pb.ConfigServiceRequest{}
if err := proto.Unmarshal(req.Payload, &body); err != nil {
return errors.New("bad payload")
}
return server.configService(stream, &body)
}
break
}
return nil
}
func (server *kfxServer) configService(stream pb.KfxService_DoWorksServer, cfg *pb.ConfigServiceRequest) error {
m := route.Mt4Route{}
return m.Route(stream)
}
func newKfxServer() *kfxServer {
s := new(kfxServer)
return s
}
func main() {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
var opts []grpc.ServerOption
if *tls {
if *certFile == "" {
*certFile = "server1.pem"
}
if *keyFile == "" {
*keyFile = "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.RegisterKfxServiceServer(grpcServer, newKfxServer())
grpcServer.Serve(lis)
}