Code cleanup and add exported function comments
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/mcstatus-io/mcutil"
|
||||
)
|
||||
|
||||
// StatusResponse is the root response for returning any status response from the API.
|
||||
type StatusResponse struct {
|
||||
Online bool `json:"online"`
|
||||
Host string `json:"host"`
|
||||
@@ -19,11 +20,13 @@ type StatusResponse struct {
|
||||
ExpiresAt int64 `json:"expires_at"`
|
||||
}
|
||||
|
||||
// JavaStatusResponse is the combined response of the root response and the Java Edition status response.
|
||||
type JavaStatusResponse struct {
|
||||
StatusResponse
|
||||
*JavaStatus
|
||||
}
|
||||
|
||||
// JavaStatus is the status response properties for Java Edition.
|
||||
type JavaStatus struct {
|
||||
Version *JavaVersion `json:"version"`
|
||||
Players JavaPlayers `json:"players"`
|
||||
@@ -32,11 +35,13 @@ type JavaStatus struct {
|
||||
Mods []Mod `json:"mods"`
|
||||
}
|
||||
|
||||
// BedrockStatusResponse is the combined response of the root response and the Bedrock Edition status response.
|
||||
type BedrockStatusResponse struct {
|
||||
StatusResponse
|
||||
*BedrockStatus
|
||||
}
|
||||
|
||||
// BedrockStatus is the status response properties for Bedrock Edition.
|
||||
type BedrockStatus struct {
|
||||
Version *BedrockVersion `json:"version"`
|
||||
Players *BedrockPlayers `json:"players"`
|
||||
@@ -46,29 +51,34 @@ type BedrockStatus struct {
|
||||
Edition *string `json:"edition"`
|
||||
}
|
||||
|
||||
// JavaVersion holds the properties for the version of Java Edition responses.
|
||||
type JavaVersion struct {
|
||||
NameRaw string `json:"name_raw"`
|
||||
NameClean string `json:"name_clean"`
|
||||
NameHTML string `json:"name_html"`
|
||||
Protocol int `json:"protocol"`
|
||||
Protocol int64 `json:"protocol"`
|
||||
}
|
||||
|
||||
// BedrockVersion holds the properties for the version of Bedrock Edition responses.
|
||||
type BedrockVersion struct {
|
||||
Name *string `json:"name"`
|
||||
Protocol *int64 `json:"protocol"`
|
||||
}
|
||||
|
||||
// JavaPlayers holds the properties for the players of Java Edition responses.
|
||||
type JavaPlayers struct {
|
||||
Online int `json:"online"`
|
||||
Max int `json:"max"`
|
||||
Online *int64 `json:"online"`
|
||||
Max *int64 `json:"max"`
|
||||
List []Player `json:"list"`
|
||||
}
|
||||
|
||||
// BedrockPlayers holds the properties for the players of Bedrock Edition responses.
|
||||
type BedrockPlayers struct {
|
||||
Online *int64 `json:"online"`
|
||||
Max *int64 `json:"max"`
|
||||
}
|
||||
|
||||
// Player is a single sample player used in Java Edition status responses.
|
||||
type Player struct {
|
||||
UUID string `json:"uuid"`
|
||||
NameRaw string `json:"name_raw"`
|
||||
@@ -76,17 +86,20 @@ type Player struct {
|
||||
NameHTML string `json:"name_html"`
|
||||
}
|
||||
|
||||
// MOTD is a group of formatted and unformatted properties for status responses.
|
||||
type MOTD struct {
|
||||
Raw string `json:"raw"`
|
||||
Clean string `json:"clean"`
|
||||
HTML string `json:"html"`
|
||||
}
|
||||
|
||||
// Mod is a single Forge mod installed on any Java Edition status response.
|
||||
type Mod struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// GetJavaStatus returns the status response of a Java Edition server, either using cache or fetching a fresh status.
|
||||
func GetJavaStatus(host string, port uint16) (*JavaStatusResponse, time.Duration, error) {
|
||||
cacheKey := fmt.Sprintf("java:%s-%d", host, port)
|
||||
|
||||
@@ -116,13 +129,14 @@ func GetJavaStatus(host string, port uint16) (*JavaStatusResponse, time.Duration
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if err := r.Set(cacheKey, data, config.Cache.JavaStatusDuration); err != nil {
|
||||
if err := r.Set(cacheKey, data, conf.Cache.JavaStatusDuration); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return response, 0, nil
|
||||
}
|
||||
|
||||
// GetBedrockStatus returns the status response of a Bedrock Edition server, either using cache or fetching a fresh status.
|
||||
func GetBedrockStatus(host string, port uint16) (*BedrockStatusResponse, time.Duration, error) {
|
||||
cacheKey := fmt.Sprintf("bedrock:%s-%d", host, port)
|
||||
|
||||
@@ -152,13 +166,14 @@ func GetBedrockStatus(host string, port uint16) (*BedrockStatusResponse, time.Du
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if err := r.Set(cacheKey, data, config.Cache.BedrockStatusDuration); err != nil {
|
||||
if err := r.Set(cacheKey, data, conf.Cache.BedrockStatusDuration); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return response, 0, nil
|
||||
}
|
||||
|
||||
// GetServerIcon returns the icon image of a Java Edition server, either using cache or fetching a fresh image.
|
||||
func GetServerIcon(host string, port uint16) ([]byte, time.Duration, error) {
|
||||
cacheKey := fmt.Sprintf("icon:%s-%d", host, port)
|
||||
|
||||
@@ -186,13 +201,14 @@ func GetServerIcon(host string, port uint16) ([]byte, time.Duration, error) {
|
||||
icon = data
|
||||
}
|
||||
|
||||
if err := r.Set(cacheKey, icon, config.Cache.IconDuration); err != nil {
|
||||
if err := r.Set(cacheKey, icon, conf.Cache.IconDuration); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return icon, 0, nil
|
||||
}
|
||||
|
||||
// FetchJavaStatus fetches a fresh status of a Java Edition server.
|
||||
func FetchJavaStatus(host string, port uint16) (*JavaStatusResponse, error) {
|
||||
status, err := mcutil.Status(host, port)
|
||||
|
||||
@@ -207,7 +223,7 @@ func FetchJavaStatus(host string, port uint16) (*JavaStatusResponse, error) {
|
||||
Port: port,
|
||||
EULABlocked: IsBlockedAddress(host),
|
||||
RetrievedAt: time.Now().UnixMilli(),
|
||||
ExpiresAt: time.Now().Add(config.Cache.JavaStatusDuration).UnixMilli(),
|
||||
ExpiresAt: time.Now().Add(conf.Cache.JavaStatusDuration).UnixMilli(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -219,13 +235,13 @@ func FetchJavaStatus(host string, port uint16) (*JavaStatusResponse, error) {
|
||||
Port: port,
|
||||
EULABlocked: IsBlockedAddress(host),
|
||||
RetrievedAt: time.Now().UnixMilli(),
|
||||
ExpiresAt: time.Now().Add(config.Cache.JavaStatusDuration).UnixMilli(),
|
||||
ExpiresAt: time.Now().Add(conf.Cache.JavaStatusDuration).UnixMilli(),
|
||||
},
|
||||
JavaStatus: &JavaStatus{
|
||||
Version: nil,
|
||||
Players: JavaPlayers{
|
||||
Online: statusLegacy.Players.Online,
|
||||
Max: statusLegacy.Players.Max,
|
||||
Online: &statusLegacy.Players.Online,
|
||||
Max: &statusLegacy.Players.Max,
|
||||
List: make([]Player, 0),
|
||||
},
|
||||
MOTD: MOTD{
|
||||
@@ -281,7 +297,7 @@ func FetchJavaStatus(host string, port uint16) (*JavaStatusResponse, error) {
|
||||
Port: port,
|
||||
EULABlocked: IsBlockedAddress(host),
|
||||
RetrievedAt: time.Now().UnixMilli(),
|
||||
ExpiresAt: time.Now().Add(config.Cache.JavaStatusDuration).UnixMilli(),
|
||||
ExpiresAt: time.Now().Add(conf.Cache.JavaStatusDuration).UnixMilli(),
|
||||
},
|
||||
JavaStatus: &JavaStatus{
|
||||
Version: &JavaVersion{
|
||||
@@ -306,6 +322,7 @@ func FetchJavaStatus(host string, port uint16) (*JavaStatusResponse, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FetchBedrockStatus fetches a fresh status of a Bedrock Edition server.
|
||||
func FetchBedrockStatus(host string, port uint16) (*BedrockStatusResponse, error) {
|
||||
status, err := mcutil.StatusBedrock(host, port)
|
||||
|
||||
@@ -317,7 +334,7 @@ func FetchBedrockStatus(host string, port uint16) (*BedrockStatusResponse, error
|
||||
Port: port,
|
||||
EULABlocked: IsBlockedAddress(host),
|
||||
RetrievedAt: time.Now().UnixMilli(),
|
||||
ExpiresAt: time.Now().Add(config.Cache.BedrockStatusDuration).UnixMilli(),
|
||||
ExpiresAt: time.Now().Add(conf.Cache.BedrockStatusDuration).UnixMilli(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -329,7 +346,7 @@ func FetchBedrockStatus(host string, port uint16) (*BedrockStatusResponse, error
|
||||
Port: port,
|
||||
EULABlocked: IsBlockedAddress(host),
|
||||
RetrievedAt: time.Now().UnixMilli(),
|
||||
ExpiresAt: time.Now().Add(config.Cache.BedrockStatusDuration).UnixMilli(),
|
||||
ExpiresAt: time.Now().Add(conf.Cache.BedrockStatusDuration).UnixMilli(),
|
||||
},
|
||||
BedrockStatus: &BedrockStatus{
|
||||
Version: nil,
|
||||
|
||||
Reference in New Issue
Block a user