This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
config.go
116 lines (92 loc) · 2.27 KB
/
config.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
package grpc
import (
"errors"
"fmt"
"github.com/spiral/roadrunner"
"github.com/spiral/roadrunner/service"
"net"
"os"
"strings"
"syscall"
)
// Config describes GRPC service configuration.
type Config struct {
// Address to listen.
Listen string
// Proto file associated with the service.
Proto string
// TLS defined authentication method (TLS for now).
TLS TLS
// Workers configures roadrunner grpc and worker pool.
Workers *roadrunner.ServerConfig
}
// TLS defines auth credentials.
type TLS struct {
// Key defined private server key.
Key string
// Cert is https certificate.
Cert string
}
// Hydrate the config and validate it's values.
func (c *Config) Hydrate(cfg service.Config) error {
c.Workers = &roadrunner.ServerConfig{}
c.Workers.InitDefaults()
if err := cfg.Unmarshal(c); err != nil {
return err
}
c.Workers.UpscaleDurations()
return c.Valid()
}
// Valid validates the configuration.
func (c *Config) Valid() error {
if c.Proto == "" && c.Workers.Command != "" {
// only when rr server is set
return errors.New("proto file is required")
}
if c.Proto != "" {
if _, err := os.Stat(c.Proto); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("proto file '%s' does not exists", c.Proto)
}
return err
}
}
if c.Workers.Command != "" {
if err := c.Workers.Pool.Valid(); err != nil {
return err
}
}
if !strings.Contains(c.Listen, ":") {
return errors.New("mailformed grpc grpc address")
}
if c.EnableTLS() {
if _, err := os.Stat(c.TLS.Key); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("key file '%s' does not exists", c.TLS.Key)
}
return err
}
if _, err := os.Stat(c.TLS.Cert); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("cert file '%s' does not exists", c.TLS.Cert)
}
return err
}
}
return nil
}
// Listener creates new rpc socket Listener.
func (c *Config) Listener() (net.Listener, error) {
dsn := strings.Split(c.Listen, "://")
if len(dsn) != 2 {
return nil, errors.New("invalid socket DSN (tcp://:6001, unix://rpc.sock)")
}
if dsn[0] == "unix" {
syscall.Unlink(dsn[1])
}
return net.Listen(dsn[0], dsn[1])
}
// EnableTLS returns true if rr must listen TLS connections.
func (c *Config) EnableTLS() bool {
return c.TLS.Key != "" || c.TLS.Cert != ""
}