aqi/remote_provider.go
2024-06-18 18:09:39 +08:00

67 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package aqi
import (
"net/url"
"path/filepath"
"strings"
"github.com/wonli/aqi/logger"
)
type Provider string
const ProviderConsul Provider = "consul"
const ProviderEtcd Provider = "etcd"
type RemoteProvider struct {
Name Provider //服务商名称
Path string //路径
Endpoint string //服务器地址
Type string //json, yaml等
}
// ParseRemoteProvider 格式 provider[s]://endpoint/path.type
// 例consul://localhost:8500/a.yaml
// 表示远程配置中心为consul服务器地址为http://localhost:8500, path为a, 配置类型是yaml
// scheme加s表示服务器支持ssl
func ParseRemoteProvider(s string) *RemoteProvider {
u, err := url.Parse(s)
if err != nil {
logger.SugarLog.Errorf("Parse remote provider endpoint error: %s", err.Error())
return nil
}
if u.Scheme == "" {
return nil
}
path := strings.TrimLeft(u.Path, "/")
fileType := filepath.Ext(path)
if fileType == "" {
fileType = "yaml"
} else {
path = path[:len(path)-len(fileType)]
fileType = fileType[1:]
}
p := &RemoteProvider{
Path: path,
Type: fileType,
}
switch u.Scheme {
case "consul", "consuls":
p.Name = ProviderConsul
case "etcd", "etcds":
p.Name = ProviderEtcd
}
if strings.HasSuffix(u.Scheme, "s") {
p.Endpoint = "https://" + u.Host
} else {
p.Endpoint = "http://" + u.Host
}
return p
}