Compare commits

...

2 Commits

Author SHA1 Message Date
05d6d5af4d chore: seed 스크립트 제거
All checks were successful
Server CI/CD / deploy (push) Successful in 36s
환경변수 기반 admin 자동 생성으로 대체

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-24 14:29:29 +09:00
633175f5be feat: 환경변수로 초기 admin 계정 자동 생성
서버 시작 시 ADMIN_USERNAME, ADMIN_PASSWORD 환경변수 기반으로
admin 계정이 없을 경우 자동 생성 (이미 있으면 스킵)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-24 14:28:33 +09:00
5 changed files with 30 additions and 49 deletions

View File

@@ -11,3 +11,6 @@ REDIS_PASSWORD=
JWT_SECRET=your-secret-key-here
JWT_EXPIRY_HOURS=24
ADMIN_USERNAME=admin
ADMIN_PASSWORD=admin1234

View File

@@ -1,48 +0,0 @@
package main
import (
"fmt"
"log"
"os"
"a301_server/internal/auth"
"a301_server/pkg/config"
"a301_server/pkg/database"
"golang.org/x/crypto/bcrypt"
)
func main() {
config.Load()
if err := database.ConnectMySQL(); err != nil {
log.Fatalf("MySQL 연결 실패: %v", err)
}
username := getArg(1, "admin")
password := getArg(2, "admin1234")
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
log.Fatalf("비밀번호 해시 실패: %v", err)
}
user := auth.User{
Username: username,
PasswordHash: string(hash),
Role: auth.RoleAdmin,
}
repo := auth.NewRepository(database.DB)
if err := repo.Create(&user); err != nil {
log.Fatalf("관리자 계정 생성 실패: %v", err)
}
fmt.Printf("관리자 계정 생성 완료\n 아이디: %s\n 비밀번호: %s\n", username, password)
}
func getArg(index int, fallback string) string {
if len(os.Args) > index {
return os.Args[index]
}
return fallback
}

View File

@@ -64,3 +64,18 @@ func (s *Service) Logout(userID uint) {
key := fmt.Sprintf("session:%d", userID)
s.rdb.Del(context.Background(), key)
}
func (s *Service) EnsureAdmin(username, password string) error {
if _, err := s.repo.FindByUsername(username); err == nil {
return nil // 이미 존재하면 스킵
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
return s.repo.Create(&User{
Username: username,
PasswordHash: string(hash),
Role: RoleAdmin,
})
}

View File

@@ -35,6 +35,13 @@ func main() {
authSvc := auth.NewService(authRepo, database.RDB)
authHandler := auth.NewHandler(authSvc)
// 초기 admin 계정 생성
if err := authSvc.EnsureAdmin(config.C.AdminUsername, config.C.AdminPassword); err != nil {
log.Printf("admin 계정 생성 실패: %v", err)
} else {
log.Printf("admin 계정 확인 완료: %s", config.C.AdminUsername)
}
annRepo := announcement.NewRepository(database.DB)
annSvc := announcement.NewService(annRepo)
annHandler := announcement.NewHandler(annSvc)

View File

@@ -18,6 +18,8 @@ type Config struct {
RedisPassword string
JWTSecret string
JWTExpiryHours int
AdminUsername string
AdminPassword string
}
var C Config
@@ -37,6 +39,8 @@ func Load() {
RedisPassword: getEnv("REDIS_PASSWORD", ""),
JWTSecret: getEnv("JWT_SECRET", "secret"),
JWTExpiryHours: hours,
AdminUsername: getEnv("ADMIN_USERNAME", "admin"),
AdminPassword: getEnv("ADMIN_PASSWORD", "admin1234"),
}
}