-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (133 loc) · 3.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/yaml.v2"
)
var configDir string
func Init() error {
// 设置日志输出前缀
configDir = os.Getenv("CONFIG_DIR")
if configDir == "" {
configDir = "/data"
}
initLog(filepath.Join(configDir, "/log/errors.log"))
return nil
}
func main() {
err := Init()
if err != nil {
log.Fatalf("获取运行路径失败")
return
}
// 获取参数
port := os.Getenv("PORT")
if port == "" {
port = "8081"
}
log.Infof("服务启动,端口:" + port)
http.HandleFunc("/", handle)
err = http.ListenAndServe("0.0.0.0:"+port, nil)
if err != nil {
log.Errorf("start server fail, %v", err)
}
}
func handle(w http.ResponseWriter, r *http.Request) {
res, err := func() ([]byte, error) {
configFileName := r.URL.Query().Get("name")
if configFileName == "" {
return []byte("{}"), nil
}
configItemArr, err := parseConfig(configFileName)
if err != nil {
return nil, err
}
items, err := processConfig(configItemArr)
if err != nil {
return nil, err
}
data := map[string]any{}
res, err := doMerge(data, items)
if err != nil {
return nil, err
}
return yaml.Marshal(res)
}()
if err != nil {
log.Errorf(err.Error())
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
w.Write(res)
}
func parseConfig(configFileName string) ([]*ConfigItem, error) {
// 根据配置名称读取文件
configFilePath := filepath.Join(configDir, configFileName)
configContent, err := os.ReadFile(configFilePath)
if err != nil {
return nil, fmt.Errorf("无法读取配置文件: %v", err)
}
log.Infof("成功读取配置文件: %s", configFileName)
var configItemArr []*ConfigItem
err = yaml.Unmarshal(configContent, &configItemArr)
if err != nil {
return nil, fmt.Errorf("解析配置文件失败: %v", err)
}
return configItemArr, nil
}
func processConfig(configItemArr []*ConfigItem) ([]*ConfigItem, error) {
for i, data:= range configItemArr {
item:= data
_type := item.Type
if _type == "" {
return nil, errors.New("type is required")
}
if _type == "url" {
_data, err := http.Get(item.URL)
if err != nil {
return nil, fmt.Errorf("获取URL失败,url %s, %v", item.URL, err)
}
defer _data.Body.Close()
dataBytes, err := io.ReadAll(_data.Body)
if err != nil {
return nil, fmt.Errorf("读取URL内容失败,url %s, %v", item.URL, err)
}
var dataMap map[string]any
err = yaml.Unmarshal(dataBytes, &dataMap)
if err != nil {
return nil, fmt.Errorf("解析URL内容失败,url %s, %v", item.URL, err)
}
item.Data = dataMap
configItemArr[i] = item
}
}
return configItemArr, nil
}
func initLog(_filepath string) {
level := logrus.InfoLevel
envLevel:= os.Getenv("LOG_LEVEL")
v, err := logrus.ParseLevel(envLevel)
if err == nil {
level = v
}
logrus.SetLevel(level)
lumberjackLogger := &lumberjack.Logger{
// Log file abbsolute path, os agnostic
Filename: filepath.ToSlash(_filepath),
MaxSize: 10, // MB
MaxBackups: 5,
MaxAge: 30, // days
//Compress: true, // disabled by default
}
// Fork writing into two outputs
multiWriter := io.MultiWriter(os.Stdout, lumberjackLogger)
logrus.SetOutput(multiWriter)
}