2022-04-28 16:35:02 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-05-08 09:00:22 +00:00
|
|
|
"fmt"
|
2023-12-04 10:22:49 +00:00
|
|
|
"maps"
|
2022-04-28 16:35:02 +00:00
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-04-28 16:35:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServerType uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
ServerTypeUnspecified ServerType = iota
|
|
|
|
ServerTypeRedis
|
2022-06-18 09:45:45 +00:00
|
|
|
ServerTypePostgres
|
|
|
|
ServerTypeMysql
|
2022-05-08 09:00:22 +00:00
|
|
|
|
|
|
|
//nolint:lll // cannot break regex
|
|
|
|
// language=regexp
|
|
|
|
urlSchemeRegex = `^(?P<scheme>\w+)://((?P<username>[\w-.]+)(:(?P<password>.*))?@)?(\{(?P<hosts>(.+:\d{1,5})(,(.+:\d{1,5}))*)}|(?P<host>.+:\d{1,5}))(?P<path>/.*$)?`
|
2022-04-28 16:35:02 +00:00
|
|
|
)
|
|
|
|
|
2022-09-22 12:04:53 +00:00
|
|
|
//nolint:goconst
|
2022-05-08 09:00:22 +00:00
|
|
|
func (t ServerType) Scheme() string {
|
|
|
|
switch t {
|
|
|
|
case ServerTypeRedis:
|
|
|
|
return "redis"
|
2022-06-18 09:45:45 +00:00
|
|
|
case ServerTypePostgres:
|
|
|
|
return "postgres"
|
|
|
|
case ServerTypeMysql:
|
|
|
|
return "mysql"
|
2022-05-08 09:00:22 +00:00
|
|
|
case ServerTypeUnspecified:
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 09:45:45 +00:00
|
|
|
func (t ServerType) Driver() string {
|
|
|
|
switch t {
|
|
|
|
case ServerTypeRedis:
|
|
|
|
return "redis"
|
|
|
|
case ServerTypePostgres:
|
|
|
|
return "pgx"
|
|
|
|
case ServerTypeMysql:
|
|
|
|
return "mysql"
|
|
|
|
case ServerTypeUnspecified:
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
var hostsRegexp = regexp.MustCompile(urlSchemeRegex)
|
|
|
|
|
|
|
|
type Credentials struct {
|
|
|
|
Username string
|
|
|
|
Password *string
|
|
|
|
}
|
|
|
|
|
2022-06-18 09:45:45 +00:00
|
|
|
func (c *Credentials) appendToBuilder(builder *strings.Builder) {
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = builder.WriteString(c.Username)
|
|
|
|
if c.Password != nil {
|
|
|
|
_, _ = builder.WriteString(":")
|
|
|
|
_, _ = builder.WriteString(*c.Password)
|
|
|
|
}
|
|
|
|
_, _ = builder.WriteString("@")
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
var (
|
|
|
|
_ json.Unmarshaler = (*Server)(nil)
|
|
|
|
_ yaml.Unmarshaler = (*Server)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type marshaledServer struct {
|
|
|
|
Type ServerType
|
|
|
|
URL string
|
|
|
|
Hosts []string
|
|
|
|
Args map[string]any
|
|
|
|
}
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Type ServerType
|
|
|
|
Credentials *Credentials
|
|
|
|
Hosts []string
|
|
|
|
Path []string
|
|
|
|
Args map[string]any
|
|
|
|
}
|
|
|
|
|
2022-06-18 09:45:45 +00:00
|
|
|
func (s *Server) Query() string {
|
|
|
|
if s.Args == nil || len(s.Args) < 1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var builder strings.Builder
|
|
|
|
|
|
|
|
for k, v := range s.Args {
|
|
|
|
if builder.Len() > 0 {
|
|
|
|
_, _ = builder.WriteString("&")
|
|
|
|
}
|
|
|
|
_, _ = builder.WriteString(k)
|
|
|
|
_, _ = builder.WriteString("=")
|
|
|
|
_, _ = builder.WriteString(fmt.Sprintf("%v", v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) DSNs() (result []string) {
|
|
|
|
var builder strings.Builder
|
|
|
|
result = make([]string, 0, len(s.Hosts))
|
|
|
|
joinedPath := strings.Join(s.Path, "/")
|
|
|
|
|
|
|
|
for i := range s.Hosts {
|
|
|
|
builder.Reset()
|
|
|
|
s.Credentials.appendToBuilder(&builder)
|
|
|
|
|
|
|
|
_, _ = builder.WriteString("tcp(")
|
|
|
|
_, _ = builder.WriteString(s.Hosts[i])
|
|
|
|
_, _ = builder.WriteString(")")
|
|
|
|
|
|
|
|
if len(s.Path) > 0 {
|
|
|
|
_, _ = builder.WriteString("/")
|
|
|
|
|
|
|
|
_, _ = builder.WriteString(joinedPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if query := s.Query(); query != "" {
|
|
|
|
_, _ = builder.WriteString("?")
|
|
|
|
_, _ = builder.WriteString(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, builder.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ConnectionStrings() (result []string) {
|
|
|
|
var builder strings.Builder
|
|
|
|
result = make([]string, 0, len(s.Hosts))
|
|
|
|
joinedPath := strings.Join(s.Path, "/")
|
|
|
|
|
|
|
|
for i := range s.Hosts {
|
|
|
|
builder.Reset()
|
|
|
|
|
|
|
|
_, _ = builder.WriteString(s.Type.Scheme())
|
|
|
|
_, _ = builder.WriteString("://")
|
|
|
|
|
|
|
|
s.Credentials.appendToBuilder(&builder)
|
|
|
|
|
|
|
|
_, _ = builder.WriteString(s.Hosts[i])
|
|
|
|
if len(s.Path) > 0 {
|
|
|
|
_, _ = builder.WriteString("/")
|
|
|
|
|
|
|
|
_, _ = builder.WriteString(joinedPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if query := s.Query(); query != "" {
|
|
|
|
_, _ = builder.WriteString("?")
|
|
|
|
_, _ = builder.WriteString(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, builder.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
func (s *Server) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
s.Args = make(map[string]any)
|
|
|
|
|
|
|
|
tmp := new(marshaledServer)
|
|
|
|
|
|
|
|
if err := value.Decode(tmp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.mergedMarshaledServer(*tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) UnmarshalJSON(bytes []byte) error {
|
|
|
|
s.Args = make(map[string]any)
|
|
|
|
|
|
|
|
tmp := new(marshaledServer)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(bytes, tmp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.mergedMarshaledServer(*tmp)
|
|
|
|
}
|
2022-04-28 16:35:02 +00:00
|
|
|
|
2022-09-27 20:19:27 +00:00
|
|
|
func (s *Server) UnmarshalURL(rawURL string) error {
|
|
|
|
rawPath, username, password, err := s.extractBaseProperties(rawURL)
|
2022-05-08 09:00:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-04-28 16:35:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
if username != "" {
|
|
|
|
s.Credentials = &Credentials{
|
|
|
|
Username: username,
|
2022-04-28 16:35:02 +00:00
|
|
|
}
|
2022-05-08 09:00:22 +00:00
|
|
|
if password != "" {
|
|
|
|
s.Credentials.Password = &password
|
2022-04-28 16:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
if rawPath != "" {
|
2022-09-27 20:19:27 +00:00
|
|
|
parsedURL, err := url.Parse(fmt.Sprintf("%s://%s%s", s.Type.Scheme(), s.Hosts[0], rawPath))
|
2022-05-08 09:00:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-27 20:19:27 +00:00
|
|
|
if err := s.unmarshalPath(parsedURL); err != nil {
|
2022-05-08 09:00:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.Args = make(map[string]any)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:19:27 +00:00
|
|
|
func (s *Server) extractBaseProperties(rawURL string) (rawPath, username, password string, err error) {
|
|
|
|
allMatches := hostsRegexp.FindAllStringSubmatch(rawURL, -1)
|
2022-05-08 09:00:22 +00:00
|
|
|
if matchLen := len(allMatches); matchLen != 1 {
|
|
|
|
return "", "", "", fmt.Errorf("ambiguous server match: %d", matchLen)
|
|
|
|
}
|
|
|
|
|
|
|
|
match := allMatches[0]
|
|
|
|
|
|
|
|
for i, name := range hostsRegexp.SubexpNames() {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "scheme":
|
|
|
|
s.Type = SchemeToServerType(match[i])
|
|
|
|
case "host":
|
|
|
|
singleHost := match[i]
|
|
|
|
if singleHost != "" {
|
|
|
|
s.Hosts = []string{singleHost}
|
|
|
|
}
|
|
|
|
case "hosts":
|
|
|
|
hosts := strings.Split(match[i], ",")
|
|
|
|
for _, host := range hosts {
|
|
|
|
s.Hosts = append(s.Hosts, strings.TrimSpace(host))
|
|
|
|
}
|
|
|
|
case "path":
|
|
|
|
rawPath = match[i]
|
|
|
|
case "username":
|
|
|
|
username = match[i]
|
|
|
|
case "password":
|
|
|
|
password = match[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rawPath, username, password, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) unmarshalPath(u *url.URL) error {
|
|
|
|
s.Path = strings.Split(strings.Trim(u.EscapedPath(), "/"), "/")
|
2022-04-28 16:35:02 +00:00
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
q := u.Query()
|
2022-04-28 16:35:02 +00:00
|
|
|
qm := map[string][]string(q)
|
2022-05-08 09:00:22 +00:00
|
|
|
s.Args = make(map[string]any, len(qm))
|
2022-04-28 16:35:02 +00:00
|
|
|
|
|
|
|
for k := range qm {
|
|
|
|
var val any
|
|
|
|
if err := json.Unmarshal([]byte(q.Get(k)), &val); err != nil {
|
2022-05-08 09:00:22 +00:00
|
|
|
return err
|
2022-04-28 16:35:02 +00:00
|
|
|
} else {
|
2022-05-08 09:00:22 +00:00
|
|
|
s.Args[k] = val
|
2022-04-28 16:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
return nil
|
2022-04-28 16:35:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
func (s *Server) mergedMarshaledServer(srv marshaledServer) error {
|
|
|
|
if srv.URL != "" {
|
|
|
|
if err := s.UnmarshalURL(srv.URL); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-28 16:35:02 +00:00
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
if srv.Args != nil {
|
|
|
|
maps.Copy(s.Args, srv.Args)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Type = srv.Type
|
|
|
|
s.Hosts = srv.Hosts
|
|
|
|
if srv.Args != nil {
|
|
|
|
s.Args = srv.Args
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-04-28 16:35:02 +00:00
|
|
|
}
|