-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
72 lines (53 loc) · 1.59 KB
/
Copy pathmain.go
File metadata and controls
72 lines (53 loc) · 1.59 KB
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
package main
import (
"log"
"net"
"github.com/Serial-Experiments-Weebify/weebchat/core"
"github.com/alecthomas/kong"
)
var CLI struct {
Data struct {
Control string `help:"Control server address" default:"localhost:6767" short:"c"`
SecretKey string `required:"" help:"Secret key for node authentication." short:"s"`
Address string `optional:"" help:"Address to listen on." short:"a"`
} `cmd:"" help:"Start the data server."`
Control struct {
Address string `optional:"" help:"Address to listen on." default:"localhost:6767" short:"a"`
SecretKey string `required:"" help:"Secret key for node authentication." short:"s"`
} `cmd:"" help:"Start the control server."`
}
func createListener(addr string) (net.Listener, error) {
if addr != "" {
return net.Listen("tcp", addr)
} else {
return net.Listen("tcp", "localhost:0")
}
}
func main() {
ctx := kong.Parse(&CLI)
switch ctx.Command() {
case "data":
lis, err := createListener(CLI.Data.Address)
if err != nil {
panic(err)
}
log.Printf("Data server starting on %s (control: %s)", lis.Addr().String(), CLI.Data.Control)
srv, err := core.StartDataServer(lis.Addr().String(), CLI.Data.Control, CLI.Data.SecretKey)
if err != nil {
panic(err)
}
if err := srv.Serve(lis); err != nil {
panic(err)
}
case "control":
lis, err := createListener(CLI.Control.Address)
if err != nil {
panic(err)
}
log.Printf("Control plane server starting on %s", lis.Addr().String())
srv := core.StartControlServer(lis.Addr().String(), CLI.Control.SecretKey)
if err := srv.Serve(lis); err != nil {
panic(err)
}
}
}