sysInfo增加LocalIP字段

This commit is contained in:
dexter
2022-07-17 09:36:27 +08:00
parent ec5f8b0214
commit 935cd73f0d
2 changed files with 16 additions and 19 deletions

18
http.go
View File

@@ -2,7 +2,6 @@ package engine
import (
"encoding/json"
"net"
"net/http"
"time"
@@ -51,22 +50,7 @@ func (conf *GlobalConfig) API_stream(rw http.ResponseWriter, r *http.Request) {
}
func (conf *GlobalConfig) API_sysInfo(rw http.ResponseWriter, r *http.Request) {
var IP []string
if addrs, err := net.InterfaceAddrs(); err == nil {
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
IP = append(IP, ipnet.IP.String())
}
}
}
}
if err := json.NewEncoder(rw).Encode(&struct {
Version string
StartTime string
IP []string
}{Engine.Version, StartTime.Format("2006-01-02 15:04:05"), IP}); err != nil {
if err := json.NewEncoder(rw).Encode(&SysInfo); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}

17
main.go
View File

@@ -5,10 +5,12 @@ import (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/google/uuid"
@@ -21,11 +23,15 @@ import (
)
var (
SysInfo struct {
StartTime time.Time //启动时间
LocalIP string
Version string
}
ExecPath = os.Args[0]
ExecDir = filepath.Dir(ExecPath)
// ConfigRaw 配置信息的原始数据
ConfigRaw []byte
StartTime time.Time //启动时间
Plugins = make(map[string]*Plugin) // Plugins 所有的插件配置
EngineConfig = &GlobalConfig{
Engine: config.Global,
@@ -37,9 +43,16 @@ var (
apiList []string //注册到引擎的API接口列表
)
func init() {
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, configFile string) (err error) {
StartTime = time.Now()
SysInfo.StartTime = time.Now()
SysInfo.Version = Engine.Version
Engine.Context = ctx
if _, err := os.Stat(configFile); err != nil {
configFile = filepath.Join(ExecDir, configFile)