config path에 http 프로토콜 지원
This commit is contained in:
@ -1,8 +1,15 @@
|
|||||||
package gocommon
|
package gocommon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"repositories.action2quare.com/ayo/gocommon/flagx"
|
"repositories.action2quare.com/ayo/gocommon/flagx"
|
||||||
@ -58,14 +65,57 @@ func MonitorConfig[T any](onChanged func(newconf *T)) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig[T any](outptr *T) error {
|
var configContents []byte
|
||||||
configfilepath := configFilePath()
|
|
||||||
content, err := os.ReadFile(configfilepath)
|
func splitURL(inputURL string) (string, string, error) {
|
||||||
if os.IsNotExist(err) {
|
parsedURL, err := url.Parse(inputURL)
|
||||||
return os.WriteFile(configfilepath, []byte("{}"), 0666)
|
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 {
|
type StorageAddr struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user