Skip to content

Commit

Permalink
去掉不必要的打印日志
Browse files Browse the repository at this point in the history
  • Loading branch information
ystyle committed Mar 10, 2020
1 parent 2a0b855 commit 3863b4e
Showing 1 changed file with 59 additions and 44 deletions.
103 changes: 59 additions & 44 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,29 @@ import (
"os"
"path"
"runtime"
"strings"
"time"
)

var upgrader = websocket.Upgrader{}

func WS(c echo.Context) error {
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
if err != nil {
return err
}
wm := core.GetWsManager()
wm.Add(core.NewWsClient(ws, c.Request()))
return nil
}
var (
upgrader = websocket.Upgrader{}
loggerConfig = middleware.LoggerWithConfig(middleware.LoggerConfig{
Skipper: func(c echo.Context) bool {
if c.Path() == "/ping" {
return true
}
if strings.HasPrefix(c.Path(), "/asset/") {
return true
}
return false
},
})
)

func init() {
createStoreDir()
}

func createStoreDir() {
file.CheckDir(config.StoreDir)
}

func PrintStatistics() {
wm := core.GetWsManager()
timer := time.NewTimer(time.Second * 5)
i := 0
for {
select {
case <-timer.C:
timer.Reset(time.Second * 5)
clients := len(wm.GetClients())
var drives []model.Drive
err := model.DB().All(&drives)
if err != nil {
log.Error(err)
continue
}
var count uint
for _, drive := range drives {
count += drive.Count
}
// 连接有变动时就打印
if clients != i && err == nil {
fmt.Printf("注册设备: %d, 总计转换次数为: %d 当前连接数为: %d\n", len(drives), count, clients)
}
i = clients
}
}
}

func main() {
log.EnableColor()
if os.Getenv("MODE") == "DEBUG" {
Expand All @@ -91,7 +63,7 @@ func main() {
wm.RegisterService("regsiter", services.Register)

e := echo.New()
e.Use(middleware.Logger())
e.Use(loggerConfig)
e.Use(middleware.Recover())
e.Use(middleware.RequestID())
e.Use(middleware.Gzip())
Expand All @@ -105,6 +77,7 @@ func main() {
e.Static("/download", "storage")
e.GET("/ws", WS)
e.GET("/ws#", WS)
e.GET("ping", ping)

// 打印服务器负载
go PrintStatistics()
Expand All @@ -115,3 +88,45 @@ func main() {
}
e.Logger.Fatal(e.Start(":1323"))
}

func WS(c echo.Context) error {
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
if err != nil {
return err
}
wm := core.GetWsManager()
wm.Add(core.NewWsClient(ws, c.Request()))
return nil
}

func PrintStatistics() {
wm := core.GetWsManager()
timer := time.NewTimer(time.Second * 5)
i := 0
for {
select {
case <-timer.C:
timer.Reset(time.Second * 5)
clients := len(wm.GetClients())
var drives []model.Drive
err := model.DB().All(&drives)
if err != nil {
log.Error(err)
continue
}
var count uint
for _, drive := range drives {
count += drive.Count
}
// 连接有变动时就打印
if clients != i && err == nil {
fmt.Printf("注册设备: %d, 总计转换次数为: %d 当前连接数为: %d\n", len(drives), count, clients)
}
i = clients
}
}
}

func ping(c echo.Context) error {
return c.String(http.StatusOK, "pong")
}

0 comments on commit 3863b4e

Please sign in to comment.