-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (50 loc) · 1.26 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
package main
import (
"fmt"
"net"
"os"
pb "github.com/iochti/user-service/proto"
"github.com/namsral/flag"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// Db is used to init the DB connection in init() method
var Db *MgoDL
func init() {
Db = new(MgoDL)
Db.Init()
}
func dieIf(err error) {
if err == nil {
return
}
fmt.Fprintf(os.Stderr, "Error: %s. Try --help for help.\n", err)
os.Exit(-1)
}
func main() {
addr := flag.String("srv", ":5001", "TCP address to listen on (in host:port form)")
certFile := flag.String("cert", "", "Path to PEM-encoded certificate")
keyFile := flag.String("key", "", "Path to PEM-encoded secret key")
flag.Parse()
svc := &UserSvc{
Db: Db,
}
var server *grpc.Server
// Create server, with TLS if cert & key are specified
if *keyFile == "" && *certFile == "" {
server = grpc.NewServer()
} else if *keyFile == "" {
dieIf(fmt.Errorf("Cert specified with no key"))
} else if *certFile == "" {
dieIf(fmt.Errorf("Key specified with no cert"))
} else {
pair, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
dieIf(err)
creds := grpc.Creds(pair)
server = grpc.NewServer(creds)
}
lis, err := net.Listen("tcp", *addr)
dieIf(err)
pb.RegisterUserSvcServer(server, svc)
server.Serve(lis)
}