This commit is contained in:
2023-10-23 16:53:47 +08:00
3 changed files with 15 additions and 83 deletions

View File

@@ -25,7 +25,6 @@ type SystemInfoController struct {
// GET /
func (s *SystemInfoController) Info(c *gin.Context) {
c.JSON(200, result.OkData(map[string]any{
"project": s.systemInfogService.ProjectInfo(),
"cpu": s.systemInfogService.CPUInfo(),
"memory": s.systemInfogService.MemoryInfo(),
"network": s.systemInfogService.NetworkInfo(),

View File

@@ -2,9 +2,6 @@ package service
// ISystemInfo 服务器系统相关信息 服务层接口
type ISystemInfo interface {
// ProjectInfo 程序项目信息
ProjectInfo() map[string]any
// SystemInfo 系统信息
SystemInfo() map[string]any

View File

@@ -1,7 +1,6 @@
package service
import (
"bufio"
"fmt"
"os"
"runtime"
@@ -24,101 +23,38 @@ var NewSystemInfoImpl = &SystemInfoImpl{}
// SystemInfoImpl 服务器系统相关信息 服务层处理
type SystemInfoImpl struct{}
// ProjectInfo 程序项目信息
func (s *SystemInfoImpl) ProjectInfo() map[string]any {
// 获取工作目录
appDir, err := os.Getwd()
if err != nil {
appDir = ""
}
// 项目依赖
dependencies := s.dependencies()
return map[string]any{
"appDir": appDir,
"env": config.Env(),
"name": config.Get("framework.name"),
"version": config.Get("framework.version"),
"dependencies": dependencies,
}
}
// dependencies 读取mod内项目包依赖
func (s *SystemInfoImpl) dependencies() map[string]string {
var pkgs = make(map[string]string)
// 打开 go.mod 文件
file, err := os.Open("go.mod")
if err != nil {
return pkgs
}
defer file.Close()
// 使用 bufio.Scanner 逐行读取文件内容
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
// 行不为空不以module\require开头不带有 // indirect 注释,则解析包名和版本
prefixLine := strings.HasPrefix(line, "module") || strings.HasPrefix(line, "require") || strings.HasPrefix(line, "go ")
suffixLine := strings.HasSuffix(line, ")") || strings.HasSuffix(line, "// indirect")
if line == "" || prefixLine || suffixLine {
continue
}
modInfo := strings.Split(line, " ")
if len(modInfo) >= 2 {
moduleName := strings.TrimSpace(modInfo[0])
version := strings.TrimSpace(modInfo[1])
pkgs[moduleName] = version
}
}
if err := scanner.Err(); err != nil {
pkgs["scanner-err"] = err.Error()
}
return pkgs
}
// SystemInfo 系统信息
func (s *SystemInfoImpl) SystemInfo() map[string]any {
info, err := host.Info()
if err != nil {
info.Platform = err.Error()
}
// 用户目录
homeDir, err := os.UserHomeDir()
if err != nil {
homeDir = ""
}
cmd, err := os.Executable()
if err != nil {
cmd = ""
}
bootTime := time.Since(time.Unix(int64(info.BootTime), 0)).String()
return map[string]any{
"platform": info.Platform,
"go": runtime.Version(),
"processId": os.Getpid(),
"arch": info.KernelArch,
"uname": runtime.GOARCH,
"release": info.OS,
"hostname": info.Hostname,
"homeDir": homeDir,
"cmd": cmd,
"execCommand": strings.Join(os.Args, " "),
"platform": info.Platform,
"platformVersion": info.PlatformVersion,
"arch": info.KernelArch,
"archVersion": info.KernelVersion,
"os": info.OS,
"hostname": info.Hostname,
"bootTime": bootTime,
"processId": os.Getpid(),
"runArch": runtime.GOARCH,
"runVersion": runtime.Version(),
}
}
// TimeInfo 系统时间信息
func (s *SystemInfoImpl) TimeInfo() map[string]string {
now := time.Now()
// 获取当前时间
current := time.Now().Format("2006-01-02 15:04:05")
current := now.Format("2006-01-02 15:04:05")
// 获取程序运行时间
uptime := time.Since(config.RunTime()).String()
// 获取时区
timezone := time.Now().Format("-0700 MST")
timezone := now.Format("-0700 MST")
// 获取时区名称
timezoneName := time.Now().Format("MST")
timezoneName := now.Format("MST")
return map[string]string{
"current": current,