Skip to content

Commit

Permalink
增加日志记录
Browse files Browse the repository at this point in the history
  • Loading branch information
nyancatda committed Jun 20, 2022
1 parent d42aa03 commit de7e6ce
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 73 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test
*_test.go

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

/logs
45 changes: 39 additions & 6 deletions Routers.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/*
* @Author: NyanCatda
* @Date: 2022-06-20 13:12:12
* @LastEditTime: 2022-06-20 13:17:02
* @LastEditTime: 2022-06-20 13:42:28
* @LastEditors: NyanCatda
* @Description: 路由注册
* @FilePath: \MCBE-Server-Motd\Routers.go
*/
package main

import (
"fmt"
"net/http"
"time"

Expand All @@ -18,6 +17,7 @@ import (
cache "github.com/chenyahui/gin-cache"
"github.com/chenyahui/gin-cache/persist"
"github.com/gin-gonic/gin"
"github.com/nyancatda/AyaLog"
)

/**
Expand All @@ -26,6 +26,9 @@ import (
* @return {*gin.Engine} gin引擎
*/
func SetupRouter(r *gin.Engine) *gin.Engine {
// 500错误处理
r.Use(ServerError)

// 注册静态资源
r.Static("/static", "./fronend/dist/static")

Expand All @@ -51,7 +54,7 @@ func SetupRouter(r *gin.Engine) *gin.Engine {

data, err := MotdBEAPI.MotdBE(Host)
if err != nil {
fmt.Println(err)
AyaLog.DeBug("Request", err)
}
c.JSON(http.StatusOK, data)
})
Expand All @@ -63,7 +66,7 @@ func SetupRouter(r *gin.Engine) *gin.Engine {

data, err := MotdBEAPI.MotdJava(Host)
if err != nil {
fmt.Println(err)
AyaLog.DeBug("Request", err)
}
c.JSON(http.StatusOK, data)
})
Expand All @@ -75,17 +78,47 @@ func SetupRouter(r *gin.Engine) *gin.Engine {
r.GET("/status_img", cache.CacheByRequestURI(memoryStore, 10*time.Second), func(c *gin.Context) {
Host := c.Query("host")

Img := StatusImg.ServerStatusImg(Host)
Img, err := StatusImg.ServerStatusImg(Host)
if err != nil {
AyaLog.Error("Request", err)
c.String(http.StatusInternalServerError, "Server Error")
return
}
c.String(http.StatusOK, Img.String())
})

// Java版服务器状态图片
r.GET("/status_img/java", cache.CacheByRequestURI(memoryStore, 10*time.Second), func(c *gin.Context) {
Host := c.Query("host")

Img := StatusImg.ServerStatusImgJava(Host)
Img, err := StatusImg.ServerStatusImgJava(Host)
if err != nil {
AyaLog.Error("Request", err)
c.String(http.StatusInternalServerError, "Server Error")
return
}
c.String(http.StatusOK, Img.String())
})

return r
}

/**
* @description: 500错误处理
* @param {*gin.Context} c
* @return {*}
*/
func ServerError(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
// 打印错误堆栈信息
AyaLog.Error("Request", r.(error))

// 500返回
c.String(http.StatusInternalServerError, "Server Error")
c.Abort()
return
}
}()
c.Next()
}
27 changes: 13 additions & 14 deletions StatusImg/StatusImg.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: NyanCatda
* @Date: 2021-12-26 21:23:59
* @LastEditTime: 2022-06-20 13:18:32
* @LastEditTime: 2022-06-20 13:28:28
* @LastEditors: NyanCatda
* @Description: 服务器状态图片生成
* @FilePath: \MCBE-Server-Motd\StatusImg\StatusImg.go
Expand All @@ -10,7 +10,6 @@ package StatusImg

