mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-14 06:10:22 +01:00
47 lines
891 B
Go
47 lines
891 B
Go
// Copyright 2022 by Sascha L. Teichmann
|
|
// Use of this source code is governed by the MIT license
|
|
// that can be found in the LICENSE file.
|
|
|
|
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type DBClientCreator func() (DBClient, error)
|
|
|
|
func IsPostgreSQL(host string) (string, bool) {
|
|
if strings.HasPrefix(host, "postgres:") {
|
|
return host[len("postgres:"):], true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
func CreateDBClientCreator(host string, port int) DBClientCreator {
|
|
|
|
if host, ok := IsPostgreSQL(host); ok {
|
|
return func() (DBClient, error) {
|
|
return NewPGClient(host)
|
|
}
|
|
}
|
|
|
|
var address string
|
|
if strings.ContainsRune(host, '/') {
|
|
address = host
|
|
} else {
|
|
address = fmt.Sprintf("%s:%d", host, port)
|
|
}
|
|
|
|
var proto string
|
|
if strings.ContainsRune(address, '/') {
|
|
proto = "unix"
|
|
} else {
|
|
proto = "tcp"
|
|
}
|
|
|
|
return func() (DBClient, error) {
|
|
return NewRedisClient(proto, address)
|
|
}
|
|
}
|