From 3863b4ecadd2128e900040a78f01ee45084fa255 Mon Sep 17 00:00:00 2001 From: YSTYLE Date: Wed, 11 Mar 2020 01:36:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E4=B8=8D=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=9A=84=E6=89=93=E5=8D=B0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 103 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 44 deletions(-) diff --git a/main.go b/main.go index 1bee11e..d8d77ca 100644 --- a/main.go +++ b/main.go @@ -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" { @@ -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()) @@ -105,6 +77,7 @@ func main() { e.Static("/download", "storage") e.GET("/ws", WS) e.GET("/ws#", WS) + e.GET("ping", ping) // 打印服务器负载 go PrintStatistics() @@ -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") +}