-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (91 loc) · 2.29 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
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
pb "github.com/clawio/service-localfs-prop/proto/propagator"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"net"
"os"
"runtime"
"strconv"
)
const (
serviceID = "CLAWIO_LOCALFS_PROP"
dsnEnvar = serviceID + "_DSN"
portEnvar = serviceID + "_PORT"
logLevelEnvar = serviceID + "_LOGLEVEL"
maxSqlIdleEnvar = serviceID + "_MAXSQLIDLE"
maxSqlConcurrencyEnvar = serviceID + "_MAXSQLCONCURRENCY"
sharedSecretEnvar = "CLAWIO_SHAREDSECRET"
)
type environ struct {
dsn string
port int
logLevel string
maxSqlIdle int
maxSqlConcurrency int
sharedSecret string
}
func getEnviron() (*environ, error) {
e := &environ{}
e.dsn = os.Getenv(dsnEnvar)
port, err := strconv.Atoi(os.Getenv(portEnvar))
if err != nil {
return nil, err
}
e.port = port
maxSqlIdle, err := strconv.Atoi(os.Getenv(maxSqlIdleEnvar))
if err != nil {
return nil, err
}
e.maxSqlIdle = maxSqlIdle
maxSqlConcurrency, err := strconv.Atoi(os.Getenv(maxSqlConcurrencyEnvar))
if err != nil {
return nil, err
}
e.maxSqlConcurrency = maxSqlConcurrency
e.logLevel = os.Getenv(logLevelEnvar)
e.sharedSecret = os.Getenv(sharedSecretEnvar)
return e, nil
}
func printEnviron(e *environ) {
log.Infof("%s=%s", dsnEnvar, e.dsn)
log.Infof("%s=%d", portEnvar, e.port)
log.Infof("%s=%d", maxSqlIdleEnvar, e.maxSqlIdle)
log.Infof("%s=%d", maxSqlConcurrencyEnvar, e.maxSqlConcurrency)
log.Infof("%s=%d", portEnvar, e.port)
log.Infof("%s=%s", sharedSecretEnvar, "******")
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
env, err := getEnviron()
if err != nil {
log.Error(err)
os.Exit(1)
}
l, err := log.ParseLevel(env.logLevel)
if err != nil {
l = log.ErrorLevel
}
log.SetLevel(l)
log.Infof("Service %s started", serviceID)
printEnviron(env)
p := &newServerParams{}
p.dsn = env.dsn
p.sharedSecret = env.sharedSecret
p.maxSqlIdle = env.maxSqlIdle
p.maxSqlConcurrency = env.maxSqlConcurrency
srv, err := newServer(p)
if err != nil {
log.Error(err)
os.Exit(1)
}
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", env.port))
if err != nil {
log.Error(err)
os.Exit(1)
}
grpcServer := grpc.NewServer()
pb.RegisterPropServer(grpcServer, srv)
grpcServer.Serve(lis)
}