forked from Monibuca/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·256 lines (245 loc) · 8.05 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package engine // import "m7s.live/engine/v4"
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/denisbrodbeck/machineid"
"github.com/google/uuid"
. "github.com/logrusorgru/aurora/v4"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v3"
"m7s.live/engine/v4/lang"
"m7s.live/engine/v4/log"
"m7s.live/engine/v4/util"
)
var (
SysInfo struct {
StartTime time.Time //启动时间
LocalIP string
Version string
}
ExecPath = os.Args[0]
ExecDir = filepath.Dir(ExecPath)
// ConfigRaw 配置信息的原始数据
ConfigRaw []byte
Plugins = make(map[string]*Plugin) // Plugins 所有的插件配置
plugins []*Plugin //插件列表
EngineConfig = &GlobalConfig{}
Engine = InstallPlugin(EngineConfig)
SettingDir = filepath.Join(ExecDir, ".m7s") //配置缓存目录,该目录按照插件名称作为文件名存储修改过的配置
MergeConfigs = []string{"Publish", "Subscribe", "HTTP"} //需要合并配置的属性项,插件若没有配置则使用全局配置
EventBus chan any
apiList []string //注册到引擎的API接口列表
)
func init() {
if setting_dir := os.Getenv("M7S_SETTING_DIR"); setting_dir != "" {
SettingDir = setting_dir
}
if conn, err := net.Dial("udp", "114.114.114.114:80"); err == nil {
SysInfo.LocalIP, _, _ = strings.Cut(conn.LocalAddr().String(), ":")
}
}
// Run 启动Monibuca引擎,传入总的Context,可用于关闭所有
func Run(ctx context.Context, conf any) (err error) {
id, _ := machineid.ProtectedID("monibuca")
SysInfo.StartTime = time.Now()
SysInfo.Version = Engine.Version
Engine.Context = ctx
var cg map[string]map[string]any
switch v := conf.(type) {
case string:
if _, err = os.Stat(v); err != nil {
v = filepath.Join(ExecDir, v)
}
if ConfigRaw, err = os.ReadFile(v); err != nil {
log.Warn("read config file error:", err.Error())
}
case []byte:
ConfigRaw = v
case map[string]map[string]any:
cg = v
}
if err = util.CreateShutdownScript(); err != nil {
log.Error("create shutdown script error:", err)
}
if err = os.MkdirAll(SettingDir, 0766); err != nil {
log.Error("create dir .m7s error:", err)
return
}
log.Info("Ⓜ starting engine:", Blink(Engine.Version))
if ConfigRaw != nil {
if err = yaml.Unmarshal(ConfigRaw, &cg); err != nil {
log.Error("parsing yml error:", err)
}
}
Engine.RawConfig.Parse(&EngineConfig.Engine, "GLOBAL")
if cg != nil {
Engine.RawConfig.ParseUserFile(cg["global"])
}
var logger log.Logger
log.LocaleLogger = logger.Lang(lang.Get(EngineConfig.LogLang))
if EngineConfig.LogLevel == "trace" {
log.Trace = true
log.LogLevel.SetLevel(zap.DebugLevel)
} else {
loglevel, err := zapcore.ParseLevel(EngineConfig.LogLevel)
if err != nil {
logger.Error("parse log level error:", zap.Error(err))
loglevel = zapcore.InfoLevel
}
log.LogLevel.SetLevel(loglevel)
}
Engine.Logger = log.LocaleLogger.Named("engine")
Engine.assign()
Engine.Logger.Debug("", zap.Any("config", EngineConfig))
util.PoolSize = EngineConfig.PoolSize
EventBus = make(chan any, EngineConfig.EventBusSize)
go EngineConfig.Listen(Engine)
for _, plugin := range plugins {
plugin.Logger = log.LocaleLogger.Named(plugin.Name)
if os.Getenv(strings.ToUpper(plugin.Name)+"_ENABLE") == "false" {
plugin.Disabled = true
plugin.Warn("disabled by env")
continue
}
plugin.Info("initialize", zap.String("version", plugin.Version))
plugin.RawConfig.Parse(plugin.Config, strings.ToUpper(plugin.Name))
for _, fname := range MergeConfigs {
if name := strings.ToLower(fname); plugin.RawConfig.Has(name) {
plugin.RawConfig.Get(name).ParseGlobal(Engine.RawConfig.Get(name))
}
}
var userConfig map[string]any
if plugin.defaultYaml != "" {
if err := yaml.Unmarshal([]byte(plugin.defaultYaml), &userConfig); err != nil {
log.Error("parsing default config error:", err)
} else {
plugin.RawConfig.ParseDefaultYaml(userConfig)
}
}
userConfig = cg[strings.ToLower(plugin.Name)]
plugin.RawConfig.ParseUserFile(userConfig)
if EngineConfig.DisableAll {
plugin.Disabled = true
}
if userConfig["enable"] == false {
plugin.Disabled = true
} else if userConfig["enable"] == true {
plugin.Disabled = false
}
if plugin.Disabled {
plugin.Warn("plugin disabled")
} else {
plugin.assign()
}
}
UUID := uuid.NewString()
contentBuf := bytes.NewBuffer(nil)
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "https://console.monibuca.com/report", nil)
req.Header.Set("Content-Type", "application/json")
version := Engine.Version
if ver, ok := ctx.Value("version").(string); ok && ver != "" && ver != "dev" {
version = ver
}
if EngineConfig.LogLang == "zh" {
log.Info("monibuca ", version, Green(" 启动成功"))
} else {
log.Info("monibuca ", version, Green(" start success"))
}
var enabledPlugins, disabledPlugins []*Plugin
for _, plugin := range plugins {
if plugin.Disabled {
disabledPlugins = append(disabledPlugins, plugin)
} else {
enabledPlugins = append(enabledPlugins, plugin)
}
}
if EngineConfig.LogLang == "zh" {
fmt.Print("已运行的插件:")
} else {
fmt.Print("enabled plugins:")
}
for _, plugin := range enabledPlugins {
fmt.Print(Colorize(" "+plugin.Name+" ", BlackFg|GreenBg|BoldFm), " ")
}
fmt.Println()
if EngineConfig.LogLang == "zh" {
fmt.Print("已禁用的插件:")
} else {
fmt.Print("disabled plugins:")
}
for _, plugin := range disabledPlugins {
fmt.Print(Colorize(" "+plugin.Name+" ", BlackFg|RedBg|CrossedOutFm), " ")
}
fmt.Println()
if EngineConfig.LogLang == "zh" {
fmt.Println(Cyan("🌏 官网地址: ").Bold(), Yellow("https://monibuca.com"))
fmt.Println(Cyan("🔥 启动工程: ").Bold(), Yellow("https://github.com/langhuihui/monibuca"))
fmt.Println(Cyan("📄 文档地址: ").Bold(), Yellow("https://monibuca.com/docs/index.html"))
fmt.Println(Cyan("🎞 视频教程: ").Bold(), Yellow("https://space.bilibili.com/328443019/channel/collectiondetail?sid=514619"))
fmt.Println(Cyan("🖥 远程界面: ").Bold(), Yellow("https://console.monibuca.com"))
fmt.Println(Yellow("关注公众号:不卡科技,获取更多信息"))
} else {
fmt.Println(Cyan("🌏 WebSite: ").Bold(), Yellow("https://m7s.live"))
fmt.Println(Cyan("🔥 Github: ").Bold(), Yellow("https://github.com/langhuihui/monibuca"))
fmt.Println(Cyan("📄 Docs: ").Bold(), Yellow("https://docs.m7s.live"))
fmt.Println(Cyan("🎞 Videos: ").Bold(), Yellow("https://space.bilibili.com/328443019/channel/collectiondetail?sid=514619"))
fmt.Println(Cyan("🖥 Console: ").Bold(), Yellow("https://console.monibuca.com"))
}
rp := struct {
UUID string `json:"uuid"`
Machine string `json:"machine"`
Instance string `json:"instance"`
Version string `json:"version"`
OS string `json:"os"`
Arch string `json:"arch"`
}{UUID, id, EngineConfig.GetInstanceId(), version, runtime.GOOS, runtime.GOARCH}
json.NewEncoder(contentBuf).Encode(&rp)
req.Body = io.NopCloser(contentBuf)
EngineConfig.OnEvent(ctx)
go func() {
var c http.Client
reportTimer := time.NewTimer(time.Minute)
c.Do(req)
for {
<-reportTimer.C
contentBuf.Reset()
contentBuf.WriteString(fmt.Sprintf(`{"uuid":"`+UUID+`","streams":%d}`, Streams.Len()))
req.Body = io.NopCloser(contentBuf)
c.Do(req)
reportTimer.Reset(time.Minute)
}
}()
for _, plugin := range enabledPlugins {
plugin.Config.OnEvent(EngineConfig) //引擎初始化完成后,通知插件
}
for {
select {
case event := <-EventBus:
ts := time.Now()
for _, plugin := range enabledPlugins {
ts := time.Now()
plugin.Config.OnEvent(event)
if cost := time.Since(ts); cost > time.Millisecond*100 {
plugin.Warn("event cost too much time", zap.String("event", fmt.Sprintf("%v", event)), zap.Duration("cost", cost))
}
}
EngineConfig.OnEvent(event)
if cost := time.Since(ts); cost > time.Millisecond*100 {
log.Warn("event cost too much time", zap.String("event", fmt.Sprintf("%v", event)), zap.Duration("cost", cost))
}
case <-ctx.Done():
return
}
}
}