diff --git a/reflect_config.go b/reflect_config.go index d2e7938..46b6ad7 100644 --- a/reflect_config.go +++ b/reflect_config.go @@ -1,8 +1,15 @@ package gocommon import ( + "crypto/md5" + "encoding/hex" "encoding/json" + "fmt" + "io" + "net/http" + "net/url" "os" + "strings" "time" "repositories.action2quare.com/ayo/gocommon/flagx" @@ -58,14 +65,57 @@ func MonitorConfig[T any](onChanged func(newconf *T)) error { return nil } -func LoadConfig[T any](outptr *T) error { - configfilepath := configFilePath() - content, err := os.ReadFile(configfilepath) - if os.IsNotExist(err) { - return os.WriteFile(configfilepath, []byte("{}"), 0666) +var configContents []byte + +func splitURL(inputURL string) (string, string, error) { + parsedURL, err := url.Parse(inputURL) + if err != nil { + return "", "", err } - return json.Unmarshal([]byte(os.ExpandEnv(string(content))), outptr) + base := fmt.Sprintf("%s://%s", parsedURL.Scheme, parsedURL.Host) + path := parsedURL.Path + + return base, path, nil +} + +func LoadConfig[T any](outptr *T) error { + configfilepath := configFilePath() + if len(configContents) == 0 { + if strings.HasPrefix(configfilepath, "http") { + // 여기서 다운받음 + _, subpath, err := splitURL(configfilepath) + if err != nil { + return err + } + + h := md5.New() + h.Write([]byte(subpath)) + at := hex.EncodeToString(h.Sum(nil)) + + req, _ := http.NewRequest("GET", configfilepath, nil) + req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51") + req.Header.Add("As-X-UrlHash", at) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + configContents, err = io.ReadAll(resp.Body) + if err != nil { + return err + } + } else { + content, _ := os.ReadFile(configfilepath) + if len(content) == 0 { + content = []byte("{}") + } + + configContents = content + } + } + + return json.Unmarshal([]byte(os.ExpandEnv(string(configContents))), outptr) } type StorageAddr struct {