import (
"bytes"
"fmt"
"image"
"image/color"
"image/png"
Expand All @@ -28,38 +27,38 @@ import (
* @param {string} Host 服务器地址
* @return {*bytes.Buffer} 图片Buffer
*/
func ServerStatusImg(Host string) *bytes.Buffer {
func ServerStatusImg(Host string) (*bytes.Buffer, error) {
//获取服务器信息
ServerData, err := MotdBEAPI.MotdBE(Host)
if err != nil {
fmt.Println(err)
return nil, err
}
if ServerData.Status == "offline" {
offlineImgFile, err := os.Open("StatusImg/background.png")
if err != nil {
fmt.Println(err)
return nil, err
}
offlineImg, err := png.Decode(offlineImgFile)
if err != nil {
fmt.Println(err)
return nil, err
}
//将图片写入Buffer
Buff := bytes.NewBuffer(nil)
err = png.Encode(Buff, offlineImg)
if err != nil {
fmt.Println(err)
return nil, err
}
return Buff
return Buff, nil
}

//读取背景图片
backgroundFile, err := os.Open("StatusImg/background.png")
if err != nil {
fmt.Println(err)
return nil, err
}
backgroundImg, err := png.Decode(backgroundFile)
if err != nil {
fmt.Println(err)
return nil, err
}

//转换类型
Expand All @@ -68,11 +67,11 @@ func ServerStatusImg(Host string) *bytes.Buffer {
//读取字体数据
fontBytes, err := ioutil.ReadFile("StatusImg/unifont-12.1.04.ttf")
if err != nil {
fmt.Println(err)
return nil, err
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
fmt.Println(err)
return nil, err
}

//设置标题字体
Expand Down Expand Up @@ -121,9 +120,9 @@ func ServerStatusImg(Host string) *bytes.Buffer {
Buff := bytes.NewBuffer(nil)
err = png.Encode(Buff, img)
if err != nil {
fmt.Println(err)
return nil, err
}
return Buff
return Buff, nil
}

/**
Expand Down
28 changes: 14 additions & 14 deletions StatusImg/StatusImgJava.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: NyanCatda
* @Date: 2021-12-26 21:23:59
* @LastEditTime: 2022-06-20 13:18:42
* @LastEditTime: 2022-06-20 13:28:07
* @LastEditors: NyanCatda
* @Description: Java服务器状态图片生成
* @FilePath: \MCBE-Server-Motd\StatusImg\StatusImgJava.go
Expand All @@ -11,7 +11,6 @@ package StatusImg
import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/color"
"image/draw"
Expand All @@ -29,39 +28,40 @@ import (
* @description: Java服务器状态图片生成
* @param {string} Host 服务器地址
* @return {*bytes.Buffer} 图片Buffer
* @return {error} 错误
*/
func ServerStatusImgJava(Host string) *bytes.Buffer {
func ServerStatusImgJava(Host string) (*bytes.Buffer, error) {
//获取服务器信息
ServerData, err := MotdBEAPI.MotdJava(Host)
if err != nil {
fmt.Println(err)
return nil, err
}
if ServerData.Status == "offline" {
offlineImgFile, err := os.Open("StatusImg/background.png")
if err != nil {
fmt.Println(err)
return nil, err
}
offlineImg, err := png.Decode(offlineImgFile)
if err != nil {
fmt.Println(err)
return nil, err
}
//将图片写入Buffer
Buff := bytes.NewBuffer(nil)
err = png.Encode(Buff, offlineImg)
if err != nil {
fmt.Println(err)
return nil, err
}
return Buff
return Buff, nil
}

//读取背景图片
backgroundFile, err := os.Open("StatusImg/background.png")
if err != nil {
fmt.Println(err)
return nil, err
}
backgroundImg, err := png.Decode(backgroundFile)
if err != nil {
fmt.Println(err)
return nil, err
}

//转换类型
Expand All @@ -70,11 +70,11 @@ func ServerStatusImgJava(Host string) *bytes.Buffer {
//读取字体数据
fontBytes, err := ioutil.ReadFile("StatusImg/unifont-12.1.04.ttf")
if err != nil {
fmt.Println(err)
return nil, err
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
fmt.Println(err)
return nil, err
}

//设置标题字体
Expand Down Expand Up @@ -128,7 +128,7 @@ func ServerStatusImgJava(Host string) *bytes.Buffer {
Buff := bytes.NewBuffer(nil)
err = png.Encode(Buff, img)
if err != nil {
fmt.Println(err)
return nil, err
}
return Buff
return Buff, nil
}
35 changes: 21 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ module github.com/BlackBEDevelopment/MCBE-Server-Motd

go 1.17

require github.com/gin-gonic/gin v1.7.7
require (
github.com/gin-gonic/gin v1.8.1
github.com/nyancatda/AyaLog v1.0.5
github.com/nyancatda/AyaLog/ModLog/GinLog v0.0.0-20220606093827-834efb2bcb30
)

require (
github.com/BlackBEDevelopment/MCBE-Server-Motd/MotdBEAPI v0.0.0-20211228052232-4fa847734aff
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20210423082822-04245dca01da // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

Expand All @@ -33,8 +36,12 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-redis/redis/v8 v8.11.4 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
golang.org/x/net v0.0.0-20220617184016-355a448f1bc9 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
google.golang.org/protobuf v1.26.0 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
)

retract v1.0.0
Loading

0 comments on commit de7e6ce

Please sign in to comment.