mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-23 08:03:51 +01:00
refactor: extract gametime package, replace fmt.Printf with zap logging
Move time utilities (TimeAdjusted, TimeMidnight, TimeWeekStart, TimeWeekNext, TimeGameAbsolute) from channelserver into common/gametime to break the inappropriate dependency where signserver, entranceserver, and api imported the 38K-line channelserver package just for time functions. Replace all fmt.Printf debug logging in sys_session.go and handlers_object.go with structured zap logging for consistent observability.
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
_config "erupe-ce/config"
|
||||
"erupe-ce/server/channelserver"
|
||||
"erupe-ce/common/gametime"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
@@ -77,7 +77,7 @@ type ExportData struct {
|
||||
|
||||
func (s *APIServer) newAuthData(userID uint32, userRights uint32, userTokenID uint32, userToken string, characters []Character) AuthData {
|
||||
resp := AuthData{
|
||||
CurrentTS: uint32(channelserver.TimeAdjusted().Unix()),
|
||||
CurrentTS: uint32(gametime.Adjusted().Unix()),
|
||||
ExpiryTS: uint32(s.getReturnExpiry(userID).Unix()),
|
||||
EntranceCount: 1,
|
||||
User: User{
|
||||
@@ -99,9 +99,9 @@ func (s *APIServer) newAuthData(userID uint32, userRights uint32, userTokenID ui
|
||||
stalls[4] = 2
|
||||
}
|
||||
resp.MezFes = &MezFes{
|
||||
ID: uint32(channelserver.TimeWeekStart().Unix()),
|
||||
Start: uint32(channelserver.TimeWeekStart().Add(-time.Duration(s.erupeConfig.GameplayOptions.MezFesDuration) * time.Second).Unix()),
|
||||
End: uint32(channelserver.TimeWeekNext().Unix()),
|
||||
ID: uint32(gametime.WeekStart().Unix()),
|
||||
Start: uint32(gametime.WeekStart().Add(-time.Duration(s.erupeConfig.GameplayOptions.MezFesDuration) * time.Second).Unix()),
|
||||
End: uint32(gametime.WeekNext().Unix()),
|
||||
SoloTickets: s.erupeConfig.GameplayOptions.MezFesSoloTickets,
|
||||
GroupTickets: s.erupeConfig.GameplayOptions.MezFesGroupTickets,
|
||||
Stalls: stalls,
|
||||
@@ -118,7 +118,7 @@ func (s *APIServer) Launcher(w http.ResponseWriter, r *http.Request) {
|
||||
respData.Messages = s.erupeConfig.API.Messages
|
||||
respData.Links = s.erupeConfig.API.Links
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(respData)
|
||||
_ = json.NewEncoder(w).Encode(respData)
|
||||
}
|
||||
|
||||
func (s *APIServer) Login(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -140,7 +140,7 @@ func (s *APIServer) Login(w http.ResponseWriter, r *http.Request) {
|
||||
err := s.db.QueryRow("SELECT id, password, rights FROM users WHERE username = $1", reqData.Username).Scan(&userID, &password, &userRights)
|
||||
if err == sql.ErrNoRows {
|
||||
w.WriteHeader(400)
|
||||
w.Write([]byte("username-error"))
|
||||
_, _ = w.Write([]byte("username-error"))
|
||||
return
|
||||
} else if err != nil {
|
||||
s.logger.Warn("SQL query error", zap.Error(err))
|
||||
@@ -149,7 +149,7 @@ func (s *APIServer) Login(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if bcrypt.CompareHashAndPassword([]byte(password), []byte(reqData.Password)) != nil {
|
||||
w.WriteHeader(400)
|
||||
w.Write([]byte("password-error"))
|
||||
_, _ = w.Write([]byte("password-error"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ func (s *APIServer) Login(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
respData := s.newAuthData(userID, userRights, userTokenID, userToken, characters)
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(respData)
|
||||
_ = json.NewEncoder(w).Encode(respData)
|
||||
}
|
||||
|
||||
func (s *APIServer) Register(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -194,7 +194,7 @@ func (s *APIServer) Register(w http.ResponseWriter, r *http.Request) {
|
||||
var pqErr *pq.Error
|
||||
if errors.As(err, &pqErr) && pqErr.Constraint == "users_username_key" {
|
||||
w.WriteHeader(400)
|
||||
w.Write([]byte("username-exists-error"))
|
||||
_, _ = w.Write([]byte("username-exists-error"))
|
||||
return
|
||||
}
|
||||
s.logger.Error("Error checking user", zap.Error(err), zap.String("username", reqData.Username))
|
||||
@@ -210,7 +210,7 @@ func (s *APIServer) Register(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
respData := s.newAuthData(userID, userRights, userTokenID, userToken, []Character{})
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(respData)
|
||||
_ = json.NewEncoder(w).Encode(respData)
|
||||
}
|
||||
|
||||
func (s *APIServer) CreateCharacter(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -239,7 +239,7 @@ func (s *APIServer) CreateCharacter(w http.ResponseWriter, r *http.Request) {
|
||||
character.HR = 7
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(character)
|
||||
_ = json.NewEncoder(w).Encode(character)
|
||||
}
|
||||
|
||||
func (s *APIServer) DeleteCharacter(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -264,7 +264,7 @@ func (s *APIServer) DeleteCharacter(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(struct{}{})
|
||||
_ = json.NewEncoder(w).Encode(struct{}{})
|
||||
}
|
||||
|
||||
func (s *APIServer) ExportSave(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -293,7 +293,7 @@ func (s *APIServer) ExportSave(w http.ResponseWriter, r *http.Request) {
|
||||
Character: character,
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(save)
|
||||
_ = json.NewEncoder(w).Encode(save)
|
||||
}
|
||||
func (s *APIServer) ScreenShotGet(w http.ResponseWriter, r *http.Request) {
|
||||
// Get the 'id' parameter from the URL
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"testing"
|
||||
|
||||
_config "erupe-ce/config"
|
||||
"erupe-ce/server/channelserver"
|
||||
"erupe-ce/common/gametime"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -99,7 +99,7 @@ func TestLauncherEndpointEmptyConfig(t *testing.T) {
|
||||
server.Launcher(recorder, req)
|
||||
|
||||
var respData LauncherResponse
|
||||
json.NewDecoder(recorder.Body).Decode(&respData)
|
||||
_ = json.NewDecoder(recorder.Body).Decode(&respData)
|
||||
|
||||
if respData.Banners == nil {
|
||||
t.Error("Banners should not be nil, should be empty slice")
|
||||
@@ -355,7 +355,7 @@ func TestScreenShotEndpointDisabled(t *testing.T) {
|
||||
XMLName xml.Name `xml:"result"`
|
||||
Code string `xml:"code"`
|
||||
}
|
||||
xml.NewDecoder(recorder.Body).Decode(&result)
|
||||
_ = xml.NewDecoder(recorder.Body).Decode(&result)
|
||||
|
||||
if result.Code != "400" {
|
||||
t.Errorf("Expected code 400, got %s", result.Code)
|
||||
@@ -573,7 +573,7 @@ func TestNewAuthDataTimestamps(t *testing.T) {
|
||||
authData := server.newAuthData(1, 0, 1, "token", []Character{})
|
||||
|
||||
// Timestamps should be reasonable (within last minute and next 30 days)
|
||||
now := uint32(channelserver.TimeAdjusted().Unix())
|
||||
now := uint32(gametime.Adjusted().Unix())
|
||||
if authData.CurrentTS < now-60 || authData.CurrentTS > now+60 {
|
||||
t.Errorf("CurrentTS not within reasonable range: %d vs %d", authData.CurrentTS, now)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user