Compare commits
7 Commits
0cd0d2a402
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 7acd72c74e | |||
| b1e89dca1c | |||
| 510f731a10 | |||
| ac6827aae5 | |||
| b006fe77c2 | |||
| c9af89a852 | |||
| 11d3cdfc25 |
@@ -66,10 +66,10 @@ func (h *Handler) Create(c *fiber.Ctx) error {
|
|||||||
if err := c.BodyParser(&body); err != nil || body.Title == "" || body.Content == "" {
|
if err := c.BodyParser(&body); err != nil || body.Title == "" || body.Content == "" {
|
||||||
return apperror.BadRequest("제목과 내용을 입력해주세요")
|
return apperror.BadRequest("제목과 내용을 입력해주세요")
|
||||||
}
|
}
|
||||||
if len(body.Title) > 256 {
|
if len([]rune(body.Title)) > 256 {
|
||||||
return apperror.BadRequest("제목은 256자 이하여야 합니다")
|
return apperror.BadRequest("제목은 256자 이하여야 합니다")
|
||||||
}
|
}
|
||||||
if len(body.Content) > 10000 {
|
if len([]rune(body.Content)) > 10000 {
|
||||||
return apperror.BadRequest("내용은 10000자 이하여야 합니다")
|
return apperror.BadRequest("내용은 10000자 이하여야 합니다")
|
||||||
}
|
}
|
||||||
a, err := h.svc.Create(body.Title, body.Content)
|
a, err := h.svc.Create(body.Title, body.Content)
|
||||||
@@ -110,10 +110,10 @@ func (h *Handler) Update(c *fiber.Ctx) error {
|
|||||||
if body.Title == "" && body.Content == "" {
|
if body.Title == "" && body.Content == "" {
|
||||||
return apperror.BadRequest("수정할 내용을 입력해주세요")
|
return apperror.BadRequest("수정할 내용을 입력해주세요")
|
||||||
}
|
}
|
||||||
if len(body.Title) > 256 {
|
if len([]rune(body.Title)) > 256 {
|
||||||
return apperror.BadRequest("제목은 256자 이하여야 합니다")
|
return apperror.BadRequest("제목은 256자 이하여야 합니다")
|
||||||
}
|
}
|
||||||
if len(body.Content) > 10000 {
|
if len([]rune(body.Content)) > 10000 {
|
||||||
return apperror.BadRequest("내용은 10000자 이하여야 합니다")
|
return apperror.BadRequest("내용은 10000자 이하여야 합니다")
|
||||||
}
|
}
|
||||||
a, err := h.svc.Update(uint(id), body.Title, body.Content)
|
a, err := h.svc.Update(uint(id), body.Title, body.Content)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -56,8 +57,8 @@ func (h *Handler) Register(c *fiber.Ctx) error {
|
|||||||
return apperror.BadRequest("비밀번호는 72자 이하여야 합니다")
|
return apperror.BadRequest("비밀번호는 72자 이하여야 합니다")
|
||||||
}
|
}
|
||||||
if err := h.svc.Register(req.Username, req.Password); err != nil {
|
if err := h.svc.Register(req.Username, req.Password); err != nil {
|
||||||
if strings.Contains(err.Error(), "이미 사용 중") {
|
if errors.Is(err, apperror.ErrDuplicateUsername) {
|
||||||
return apperror.Conflict(err.Error())
|
return apperror.Conflict("이미 사용 중인 아이디입니다")
|
||||||
}
|
}
|
||||||
return apperror.Internal("회원가입에 실패했습니다")
|
return apperror.Internal("회원가입에 실패했습니다")
|
||||||
}
|
}
|
||||||
@@ -111,6 +112,7 @@ func (h *Handler) Login(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
return c.JSON(fiber.Map{
|
return c.JSON(fiber.Map{
|
||||||
"token": accessToken,
|
"token": accessToken,
|
||||||
|
"refreshToken": refreshToken,
|
||||||
"username": user.Username,
|
"username": user.Username,
|
||||||
"role": user.Role,
|
"role": user.Role,
|
||||||
})
|
})
|
||||||
@@ -159,6 +161,7 @@ func (h *Handler) Refresh(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
return c.JSON(fiber.Map{
|
return c.JSON(fiber.Map{
|
||||||
"token": newAccessToken,
|
"token": newAccessToken,
|
||||||
|
"refreshToken": newRefreshToken,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -249,6 +252,11 @@ func (h *Handler) UpdateRole(c *fiber.Ctx) error {
|
|||||||
return apperror.BadRequest("role은 admin 또는 user여야 합니다")
|
return apperror.BadRequest("role은 admin 또는 user여야 합니다")
|
||||||
}
|
}
|
||||||
uid := uint(id)
|
uid := uint(id)
|
||||||
|
// 자기 자신의 admin 권한 강등 방지
|
||||||
|
callerID, _ := c.Locals("userID").(uint)
|
||||||
|
if uid == callerID && body.Role != "admin" {
|
||||||
|
return apperror.BadRequest("자신의 관리자 권한을 제거할 수 없습니다")
|
||||||
|
}
|
||||||
if err := h.svc.UpdateRole(uid, Role(body.Role)); err != nil {
|
if err := h.svc.UpdateRole(uid, Role(body.Role)); err != nil {
|
||||||
return apperror.Internal("권한 변경에 실패했습니다")
|
return apperror.Internal("권한 변경에 실패했습니다")
|
||||||
}
|
}
|
||||||
@@ -343,6 +351,7 @@ func (h *Handler) SSAFYCallback(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
return c.JSON(fiber.Map{
|
return c.JSON(fiber.Map{
|
||||||
"token": accessToken,
|
"token": accessToken,
|
||||||
|
"refreshToken": refreshToken,
|
||||||
"username": user.Username,
|
"username": user.Username,
|
||||||
"role": user.Role,
|
"role": user.Role,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
"a301_server/pkg/apperror"
|
||||||
"a301_server/pkg/config"
|
"a301_server/pkg/config"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
@@ -263,9 +264,6 @@ func (s *Service) RedeemLaunchTicket(ticket string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) Register(username, password string) error {
|
func (s *Service) Register(username, password string) error {
|
||||||
if _, err := s.repo.FindByUsername(username); err == nil {
|
|
||||||
return fmt.Errorf("이미 사용 중인 아이디입니다")
|
|
||||||
}
|
|
||||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("비밀번호 처리에 실패했습니다")
|
return fmt.Errorf("비밀번호 처리에 실패했습니다")
|
||||||
@@ -274,6 +272,9 @@ func (s *Service) Register(username, password string) error {
|
|||||||
return s.repo.Transaction(func(txRepo *Repository) error {
|
return s.repo.Transaction(func(txRepo *Repository) error {
|
||||||
user := &User{Username: username, PasswordHash: string(hash), Role: RoleUser}
|
user := &User{Username: username, PasswordHash: string(hash), Role: RoleUser}
|
||||||
if err := txRepo.Create(user); err != nil {
|
if err := txRepo.Create(user); err != nil {
|
||||||
|
if apperror.IsDuplicateEntry(err) {
|
||||||
|
return apperror.ErrDuplicateUsername
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if s.walletCreator != nil {
|
if s.walletCreator != nil {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package bossraid
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"a301_server/pkg/apperror"
|
"a301_server/pkg/apperror"
|
||||||
|
|
||||||
@@ -61,7 +62,11 @@ func (h *Handler) RequestEntry(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
room, tokens, err := h.svc.RequestEntryWithTokens(req.Usernames, req.BossID)
|
room, tokens, err := h.svc.RequestEntryWithTokens(req.Usernames, req.BossID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bossError(fiber.StatusConflict, "보스 레이드 입장에 실패했습니다", err)
|
status := fiber.StatusConflict
|
||||||
|
if strings.Contains(err.Error(), "이용 가능한") {
|
||||||
|
status = fiber.StatusServiceUnavailable
|
||||||
|
}
|
||||||
|
return bossError(status, "보스 레이드 입장에 실패했습니다", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
|
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ type BossRoom struct {
|
|||||||
UpdatedAt time.Time `json:"updatedAt"`
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
||||||
SessionName string `json:"sessionName" gorm:"type:varchar(100);uniqueIndex;not null"`
|
SessionName string `json:"sessionName" gorm:"type:varchar(100);uniqueIndex;not null"`
|
||||||
|
SlotSessionName string `json:"slotSessionName" gorm:"type:varchar(100);index;not null"`
|
||||||
BossID int `json:"bossId" gorm:"index;not null"`
|
BossID int `json:"bossId" gorm:"index;not null"`
|
||||||
Status RoomStatus `json:"status" gorm:"type:varchar(20);index;default:waiting;not null"`
|
Status RoomStatus `json:"status" gorm:"type:varchar(20);index;default:waiting;not null"`
|
||||||
MaxPlayers int `json:"maxPlayers" gorm:"default:3;not null"`
|
MaxPlayers int `json:"maxPlayers" gorm:"default:3;not null"`
|
||||||
|
|||||||
@@ -224,6 +224,12 @@ func (r *Repository) DeleteRoomBySessionName(sessionName string) error {
|
|||||||
return r.db.Unscoped().Where("session_name = ?", sessionName).Delete(&BossRoom{}).Error
|
return r.db.Unscoped().Where("session_name = ?", sessionName).Delete(&BossRoom{}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteRoomBySlotSessionName removes BossRoom records matching the original slot session name.
|
||||||
|
// Used when dedicated server calls ResetRoom with the slot name (not the unique per-entry name).
|
||||||
|
func (r *Repository) DeleteRoomBySlotSessionName(slotSessionName string) error {
|
||||||
|
return r.db.Unscoped().Where("slot_session_name = ?", slotSessionName).Delete(&BossRoom{}).Error
|
||||||
|
}
|
||||||
|
|
||||||
// CleanupStaleWaitingRooms deletes BossRoom records stuck in "waiting" status
|
// CleanupStaleWaitingRooms deletes BossRoom records stuck in "waiting" status
|
||||||
// past the given threshold and resets their associated RoomSlots to idle.
|
// past the given threshold and resets their associated RoomSlots to idle.
|
||||||
// This handles cases where players disconnect during loading before the Fusion session starts.
|
// This handles cases where players disconnect during loading before the Fusion session starts.
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ func (s *Service) RequestEntry(usernames []string, bossID int) (*BossRoom, error
|
|||||||
|
|
||||||
room = &BossRoom{
|
room = &BossRoom{
|
||||||
SessionName: slot.SessionName,
|
SessionName: slot.SessionName,
|
||||||
|
SlotSessionName: slot.SessionName,
|
||||||
BossID: bossID,
|
BossID: bossID,
|
||||||
Status: StatusWaiting,
|
Status: StatusWaiting,
|
||||||
MaxPlayers: len(usernames),
|
MaxPlayers: len(usernames),
|
||||||
@@ -221,28 +222,27 @@ func (s *Service) CompleteRaid(sessionName string, rewards []PlayerReward) (*Bos
|
|||||||
hasRewardFailure := false
|
hasRewardFailure := false
|
||||||
if s.rewardGrant != nil {
|
if s.rewardGrant != nil {
|
||||||
for _, r := range rewards {
|
for _, r := range rewards {
|
||||||
lastTxID, grantErr := s.grantWithRetry(r.Username, r.TokenAmount, r.Assets)
|
// 1회만 시도 — 실패 시 즉시 RewardFailure에 저장하여 백그라운드 워커가 재시도
|
||||||
|
txID, grantErr := s.rewardGrant(r.Username, r.TokenAmount, r.Assets)
|
||||||
result := RewardResult{Username: r.Username, Success: grantErr == nil}
|
result := RewardResult{Username: r.Username, Success: grantErr == nil}
|
||||||
if grantErr != nil {
|
if grantErr != nil {
|
||||||
result.Error = grantErr.Error()
|
result.Error = grantErr.Error()
|
||||||
log.Printf("보상 지급 실패 (재시도 소진): %s: %v", r.Username, grantErr)
|
log.Printf("보상 지급 실패: %s: %v (백그라운드 재시도 예정)", r.Username, grantErr)
|
||||||
hasRewardFailure = true
|
hasRewardFailure = true
|
||||||
// 실패한 보상을 DB에 기록하여 백그라운드 재시도 가능하게 함
|
s.saveRewardFailure(sessionName, r, grantErr, txID)
|
||||||
s.saveRewardFailure(sessionName, r, grantErr, lastTxID)
|
|
||||||
}
|
}
|
||||||
resultRewards = append(resultRewards, result)
|
resultRewards = append(resultRewards, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grant experience to players (with retry)
|
// Grant experience to players (1회 시도, 실패 시 백그라운드 재시도)
|
||||||
if s.expGrant != nil {
|
if s.expGrant != nil {
|
||||||
for _, r := range rewards {
|
for _, r := range rewards {
|
||||||
if r.Experience > 0 {
|
if r.Experience > 0 {
|
||||||
expErr := s.grantExpWithRetry(r.Username, r.Experience)
|
expErr := s.expGrant(r.Username, r.Experience)
|
||||||
if expErr != nil {
|
if expErr != nil {
|
||||||
log.Printf("경험치 지급 실패 (재시도 소진): %s: %v", r.Username, expErr)
|
log.Printf("경험치 지급 실패: %s: %v (백그라운드 재시도 예정)", r.Username, expErr)
|
||||||
hasRewardFailure = true
|
hasRewardFailure = true
|
||||||
// 경험치 실패도 RewardFailure에 기록 (토큰/에셋 없이 경험치만)
|
|
||||||
s.saveRewardFailure(sessionName, PlayerReward{
|
s.saveRewardFailure(sessionName, PlayerReward{
|
||||||
Username: r.Username,
|
Username: r.Username,
|
||||||
Experience: r.Experience,
|
Experience: r.Experience,
|
||||||
@@ -263,8 +263,13 @@ func (s *Service) CompleteRaid(sessionName string, rewards []PlayerReward) (*Bos
|
|||||||
if err := s.repo.DeleteRoomBySessionName(sessionName); err != nil {
|
if err := s.repo.DeleteRoomBySessionName(sessionName); err != nil {
|
||||||
log.Printf("BossRoom 삭제 실패 (complete): %s: %v", sessionName, err)
|
log.Printf("BossRoom 삭제 실패 (complete): %s: %v", sessionName, err)
|
||||||
}
|
}
|
||||||
if err := s.repo.ResetRoomSlot(sessionName); err != nil {
|
// SlotSessionName으로 슬롯 리셋 (고유 세션명이 아닌 원래 슬롯명)
|
||||||
log.Printf("슬롯 리셋 실패 (complete): %s: %v", sessionName, err)
|
slotName := resultRoom.SlotSessionName
|
||||||
|
if slotName == "" {
|
||||||
|
slotName = sessionName // 하위 호환
|
||||||
|
}
|
||||||
|
if err := s.repo.ResetRoomSlot(slotName); err != nil {
|
||||||
|
log.Printf("슬롯 리셋 실패 (complete): %s: %v", slotName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return resultRoom, resultRewards, nil
|
return resultRoom, resultRewards, nil
|
||||||
@@ -296,8 +301,12 @@ func (s *Service) FailRaid(sessionName string) (*BossRoom, error) {
|
|||||||
if err := s.repo.DeleteRoomBySessionName(sessionName); err != nil {
|
if err := s.repo.DeleteRoomBySessionName(sessionName); err != nil {
|
||||||
log.Printf("BossRoom 삭제 실패 (fail): %s: %v", sessionName, err)
|
log.Printf("BossRoom 삭제 실패 (fail): %s: %v", sessionName, err)
|
||||||
}
|
}
|
||||||
if err := s.repo.ResetRoomSlot(sessionName); err != nil {
|
slotName := room.SlotSessionName
|
||||||
log.Printf("슬롯 리셋 실패 (fail): %s: %v", sessionName, err)
|
if slotName == "" {
|
||||||
|
slotName = sessionName
|
||||||
|
}
|
||||||
|
if err := s.repo.ResetRoomSlot(slotName); err != nil {
|
||||||
|
log.Printf("슬롯 리셋 실패 (fail): %s: %v", slotName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return room, nil
|
return room, nil
|
||||||
@@ -407,6 +416,18 @@ func (s *Service) RequestEntryWithTokens(usernames []string, bossID int) (*BossR
|
|||||||
|
|
||||||
tokens, err := s.GenerateEntryTokens(room.SessionName, usernames)
|
tokens, err := s.GenerateEntryTokens(room.SessionName, usernames)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// 토큰 생성 실패 시 방/슬롯 롤백
|
||||||
|
log.Printf("입장 토큰 생성 실패, 방/슬롯 롤백: session=%s: %v", room.SessionName, err)
|
||||||
|
if delErr := s.repo.DeleteRoomBySessionName(room.SessionName); delErr != nil {
|
||||||
|
log.Printf("롤백 중 방 삭제 실패: %v", delErr)
|
||||||
|
}
|
||||||
|
rollbackSlot := room.SlotSessionName
|
||||||
|
if rollbackSlot == "" {
|
||||||
|
rollbackSlot = room.SessionName
|
||||||
|
}
|
||||||
|
if resetErr := s.repo.ResetRoomSlot(rollbackSlot); resetErr != nil {
|
||||||
|
log.Printf("롤백 중 슬롯 리셋 실패: %v", resetErr)
|
||||||
|
}
|
||||||
return nil, nil, fmt.Errorf("입장 토큰 생성 실패: %w", err)
|
return nil, nil, fmt.Errorf("입장 토큰 생성 실패: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -438,7 +459,11 @@ func (s *Service) cleanupStaleWaitingForUsers(usernames []string) {
|
|||||||
if err := s.repo.DeleteRoomBySessionName(room.SessionName); err != nil {
|
if err := s.repo.DeleteRoomBySessionName(room.SessionName); err != nil {
|
||||||
log.Printf("대기방 삭제 실패: %v", err)
|
log.Printf("대기방 삭제 실패: %v", err)
|
||||||
}
|
}
|
||||||
_ = s.repo.ResetRoomSlot(room.SessionName)
|
cleanupSlot := room.SlotSessionName
|
||||||
|
if cleanupSlot == "" {
|
||||||
|
cleanupSlot = room.SessionName
|
||||||
|
}
|
||||||
|
_ = s.repo.ResetRoomSlot(cleanupSlot)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -520,10 +545,15 @@ func (s *Service) CheckStaleSlots() {
|
|||||||
|
|
||||||
// ResetRoom resets a room slot back to idle and cleans up any lingering BossRoom records.
|
// ResetRoom resets a room slot back to idle and cleans up any lingering BossRoom records.
|
||||||
// Called by the dedicated server after a raid ends and the runner is recycled.
|
// Called by the dedicated server after a raid ends and the runner is recycled.
|
||||||
|
// sessionName here is the slot's original session name (not the unique per-entry name).
|
||||||
func (s *Service) ResetRoom(sessionName string) error {
|
func (s *Service) ResetRoom(sessionName string) error {
|
||||||
// 완료/실패되지 않은 BossRoom 레코드 정리 (waiting/in_progress 상태)
|
// 고유 세션명 BossRoom도 정리 (slot_session_name으로 검색)
|
||||||
|
if err := s.repo.DeleteRoomBySlotSessionName(sessionName); err != nil {
|
||||||
|
log.Printf("BossRoom 레코드 정리 실패 (by slot): %s: %v", sessionName, err)
|
||||||
|
}
|
||||||
|
// 하위 호환: 원래 세션명으로도 시도
|
||||||
if err := s.repo.DeleteRoomBySessionName(sessionName); err != nil {
|
if err := s.repo.DeleteRoomBySessionName(sessionName); err != nil {
|
||||||
log.Printf("BossRoom 레코드 정리 실패: %s: %v", sessionName, err)
|
// 이미 삭제되었을 수 있으므로 무시
|
||||||
}
|
}
|
||||||
return s.repo.ResetRoomSlot(sessionName)
|
return s.repo.ResetRoomSlot(sessionName)
|
||||||
}
|
}
|
||||||
@@ -541,49 +571,7 @@ func (s *Service) GetServerStatus(serverName string) (*DedicatedServer, []RoomSl
|
|||||||
return server, slots, nil
|
return server, slots, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Reward retry helpers ---
|
// --- Reward helpers ---
|
||||||
|
|
||||||
const immediateRetries = 3
|
|
||||||
|
|
||||||
// grantWithRetry attempts the reward grant up to 3 times with backoff (1s, 2s).
|
|
||||||
// Returns the last attempted transaction ID (may be empty) and the error.
|
|
||||||
func (s *Service) grantWithRetry(username string, tokenAmount uint64, assets []core.MintAssetPayload) (string, error) {
|
|
||||||
delays := []time.Duration{1 * time.Second, 2 * time.Second}
|
|
||||||
var lastErr error
|
|
||||||
var lastTxID string
|
|
||||||
for attempt := 0; attempt < immediateRetries; attempt++ {
|
|
||||||
txID, err := s.rewardGrant(username, tokenAmount, assets)
|
|
||||||
if txID != "" {
|
|
||||||
lastTxID = txID
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
return txID, nil
|
|
||||||
}
|
|
||||||
lastErr = err
|
|
||||||
if attempt < len(delays) {
|
|
||||||
log.Printf("보상 지급 재시도 (%d/%d): %s: %v", attempt+1, immediateRetries, username, lastErr)
|
|
||||||
time.Sleep(delays[attempt])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return lastTxID, lastErr
|
|
||||||
}
|
|
||||||
|
|
||||||
// grantExpWithRetry attempts the experience grant up to 3 times with backoff (1s, 2s).
|
|
||||||
func (s *Service) grantExpWithRetry(username string, exp int) error {
|
|
||||||
delays := []time.Duration{1 * time.Second, 2 * time.Second}
|
|
||||||
var lastErr error
|
|
||||||
for attempt := 0; attempt < immediateRetries; attempt++ {
|
|
||||||
lastErr = s.expGrant(username, exp)
|
|
||||||
if lastErr == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if attempt < len(delays) {
|
|
||||||
log.Printf("경험치 지급 재시도 (%d/%d): %s: %v", attempt+1, immediateRetries, username, lastErr)
|
|
||||||
time.Sleep(delays[attempt])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return lastErr
|
|
||||||
}
|
|
||||||
|
|
||||||
// saveRewardFailure records a failed reward in the DB for background retry.
|
// saveRewardFailure records a failed reward in the DB for background retry.
|
||||||
func (s *Service) saveRewardFailure(sessionName string, r PlayerReward, grantErr error, lastTxID string) {
|
func (s *Service) saveRewardFailure(sessionName string, r PlayerReward, grantErr error, lastTxID string) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/tolelom/tolchain/core"
|
"github.com/tolelom/tolchain/core"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxLimit = 200
|
const maxLimit = 200
|
||||||
@@ -116,8 +117,11 @@ func (h *Handler) GetWalletInfo(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
w, err := h.svc.GetWallet(userID)
|
w, err := h.svc.GetWallet(userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return apperror.NotFound("지갑을 찾을 수 없습니다")
|
return apperror.NotFound("지갑을 찾을 수 없습니다")
|
||||||
}
|
}
|
||||||
|
return apperror.Internal("지갑 조회에 실패했습니다")
|
||||||
|
}
|
||||||
return c.JSON(fiber.Map{
|
return c.JSON(fiber.Map{
|
||||||
"address": w.Address,
|
"address": w.Address,
|
||||||
"pubKeyHex": w.PubKeyHex,
|
"pubKeyHex": w.PubKeyHex,
|
||||||
@@ -579,6 +583,9 @@ func (h *Handler) GrantReward(c *fiber.Ctx) error {
|
|||||||
if !validID(req.RecipientPubKey) {
|
if !validID(req.RecipientPubKey) {
|
||||||
return apperror.BadRequest("recipientPubKey는 필수입니다")
|
return apperror.BadRequest("recipientPubKey는 필수입니다")
|
||||||
}
|
}
|
||||||
|
if req.TokenAmount == 0 && len(req.Assets) == 0 {
|
||||||
|
return apperror.BadRequest("tokenAmount 또는 assets가 필요합니다")
|
||||||
|
}
|
||||||
result, err := h.svc.GrantReward(req.RecipientPubKey, req.TokenAmount, req.Assets)
|
result, err := h.svc.GrantReward(req.RecipientPubKey, req.TokenAmount, req.Assets)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return chainError("보상 지급에 실패했습니다", err)
|
return chainError("보상 지급에 실패했습니다", err)
|
||||||
@@ -644,12 +651,12 @@ func (h *Handler) ExportWallet(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
var req exportRequest
|
var req exportRequest
|
||||||
if err := c.BodyParser(&req); err != nil || req.Password == "" {
|
if err := c.BodyParser(&req); err != nil || req.Password == "" {
|
||||||
return c.Status(400).JSON(fiber.Map{"error": "password is required"})
|
return apperror.BadRequest("password는 필수입니다")
|
||||||
}
|
}
|
||||||
slog.Warn("wallet export requested", "userID", userID, "ip", c.IP())
|
slog.Warn("wallet export requested", "userID", userID, "ip", c.IP())
|
||||||
privKeyHex, err := h.svc.ExportPrivKey(userID, req.Password)
|
privKeyHex, err := h.svc.ExportPrivKey(userID, req.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(401).JSON(fiber.Map{"error": "invalid password"})
|
return apperror.Unauthorized("비밀번호가 올바르지 않습니다")
|
||||||
}
|
}
|
||||||
return c.JSON(fiber.Map{"privateKey": privKeyHex})
|
return c.JSON(fiber.Map{"privateKey": privKeyHex})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"a301_server/pkg/apperror"
|
||||||
|
|
||||||
"github.com/tolelom/tolchain/core"
|
"github.com/tolelom/tolchain/core"
|
||||||
tocrypto "github.com/tolelom/tolchain/crypto"
|
tocrypto "github.com/tolelom/tolchain/crypto"
|
||||||
"github.com/tolelom/tolchain/wallet"
|
"github.com/tolelom/tolchain/wallet"
|
||||||
@@ -69,13 +71,18 @@ func (s *Service) resolveUsername(username string) (string, error) {
|
|||||||
uw, err := s.repo.FindByUserID(userID)
|
uw, err := s.repo.FindByUserID(userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// 지갑이 없으면 자동 생성 시도
|
// 지갑이 없으면 자동 생성 시도
|
||||||
uw, err = s.CreateWallet(userID)
|
var createErr error
|
||||||
if err != nil {
|
uw, createErr = s.CreateWallet(userID)
|
||||||
|
if createErr != nil {
|
||||||
|
if apperror.IsDuplicateEntry(createErr) {
|
||||||
// unique constraint 위반 — 다른 고루틴이 먼저 생성 완료
|
// unique constraint 위반 — 다른 고루틴이 먼저 생성 완료
|
||||||
uw, err = s.repo.FindByUserID(userID)
|
uw, err = s.repo.FindByUserID(userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("wallet auto-creation failed: %w", err)
|
return "", fmt.Errorf("wallet auto-creation failed: %w", err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return "", fmt.Errorf("wallet auto-creation failed: %w", createErr)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Printf("INFO: auto-created wallet for userID=%d (username=%s)", userID, username)
|
log.Printf("INFO: auto-created wallet for userID=%d (username=%s)", userID, username)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -151,12 +151,12 @@ func (s *Service) Upload(filename string, body io.Reader, baseURL string) (*Info
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fileHash := hashGameExeFromZip(finalPath)
|
// game.zip 전체의 해시를 사용하여 업데이트 감지.
|
||||||
|
// A301.exe만 해시하면 Mono 빌드에서 exe가 안 바뀌어도
|
||||||
|
// Data 폴더의 스크립트/에셋 변경을 감지하지 못함.
|
||||||
|
fileHash := hashFileToHex(finalPath)
|
||||||
if fileHash == "" {
|
if fileHash == "" {
|
||||||
if removeErr := os.Remove(finalPath); removeErr != nil {
|
return nil, fmt.Errorf("파일 해시 계산에 실패했습니다")
|
||||||
log.Printf("WARNING: failed to remove file %s: %v", finalPath, removeErr)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("zip 파일에 %s이(가) 포함되어 있지 않습니다", "A301.exe")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := s.repo.GetLatest()
|
info, err := s.repo.GetLatest()
|
||||||
@@ -215,12 +215,17 @@ func hashGameExeFromZip(zipPath string) string {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
lr := io.LimitReader(rc, maxExeSize+1)
|
||||||
h := sha256.New()
|
h := sha256.New()
|
||||||
_, err = io.Copy(h, io.LimitReader(rc, maxExeSize))
|
n, err := io.Copy(h, lr)
|
||||||
rc.Close()
|
rc.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
if n > maxExeSize {
|
||||||
|
log.Printf("WARNING: A301.exe exceeds %dMB, hash may be inaccurate", maxExeSize/1024/1024)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
return hex.EncodeToString(h.Sum(nil))
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ import (
|
|||||||
|
|
||||||
// validateGameData checks that game data fields are within acceptable ranges.
|
// validateGameData checks that game data fields are within acceptable ranges.
|
||||||
func validateGameData(data *GameDataRequest) error {
|
func validateGameData(data *GameDataRequest) error {
|
||||||
if data.Level != nil && (*data.Level < 1 || *data.Level > 999) {
|
if data.Level != nil && (*data.Level < 1 || *data.Level > MaxLevel) {
|
||||||
return fmt.Errorf("레벨은 1~999 범위여야 합니다")
|
return fmt.Errorf("레벨은 1~%d 범위여야 합니다", MaxLevel)
|
||||||
}
|
}
|
||||||
if data.Experience != nil && *data.Experience < 0 {
|
if data.Experience != nil && *data.Experience < 0 {
|
||||||
return fmt.Errorf("경험치는 0 이상이어야 합니다")
|
return fmt.Errorf("경험치는 0 이상이어야 합니다")
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ func New() *fiber.App {
|
|||||||
AllowOrigins: config.C.CORSAllowOrigins,
|
AllowOrigins: config.C.CORSAllowOrigins,
|
||||||
AllowHeaders: "Origin, Content-Type, Authorization, Idempotency-Key, X-API-Key, X-Requested-With",
|
AllowHeaders: "Origin, Content-Type, Authorization, Idempotency-Key, X-API-Key, X-Requested-With",
|
||||||
AllowMethods: "GET, POST, PUT, PATCH, DELETE",
|
AllowMethods: "GET, POST, PUT, PATCH, DELETE",
|
||||||
|
ExposeHeaders: "X-Request-ID, X-Idempotent-Replay",
|
||||||
AllowCredentials: true,
|
AllowCredentials: true,
|
||||||
}))
|
}))
|
||||||
return app
|
return app
|
||||||
@@ -59,10 +60,10 @@ func AuthLimiter() fiber.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// APILimiter returns a rate limiter for general API endpoints (60 req/min per IP).
|
// APILimiter returns a rate limiter for general API endpoints (120 req/min per IP).
|
||||||
func APILimiter() fiber.Handler {
|
func APILimiter() fiber.Handler {
|
||||||
return limiter.New(limiter.Config{
|
return limiter.New(limiter.Config{
|
||||||
Max: 60,
|
Max: 120,
|
||||||
Expiration: 1 * time.Minute,
|
Expiration: 1 * time.Minute,
|
||||||
KeyGenerator: func(c *fiber.Ctx) string {
|
KeyGenerator: func(c *fiber.Ctx) string {
|
||||||
return c.IP()
|
return c.IP()
|
||||||
|
|||||||
11
main.go
11
main.go
@@ -183,14 +183,22 @@ func main() {
|
|||||||
// ── Graceful shutdown ────────────────────────────────────────────
|
// ── Graceful shutdown ────────────────────────────────────────────
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
if err := app.Listen(":" + config.C.AppPort); err != nil {
|
||||||
|
log.Printf("서버 Listen 종료: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
sigCh := make(chan os.Signal, 1)
|
sigCh := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
||||||
sig := <-sigCh
|
sig := <-sigCh
|
||||||
log.Printf("수신된 시그널: %v — 서버 종료 중...", sig)
|
log.Printf("수신된 시그널: %v — 서버 종료 중...", sig)
|
||||||
|
|
||||||
rewardWorker.Stop()
|
rewardWorker.Stop()
|
||||||
|
|
||||||
if err := app.ShutdownWithTimeout(10 * time.Second); err != nil {
|
if err := app.ShutdownWithTimeout(10 * time.Second); err != nil {
|
||||||
log.Printf("서버 종료 실패: %v", err)
|
log.Printf("서버 종료 실패: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if rdb != nil {
|
if rdb != nil {
|
||||||
if err := rdb.Close(); err != nil {
|
if err := rdb.Close(); err != nil {
|
||||||
log.Printf("Redis 종료 실패: %v", err)
|
log.Printf("Redis 종료 실패: %v", err)
|
||||||
@@ -205,7 +213,6 @@ func main() {
|
|||||||
log.Println("MySQL 연결 종료 완료")
|
log.Println("MySQL 연결 종료 완료")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
log.Fatal(app.Listen(":" + config.C.AppPort))
|
log.Println("서버 종료 완료")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
package apperror
|
package apperror
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-sql-driver/mysql"
|
||||||
|
)
|
||||||
|
|
||||||
// AppError is a structured application error with an HTTP status code.
|
// AppError is a structured application error with an HTTP status code.
|
||||||
// JSON response format: {"error": "<code>", "message": "<human-readable message>"}
|
// JSON response format: {"error": "<code>", "message": "<human-readable message>"}
|
||||||
@@ -57,3 +63,15 @@ func Conflict(message string) *AppError {
|
|||||||
func Internal(message string) *AppError {
|
func Internal(message string) *AppError {
|
||||||
return &AppError{Code: "internal_error", Message: message, Status: 500}
|
return &AppError{Code: "internal_error", Message: message, Status: 500}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrDuplicateUsername is returned when a username already exists.
|
||||||
|
var ErrDuplicateUsername = fmt.Errorf("이미 사용 중인 아이디입니다")
|
||||||
|
|
||||||
|
// IsDuplicateEntry checks if a GORM error is a MySQL duplicate key violation (error 1062).
|
||||||
|
func IsDuplicateEntry(err error) bool {
|
||||||
|
var mysqlErr *mysql.MySQLError
|
||||||
|
if errors.As(err, &mysqlErr) {
|
||||||
|
return mysqlErr.Number == 1062
|
||||||
|
}
|
||||||
|
return strings.Contains(err.Error(), "Duplicate entry") || strings.Contains(err.Error(), "UNIQUE constraint")
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
func ConnectMySQL() (*gorm.DB, error) {
|
func ConnectMySQL() (*gorm.DB, error) {
|
||||||
c := config.C
|
c := config.C
|
||||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=UTC",
|
||||||
c.DBUser, c.DBPassword, c.DBHost, c.DBPort, c.DBName,
|
c.DBUser, c.DBPassword, c.DBHost, c.DBPort, c.DBName,
|
||||||
)
|
)
|
||||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func Idempotency(rdb *redis.Client) fiber.Handler {
|
|||||||
if uid, ok := c.Locals("userID").(uint); ok {
|
if uid, ok := c.Locals("userID").(uint); ok {
|
||||||
redisKey += fmt.Sprintf("u%d:", uid)
|
redisKey += fmt.Sprintf("u%d:", uid)
|
||||||
}
|
}
|
||||||
redisKey += key
|
redisKey += c.Method() + ":" + c.Route().Path + ":" + key
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), redisTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), redisTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|||||||
Reference in New Issue
Block a user