2022-05-08 09:00:22 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Option interface {
|
2023-12-04 10:22:49 +00:00
|
|
|
Extend(n *Nurse) error
|
2022-05-08 09:00:22 +00:00
|
|
|
}
|
2023-12-04 10:22:49 +00:00
|
|
|
OptionFunc func(n *Nurse) error
|
2022-05-08 09:00:22 +00:00
|
|
|
)
|
|
|
|
|
2023-12-04 10:22:49 +00:00
|
|
|
func (f OptionFunc) Extend(n *Nurse) error {
|
2022-05-08 09:00:22 +00:00
|
|
|
return f(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(opts ...Option) (*Nurse, error) {
|
|
|
|
var (
|
2023-12-04 10:22:49 +00:00
|
|
|
inst = new(Nurse)
|
2022-05-08 09:00:22 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := range opts {
|
2023-12-04 10:22:49 +00:00
|
|
|
if err = opts[i].Extend(inst); err != nil {
|
2022-05-08 09:00:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 10:22:49 +00:00
|
|
|
return inst, nil
|
2022-05-08 09:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Nurse struct {
|
2022-08-07 10:22:21 +00:00
|
|
|
Servers map[string]Server
|
|
|
|
Endpoints map[Route]EndpointSpec
|
|
|
|
CheckTimeout time.Duration
|
|
|
|
CheckAttempts uint
|
2023-12-04 10:22:49 +00:00
|
|
|
Insecure bool
|
2022-05-08 09:00:22 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 10:22:49 +00:00
|
|
|
func (n *Nurse) ServerLookup() (*ServerRegister, error) {
|
2022-05-13 13:38:19 +00:00
|
|
|
register := NewServerRegister()
|
|
|
|
|
|
|
|
for name, srv := range n.Servers {
|
|
|
|
if err := register.Register(name, srv); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return register, nil
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
// Merge merges the current Nurse instance with another one
|
|
|
|
// giving the current instance precedence means no set value is overwritten
|
2023-12-04 10:22:49 +00:00
|
|
|
func (n *Nurse) Merge(other Nurse) {
|
2022-05-08 09:00:22 +00:00
|
|
|
if n.CheckTimeout == 0 {
|
|
|
|
n.CheckTimeout = other.CheckTimeout
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:22:21 +00:00
|
|
|
if n.CheckAttempts == 0 {
|
|
|
|
n.CheckAttempts = other.CheckAttempts
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
for name, srv := range other.Servers {
|
|
|
|
if _, ok := n.Servers[name]; !ok {
|
|
|
|
n.Servers[name] = srv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Nurse) Unmarshal(reader io.ReadSeeker) error {
|
|
|
|
providers := []func(io.Reader) configDecoder{
|
|
|
|
newYAMLDecoder,
|
|
|
|
newJSONDecoder,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range providers {
|
|
|
|
if err := n.tryUnmarshal(reader, providers[i]); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Nurse) ReadFromFile(configFilePath string) error {
|
|
|
|
if file, err := os.Open(configFilePath); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
defer func() {
|
|
|
|
_ = file.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return n.Unmarshal(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Nurse) tryUnmarshal(seeker io.ReadSeeker, prov func(reader io.Reader) configDecoder) error {
|
|
|
|
if _, err := seeker.Seek(0, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := prov(seeker)
|
|
|
|
return decoder.DecodeConfig(n)
|
|
|
|
}
|