Skip to content

Commit 943e6c4

Browse files
committed
feat: 加载远程配置
1 parent 54b2b6d commit 943e6c4

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

config.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ package main
22

33
import (
44
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
58
"os"
9+
"strings"
610
)
711

812
type BackupProviderConfigType string
@@ -107,12 +111,25 @@ func ConvertToBackupProviderConfig[T any](raw any) (*T, error) {
107111
}
108112

109113
func LoadConfig(path string) (*SyncConfig, error) {
110-
file, err := os.ReadFile(path)
114+
var body []byte
115+
var err error
116+
117+
if strings.HasPrefix(path, "http") {
118+
resp, httpErr := http.Get(path)
119+
if httpErr != nil {
120+
return nil, fmt.Errorf("failed to fetch config: %w", httpErr)
121+
}
122+
defer resp.Body.Close()
123+
body, err = io.ReadAll(resp.Body)
124+
} else {
125+
body, err = os.ReadFile(path)
126+
}
127+
111128
if err != nil {
112-
return nil, err
129+
return nil, fmt.Errorf("failed to read config: %w", err)
113130
}
114131
var conf SyncConfig
115-
err = json.Unmarshal(file, &conf)
132+
err = json.Unmarshal(body, &conf)
116133
if err != nil {
117134
return nil, err
118135
}

0 commit comments

Comments
 (0)