feat: 인프라 개선 — 헬스체크, 로깅, 보안, CI 검증
- /health + /ready 엔드포인트 추가 (DB/Redis 상태 확인) - RequestID 미들웨어 + 구조화 JSON 로깅 - 체인 트랜잭션 per-user rate limit (20 req/min) - DB 커넥션 풀 설정 (MaxOpen 25, MaxIdle 10, MaxLifetime 5m) - Graceful Shutdown 시 Redis/MySQL 연결 정리 - Dockerfile HEALTHCHECK 추가 - CI에 go vet + 빌드 검증 단계 추가 (deploy 전 실행) - 보스 레이드 클라이언트 입장 API (JWT 인증) - Player 프로필 모듈 추가 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
84
internal/player/handler.go
Normal file
84
internal/player/handler.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
svc *Service
|
||||
}
|
||||
|
||||
func NewHandler(svc *Service) *Handler {
|
||||
return &Handler{svc: svc}
|
||||
}
|
||||
|
||||
// GetProfile 자신의 프로필 조회 (JWT 인증)
|
||||
func (h *Handler) GetProfile(c *fiber.Ctx) error {
|
||||
userID, ok := c.Locals("userID").(uint)
|
||||
if !ok {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "인증 정보가 올바르지 않습니다"})
|
||||
}
|
||||
|
||||
profile, err := h.svc.GetProfile(userID)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
|
||||
return c.JSON(profile)
|
||||
}
|
||||
|
||||
// UpdateProfile 자신의 프로필 수정 (JWT 인증)
|
||||
func (h *Handler) UpdateProfile(c *fiber.Ctx) error {
|
||||
userID, ok := c.Locals("userID").(uint)
|
||||
if !ok {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "인증 정보가 올바르지 않습니다"})
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Nickname string `json:"nickname"`
|
||||
}
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "잘못된 요청입니다"})
|
||||
}
|
||||
|
||||
profile, err := h.svc.UpdateProfile(userID, req.Nickname)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
|
||||
return c.JSON(profile)
|
||||
}
|
||||
|
||||
// InternalGetProfile 내부 API: username 쿼리 파라미터로 프로필 조회
|
||||
func (h *Handler) InternalGetProfile(c *fiber.Ctx) error {
|
||||
username := c.Query("username")
|
||||
if username == "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "username 파라미터가 필요합니다"})
|
||||
}
|
||||
|
||||
profile, err := h.svc.GetProfileByUsername(username)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
|
||||
return c.JSON(profile)
|
||||
}
|
||||
|
||||
// InternalSaveGameData 내부 API: username 쿼리 파라미터로 게임 데이터 저장
|
||||
func (h *Handler) InternalSaveGameData(c *fiber.Ctx) error {
|
||||
username := c.Query("username")
|
||||
if username == "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "username 파라미터가 필요합니다"})
|
||||
}
|
||||
|
||||
var req GameDataRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "잘못된 요청입니다"})
|
||||
}
|
||||
|
||||
if err := h.svc.SaveGameDataByUsername(username, &req); err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{"message": "게임 데이터가 저장되었습니다"})
|
||||
}
|
||||
37
internal/player/model.go
Normal file
37
internal/player/model.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type PlayerProfile struct {
|
||||
ID uint `json:"id" gorm:"primaryKey"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
||||
|
||||
UserID uint `json:"userId" gorm:"uniqueIndex;not null"`
|
||||
Nickname string `json:"nickname" gorm:"type:varchar(50);not null;default:''"`
|
||||
|
||||
// 레벨 & 경험치
|
||||
Level int `json:"level" gorm:"default:1"`
|
||||
Experience int `json:"experience" gorm:"default:0"`
|
||||
|
||||
// 전투 스탯
|
||||
MaxHP float64 `json:"maxHp" gorm:"default:100"`
|
||||
MaxMP float64 `json:"maxMp" gorm:"default:50"`
|
||||
AttackPower float64 `json:"attackPower" gorm:"default:10"`
|
||||
AttackRange float64 `json:"attackRange" gorm:"default:3"`
|
||||
SprintMultiplier float64 `json:"sprintMultiplier" gorm:"default:1.8"`
|
||||
|
||||
// 마지막 위치
|
||||
LastPosX float64 `json:"lastPosX" gorm:"default:0"`
|
||||
LastPosY float64 `json:"lastPosY" gorm:"default:0"`
|
||||
LastPosZ float64 `json:"lastPosZ" gorm:"default:0"`
|
||||
LastRotY float64 `json:"lastRotY" gorm:"default:0"`
|
||||
|
||||
// 플레이 시간 (초 단위)
|
||||
TotalPlayTime int64 `json:"totalPlayTime" gorm:"default:0"`
|
||||
}
|
||||
41
internal/player/repository.go
Normal file
41
internal/player/repository.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package player
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) Create(profile *PlayerProfile) error {
|
||||
return r.db.Create(profile).Error
|
||||
}
|
||||
|
||||
func (r *Repository) FindByUserID(userID uint) (*PlayerProfile, error) {
|
||||
var profile PlayerProfile
|
||||
if err := r.db.Where("user_id = ?", userID).First(&profile).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &profile, nil
|
||||
}
|
||||
|
||||
func (r *Repository) Update(profile *PlayerProfile) error {
|
||||
return r.db.Save(profile).Error
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateStats(userID uint, updates map[string]interface{}) error {
|
||||
return r.db.Model(&PlayerProfile{}).Where("user_id = ?", userID).Updates(updates).Error
|
||||
}
|
||||
|
||||
func (r *Repository) FindByUsername(username string) (*PlayerProfile, error) {
|
||||
var profile PlayerProfile
|
||||
if err := r.db.Joins("JOIN users ON users.id = player_profiles.user_id").
|
||||
Where("users.username = ? AND users.deleted_at IS NULL", username).
|
||||
First(&profile).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &profile, nil
|
||||
}
|
||||
148
internal/player/service.go
Normal file
148
internal/player/service.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo *Repository
|
||||
userResolver func(username string) (uint, error)
|
||||
}
|
||||
|
||||
func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) SetUserResolver(fn func(username string) (uint, error)) {
|
||||
s.userResolver = fn
|
||||
}
|
||||
|
||||
// CreateProfile 회원가입 시 자동 호출되어 기본 프로필을 생성한다.
|
||||
func (s *Service) CreateProfile(userID uint) error {
|
||||
profile := &PlayerProfile{
|
||||
UserID: userID,
|
||||
}
|
||||
return s.repo.Create(profile)
|
||||
}
|
||||
|
||||
// GetProfile JWT 인증된 유저의 프로필을 조회한다.
|
||||
func (s *Service) GetProfile(userID uint) (*PlayerProfile, error) {
|
||||
profile, err := s.repo.FindByUserID(userID)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, fmt.Errorf("프로필이 존재하지 않습니다")
|
||||
}
|
||||
return nil, fmt.Errorf("프로필 조회에 실패했습니다")
|
||||
}
|
||||
return profile, nil
|
||||
}
|
||||
|
||||
// GetProfileByUsername 내부 API용: username으로 프로필 조회.
|
||||
func (s *Service) GetProfileByUsername(username string) (*PlayerProfile, error) {
|
||||
if s.userResolver == nil {
|
||||
return nil, fmt.Errorf("userResolver가 설정되지 않았습니다")
|
||||
}
|
||||
userID, err := s.userResolver(username)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("존재하지 않는 유저입니다")
|
||||
}
|
||||
return s.GetProfile(userID)
|
||||
}
|
||||
|
||||
// UpdateProfile 유저 자신의 프로필(닉네임)을 수정한다.
|
||||
func (s *Service) UpdateProfile(userID uint, nickname string) (*PlayerProfile, error) {
|
||||
profile, err := s.repo.FindByUserID(userID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("프로필이 존재하지 않습니다")
|
||||
}
|
||||
if nickname != "" {
|
||||
profile.Nickname = nickname
|
||||
}
|
||||
if err := s.repo.Update(profile); err != nil {
|
||||
return nil, fmt.Errorf("프로필 수정에 실패했습니다")
|
||||
}
|
||||
return profile, nil
|
||||
}
|
||||
|
||||
// SaveGameData 게임 서버에서 호출: 게임 데이터를 저장한다.
|
||||
func (s *Service) SaveGameData(userID uint, data *GameDataRequest) error {
|
||||
updates := map[string]interface{}{}
|
||||
|
||||
if data.Level != nil {
|
||||
updates["level"] = *data.Level
|
||||
}
|
||||
if data.Experience != nil {
|
||||
updates["experience"] = *data.Experience
|
||||
}
|
||||
if data.MaxHP != nil {
|
||||
updates["max_hp"] = *data.MaxHP
|
||||
}
|
||||
if data.MaxMP != nil {
|
||||
updates["max_mp"] = *data.MaxMP
|
||||
}
|
||||
if data.AttackPower != nil {
|
||||
updates["attack_power"] = *data.AttackPower
|
||||
}
|
||||
if data.AttackRange != nil {
|
||||
updates["attack_range"] = *data.AttackRange
|
||||
}
|
||||
if data.SprintMultiplier != nil {
|
||||
updates["sprint_multiplier"] = *data.SprintMultiplier
|
||||
}
|
||||
if data.LastPosX != nil {
|
||||
updates["last_pos_x"] = *data.LastPosX
|
||||
}
|
||||
if data.LastPosY != nil {
|
||||
updates["last_pos_y"] = *data.LastPosY
|
||||
}
|
||||
if data.LastPosZ != nil {
|
||||
updates["last_pos_z"] = *data.LastPosZ
|
||||
}
|
||||
if data.LastRotY != nil {
|
||||
updates["last_rot_y"] = *data.LastRotY
|
||||
}
|
||||
if data.PlayTimeDelta != nil {
|
||||
// 플레이 시간은 delta로 누적
|
||||
profile, err := s.repo.FindByUserID(userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("프로필이 존재하지 않습니다")
|
||||
}
|
||||
updates["total_play_time"] = profile.TotalPlayTime + *data.PlayTimeDelta
|
||||
}
|
||||
|
||||
if len(updates) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return s.repo.UpdateStats(userID, updates)
|
||||
}
|
||||
|
||||
// SaveGameDataByUsername 내부 API용: username 기반으로 게임 데이터 저장.
|
||||
func (s *Service) SaveGameDataByUsername(username string, data *GameDataRequest) error {
|
||||
if s.userResolver == nil {
|
||||
return fmt.Errorf("userResolver가 설정되지 않았습니다")
|
||||
}
|
||||
userID, err := s.userResolver(username)
|
||||
if err != nil {
|
||||
return fmt.Errorf("존재하지 않는 유저입니다")
|
||||
}
|
||||
return s.SaveGameData(userID, data)
|
||||
}
|
||||
|
||||
// GameDataRequest 게임 데이터 저장 요청 (nil 필드는 변경하지 않음).
|
||||
type GameDataRequest struct {
|
||||
Level *int `json:"level,omitempty"`
|
||||
Experience *int `json:"experience,omitempty"`
|
||||
MaxHP *float64 `json:"maxHp,omitempty"`
|
||||
MaxMP *float64 `json:"maxMp,omitempty"`
|
||||
AttackPower *float64 `json:"attackPower,omitempty"`
|
||||
AttackRange *float64 `json:"attackRange,omitempty"`
|
||||
SprintMultiplier *float64 `json:"sprintMultiplier,omitempty"`
|
||||
LastPosX *float64 `json:"lastPosX,omitempty"`
|
||||
LastPosY *float64 `json:"lastPosY,omitempty"`
|
||||
LastPosZ *float64 `json:"lastPosZ,omitempty"`
|
||||
LastRotY *float64 `json:"lastRotY,omitempty"`
|
||||
PlayTimeDelta *int64 `json:"playTimeDelta,omitempty"` // 누적할 플레이 시간(초)
|
||||
}
|
||||
Reference in New Issue
Block a user