Files
gocommon/reflect_config.go

136 lines
2.8 KiB
Go
Raw Permalink Normal View History

package gocommon
2023-05-24 15:10:15 +09:00
import (
"crypto/md5"
"encoding/hex"
2023-05-24 15:10:15 +09:00
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
2023-05-24 15:10:15 +09:00
"os"
"strings"
2023-05-24 15:10:15 +09:00
"time"
2023-06-21 14:13:30 +09:00
"repositories.action2quare.com/ayo/gocommon/flagx"
2023-05-24 15:10:15 +09:00
)
2023-06-21 14:13:30 +09:00
var configfileflag = flagx.String("config", "", "")
2023-05-24 15:10:15 +09:00
func configFilePath() string {
2023-06-14 18:15:57 +09:00
configfilepath := "config.json"
2023-05-24 15:10:15 +09:00
if configfileflag != nil && len(*configfileflag) > 0 {
configfilepath = *configfileflag
}
// if !strings.HasPrefix(configfilepath, "/") {
// exe, _ := os.Executable()
// configfilepath = path.Join(path.Dir(exe), configfilepath)
// }
2024-02-04 21:03:48 +09:00
2023-05-24 15:10:15 +09:00
return configfilepath
}
func ConfigModTime() time.Time {
fi, err := os.Stat(configFilePath())
if err == nil {
return fi.ModTime()
}
return time.Time{}
}
func MonitorConfig[T any](onChanged func(newconf *T)) error {
fi, err := os.Stat(configFilePath())
if err != nil {
return err
}
go func(modTime time.Time, filepath string) {
for {
if fi, err := os.Stat(filepath); err == nil {
if modTime != fi.ModTime() {
var next T
if err := LoadConfig(&next); err == nil {
fi, _ := os.Stat(filepath)
modTime = fi.ModTime()
onChanged(&next)
}
}
}
time.Sleep(time.Second)
}
}(fi.ModTime(), configFilePath())
return nil
}
var configContents []byte
func splitURL(inputURL string) (string, string, error) {
parsedURL, err := url.Parse(inputURL)
if err != nil {
return "", "", err
}
base := fmt.Sprintf("%s://%s", parsedURL.Scheme, parsedURL.Host)
path := parsedURL.Path
return base, path, nil
}
2023-05-24 15:10:15 +09:00
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
}
2023-05-24 15:10:15 +09:00
}
return json.Unmarshal([]byte(os.Expand(string(configContents), func(in string) string {
envval := os.Getenv(in)
if len(envval) == 0 {
return "$" + in
}
return envval
})), outptr)
2023-05-24 15:10:15 +09:00
}
type StorageAddr struct {
Mongo string
Squashed commit of the following: commit 29b2f258507d9e11e20a9693b86b3ae09e10d88c Author: mountain <mountain@action2quare.com> Date: Wed Jul 19 09:33:37 2023 +0900 타입 이름 변경 commit 256bfd030c294d2d7bea4ca2c3082f2d49ae9aef Author: mountain <mountain@action2quare.com> Date: Wed Jul 19 09:31:01 2023 +0900 redison 추가 commit 72a683fed2c024616b2171be1f6841a11150a3a8 Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 19:51:24 2023 +0900 gob에 []any 추가 commit 89fa9e4ac585026c331d697929697af5defc6b9d Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 17:45:12 2023 +0900 write control 수정 commit d724cc84fa94ab6cdd3d8bddd3ab08c99d0aef3a Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 17:38:04 2023 +0900 redis pubsub 채널 이름에 디비 인덱스 추가 commit 8df248fa54908a8cb547813179e4269e25c7df87 Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 17:20:47 2023 +0900 close를 writecontrol로 변경 commit 40a603522d4082f426a0d3818d852db53e458cd2 Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 12:21:06 2023 +0900 conn에 msg를 쓰는 쓰레드 단일화 commit c21017d2cd8b2bd26bdbf6d4eca49d06b8462ce0 Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 11:08:38 2023 +0900 redis call이 문제가 아니었음 commit 82abcddb497bdb95b0eff2d7a98b72cacf37bc9a Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 11:04:15 2023 +0900 잦은 redis call 회피 commit 289af24a8ffaa55336bfabca151a71f6e1f82290 Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 09:55:18 2023 +0900 room create 메시지 전송 commit 4b35e0e6386b1a27e4dec854d1fc2c755f20aeef Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 09:45:27 2023 +0900 EventReceiver 인터페이스 추가 commit 29843802ff0eb6378af63b2a14de826a594f5a9e Author: mountain <mountain@action2quare.com> Date: Mon Jul 17 17:45:40 2023 +0900 gob 등록 commit 66aea48fb7322728d0f665e37b2dd818f441c26f Author: mountain <mountain@action2quare.com> Date: Sun Jul 16 18:39:11 2023 +0900 채널간 publish marshalling을 gob으로 변경 commit 8f6c87a8aeb86f8fb41ba7a5ff75e3e8ee9ede2c Author: mountain <mountain@action2quare.com> Date: Sun Jul 16 16:37:02 2023 +0900 redis option을 copy로 변경 commit f0f459332d1a62a938a9b9b7ca34e3d3a2f26e8c Author: mountain <mountain@action2quare.com> Date: Sat Jul 15 17:08:33 2023 +0900 wshandler에서 authcache제거하고 config 포맷 변경
2023-07-19 09:35:25 +09:00
Redis map[string]string
2023-05-24 15:10:15 +09:00
}
type RegionStorageConfig struct {
RegionStorage map[string]StorageAddr `json:"region_storage"`
}