浏览代码

Make system work

Florian Hoss 2 年之前
父节点
当前提交
3adbf11558
共有 4 个文件被更改,包括 19 次插入24 次删除
  1. 1 1
      logger.go
  2. 7 3
      main.go
  3. 5 13
      system/system.go
  4. 6 7
      system/types.go

+ 1 - 1
logger.go

@@ -15,7 +15,7 @@ func (g *goDash) setupLogger() {
 		OutputPaths:      []string{"stdout"},
 		OutputPaths:      []string{"stdout"},
 		ErrorOutputPaths: []string{"stderr"},
 		ErrorOutputPaths: []string{"stderr"},
 		EncoderConfig:    zap.NewProductionEncoderConfig(),
 		EncoderConfig:    zap.NewProductionEncoderConfig(),
-	}.Build())
+	}.Build()).Sugar()
 }
 }
 
 
 func (g *goDash) setupEchoLogging() {
 func (g *goDash) setupEchoLogging() {

+ 7 - 3
main.go

@@ -7,6 +7,7 @@ import (
 	"github.com/labstack/echo/v4/middleware"
 	"github.com/labstack/echo/v4/middleware"
 	"go.uber.org/zap"
 	"go.uber.org/zap"
 	"godash/bookmarks"
 	"godash/bookmarks"
+	"godash/system"
 	"godash/weather"
 	"godash/weather"
 	"html/template"
 	"html/template"
 	"net/http"
 	"net/http"
@@ -15,7 +16,7 @@ import (
 
 
 type goDash struct {
 type goDash struct {
 	router *echo.Echo
 	router *echo.Echo
-	logger *zap.Logger
+	logger *zap.SugaredLogger
 	config config
 	config config
 }
 }
 
 
@@ -40,8 +41,11 @@ func main() {
 	defer g.logger.Sync()
 	defer g.logger.Sync()
 	g.setupEchoLogging()
 	g.setupEchoLogging()
 
 
-	w := weather.NewWeatherService(g.logger.Sugar())
-	b := bookmarks.NewBookmarkService(g.logger.Sugar())
+	w := weather.NewWeatherService(g.logger)
+	b := bookmarks.NewBookmarkService(g.logger)
+	if g.config.LiveSystem {
+		system.NewSystemService(g.logger)
+	}
 
 
 	g.router.Use(middleware.Recover())
 	g.router.Use(middleware.Recover())
 	g.router.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 5}))
 	g.router.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 5}))

+ 5 - 13
system/system.go

@@ -1,20 +1,13 @@
 package system
 package system
 
 
 import (
 import (
-	"github.com/sirupsen/logrus"
-	"godash/config"
-	"godash/hub"
+	"go.uber.org/zap"
 	"time"
 	"time"
 )
 )
 
 
-var Config = PackageConfig{}
-var Sys = System{}
-
-func NewSystemService() {
-	config.ParseViperConfig(&Config, config.AddViperConfig("system"))
-	if Config.LiveSystem {
-		Sys.Initialize()
-	}
+func NewSystemService(logging *zap.SugaredLogger) {
+	s := System{log: logging}
+	s.Initialize()
 }
 }
 
 
 func (s *System) UpdateLiveInformation() {
 func (s *System) UpdateLiveInformation() {
@@ -23,7 +16,6 @@ func (s *System) UpdateLiveInformation() {
 		s.liveRam()
 		s.liveRam()
 		s.liveDisk()
 		s.liveDisk()
 		s.uptime()
 		s.uptime()
-		hub.LiveInformationCh <- hub.Message{WsType: hub.System, Message: s.Live}
 		time.Sleep(1 * time.Second)
 		time.Sleep(1 * time.Second)
 	}
 	}
 }
 }
@@ -34,5 +26,5 @@ func (s *System) Initialize() {
 	s.Static.Ram = staticRam()
 	s.Static.Ram = staticRam()
 	s.Static.Disk = staticDisk()
 	s.Static.Disk = staticDisk()
 	go s.UpdateLiveInformation()
 	go s.UpdateLiveInformation()
-	logrus.WithFields(logrus.Fields{"cpu": s.Static.CPU.Name, "arch": s.Static.Host.Architecture}).Debug("system updated")
+	s.log.Debugw("system updated", "cpu", s.Static.CPU.Name, "arch", s.Static.Host.Architecture)
 }
 }

+ 6 - 7
system/types.go

@@ -1,7 +1,11 @@
 package system
 package system
 
 
-type PackageConfig struct {
-	LiveSystem bool `mapstructure:"LIVE_SYSTEM"`
+import "go.uber.org/zap"
+
+type System struct {
+	log    *zap.SugaredLogger
+	Live   LiveInformation   `json:"live"`
+	Static StaticInformation `json:"static"`
 }
 }
 
 
 type LiveStorageInformation struct {
 type LiveStorageInformation struct {
@@ -49,8 +53,3 @@ type StaticInformation struct {
 	Disk Disk `json:"disk"`
 	Disk Disk `json:"disk"`
 	Host Host `json:"host"`
 	Host Host `json:"host"`
 }
 }
-
-type System struct {
-	Live   LiveInformation   `json:"live"`
-	Static StaticInformation `json:"static"`
-}