-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (138 loc) · 4.11 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"crypto/tls"
"github.com/ewe-studios/sabuhp/cmd/sabuclient"
"github.com/ewe-studios/sabuhp/sabu"
"github.com/ewe-studios/sabuhp/servers/clientServer"
"github.com/go-redis/redis/v8"
"github.com/influx6/npkg/nerror"
"github.com/urfave/cli/v2"
"log"
"os"
)
func main(){
var app = &cli.App{
Name: "sabuhp",
Usage: "Building distributed application one worker at a time",
Commands: []*cli.Command{
{
Name: "client",
Usage: "starts a sabuhp client server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "redis_username",
Aliases: nil,
Usage: "Username for logging into redis",
EnvVars: nil,
FilePath: "",
Required: false,
Hidden: false,
TakesFile: false,
DefaultText: "",
},
&cli.StringFlag{
Name: "redis_password",
Aliases: nil,
Usage: "Password for logging into redis",
EnvVars: nil,
FilePath: "",
Required: false,
Hidden: false,
TakesFile: false,
DefaultText: "",
},
&cli.IntFlag{
Name: "redis_db",
Usage: "DB integer for redis",
DefaultText: "",
},
&cli.StringFlag{
Name: "redis_addr",
Usage: "Address with port pointing to redis (0.0.0.0:6590)",
DefaultText: "",
},
&cli.StringFlag{
Name: "redis_tls_cert_key",
Usage: "Path to tls certificate key",
DefaultText: "",
},
&cli.StringFlag{
Name: "redis_tls_cert",
Usage: "Path to tls certificate file",
DefaultText: "",
},
&cli.StringFlag{
Name: "tls_cert_key",
Usage: "Path to tls certificate key",
DefaultText: "",
},
&cli.StringFlag{
Name: "tls_cert",
Usage: "Path to tls certificate file",
DefaultText: "",
},
&cli.StringFlag{
Name: "codec",
Usage: "Codec to be used by client, can be (messagepack, json, gob, flatbuffer)",
DefaultText: "messagepack",
},
&cli.StringFlag{
Name: "addr",
Aliases: nil,
Usage: "Host for client",
EnvVars: []string{"CLIENT_HOST"},
Value: "",
DefaultText: "0.0.0.0:9650",
Destination: nil,
HasBeenSet: false,
},
},
Action: func(context *cli.Context) error {
var tlsCertificateFile = context.String("tls_cert")
var tlsKeyFile = context.String("tls_cert_key")
var tlsConfig *tls.Config
if len(tlsCertificateFile) != 0 && len(tlsKeyFile) != 0 {
var cer, err = tls.LoadX509KeyPair(tlsCertificateFile, tlsKeyFile)
if err != nil {
return nerror.WrapOnly(err)
}
tlsConfig = &tls.Config{Certificates: []tls.Certificate{cer}}
}
var redisTlsCertificateFile = context.String("redis_tls_cert")
var redisTlsKeyFile = context.String("redis_tls_cert_key")
var redisTlsConfig *tls.Config
if len(redisTlsKeyFile) != 0 && len(redisTlsCertificateFile) != 0 {
var cer, err = tls.LoadX509KeyPair(redisTlsCertificateFile, redisTlsKeyFile)
if err != nil {
return nerror.WrapOnly(err)
}
redisTlsConfig = &tls.Config{Certificates: []tls.Certificate{cer}}
}
var redisOp redis.Options
redisOp.TLSConfig = redisTlsConfig
redisOp.DB = context.Int("redis_db")
redisOp.Addr = context.String("redis_addr")
redisOp.Username = context.String("redis_username")
redisOp.Password = context.String("redis_password")
var clientAddr = context.String("addr")
var codec sabu.Codec
var codecName = context.String("codec")
switch codecName {
case "gob","gb", "GOB":
codec = clientServer.DefaultJSONCodec
case "json","JSON":
codec = clientServer.DefaultJSONCodec
case "MessagePack","messagepack", "msgpack", "msgpck":
codec = clientServer.DefaultMsgPackCodec
default:
return nerror.New("unknown codec %q", codecName)
}
return sabuclient.Execute(context.Context, tlsConfig, codec, redisOp, clientAddr)
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatalln(err)
}
}