11package main
22
33import (
4+ "encoding/json"
45 "fmt"
6+ "github.com/gin-gonic/gin"
57 "io/ioutil"
8+ "net/http"
69 "os"
7- "encoding/json"
8- "net/http"
9- "github.com/gin-gonic/gin"
1010 "time"
1111)
1212
1313type Config struct {
14- ServerName string `json:"servername"`
15- IP string `json:"ip"`
16- Port string `json:"port"`
17- Introduced string `json:"introduced"`
14+ ServerName string `json:"servername"`
15+ IP string `json:"ip"`
16+ Port string `json:"port"`
17+ Introduced string `json:"introduced"`
1818 QQGrouplink string `json:"qqgrouplink"`
19- Email string `json:"email"`
20- ServerPort int `json:"serverport"`
19+ Email string `json:"email"`
20+ ServerPort int `json:"serverport"`
2121}
2222
2323type MotdBEJson struct {
24- Status string `json:"status"`
25- IP string `json:"ip "`
26- Port string `json:"port "`
27- Motd string `json:"motd "`
28- Agreement string `json:"agreement "`
29- Version string `json:"version "`
30- Online string `json:"online "`
31- Max string `json:"max "`
32- Gamemode string `json:"gamemode"`
33- Delay int `json:"delay"`
24+ Status string `json:"status"`
25+ Host string `json:"host "`
26+ Motd string `json:"motd "`
27+ Agreement int `json:"agreement "`
28+ Version string `json:"version "`
29+ Online int `json:"online "`
30+ Max int `json:"max "`
31+ Level_name string `json:"level_name "`
32+ Gamemode string `json:"gamemode"`
33+ Delay int `json:"delay"`
3434}
3535
3636//读取配置文件
37- func getConfig ()( Config ) {
37+ func getConfig () Config {
3838 jsonFile , err := os .Open ("config.json" )
3939 if err != nil {
4040 fmt .Println (err )
@@ -48,61 +48,60 @@ func getConfig()(Config){
4848
4949//请求MotdPE API
5050//https://wiki.blackbe.xyz/api/motd.html
51- func getMotdBE (ip string ,port string )( MotdBEJson ) {
52- url := "http ://motdpe .blackbe.xyz/api.php?ip=" + ip + "&port=" + port
53- client := http.Client {Timeout : 10 * time .Second }//设置10秒超时
51+ func getMotdBE (ip string , port string ) MotdBEJson {
52+ url := "https ://motdbe .blackbe.xyz/api?host=" + ip + ":" + port
53+ client := http.Client {Timeout : 10 * time .Second } //设置10秒超时
5454 res , err := client .Get (url )
55- if err != nil {
56- fmt .Fprintf (os .Stderr , "fetch: %v\n " , err )
57- }
58- body , err := ioutil .ReadAll (res .Body )
59- res .Body .Close ()
60- if err != nil {
61- fmt .Fprintf (os .Stderr , "fetch: reading %s: %v\n " , url , err )
62- }
63- var config MotdBEJson
55+ if err != nil {
56+ fmt .Fprintf (os .Stderr , "fetch: %v\n " , err )
57+ }
58+ body , err := ioutil .ReadAll (res .Body )
59+ res .Body .Close ()
60+ if err != nil {
61+ fmt .Fprintf (os .Stderr , "fetch: reading %s: %v\n " , url , err )
62+ }
63+ var config MotdBEJson
6464 json .Unmarshal ([]byte (body ), & config )
65- return config
65+ return config
6666}
6767
68-
6968func main () {
7069 gin .SetMode (gin .ReleaseMode )
71- r := gin .Default ()
70+ r := gin .Default ()
7271
73- r .Static ("/public" , "./public" )//定义静态资源目录
72+ r .Static ("/public" , "./public" ) //定义静态资源目录
7473 r .LoadHTMLGlob ("assets/**/*" )
7574 ServerPort := fmt .Sprintf ("%d" , getConfig ().ServerPort )
76- fmt .Println ("网站已运行在 " + ServerPort + " 端口" )
75+ fmt .Println ("网站已运行在 " + ServerPort + " 端口" )
7776
78- r .GET ("/" , func (c * gin.Context ) {
77+ r .GET ("/" , func (c * gin.Context ) {
7978 Config := getConfig ()
80- ServerInfo := getMotdBE (Config .IP ,Config .Port )
79+ ServerInfo := getMotdBE (Config .IP , Config .Port )
8180
8281 var Status string
8382 var Status_bool bool
84- if ServerInfo .Status == "online" {
83+ if ServerInfo .Status == "online" {
8584 Status = "在线"
8685 Status_bool = true
8786 } else {
8887 Status = "离线"
89- Status_bool = false
88+ Status_bool = false
9089 }
9190
92- c .HTML (http .StatusOK , "index/index.html" , gin.H {
93- "servername" : Config .ServerName ,
94- "ip" : Config .IP ,
95- "port" : Config .Port ,
96- "introduced" : Config .Introduced ,
91+ c .HTML (http .StatusOK , "index/index.html" , gin.H {
92+ "servername" : Config .ServerName ,
93+ "ip" : Config .IP ,
94+ "port" : Config .Port ,
95+ "introduced" : Config .Introduced ,
9796 "qqgrouplink" : Config .QQGrouplink ,
98- "email" : Config .Email ,
99- "status" : Status ,
97+ "email" : Config .Email ,
98+ "status" : Status ,
10099 "status_bool" : Status_bool ,
101- "online" :ServerInfo .Online ,
102- "max" :ServerInfo .Max ,
103- "delay" :ServerInfo .Delay ,
104- "version" :ServerInfo .Version })
105- })
100+ "online" : ServerInfo .Online ,
101+ "max" : ServerInfo .Max ,
102+ "delay" : ServerInfo .Delay ,
103+ "version" : ServerInfo .Version })
104+ })
106105
107- r .Run (":" + ServerPort )
108- }
106+ r .Run (":" + ServerPort )
107+ }
0 commit comments