From 22b63af7fa282dfc1af11bcda0901c6d85574f87 Mon Sep 17 00:00:00 2001 From: mountain Date: Tue, 4 Jun 2024 16:03:07 +0900 Subject: [PATCH] =?UTF-8?q?config=20path=EC=97=90=20http=20=ED=94=84?= =?UTF-8?q?=EB=A1=9C=ED=86=A0=EC=BD=9C=20=EC=A7=80=EC=9B=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reflect_config.go | 62 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 6 deletions(-) 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 {