feat: 환경변수로 초기 admin 계정 자동 생성
서버 시작 시 ADMIN_USERNAME, ADMIN_PASSWORD 환경변수 기반으로 admin 계정이 없을 경우 자동 생성 (이미 있으면 스킵) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -11,3 +11,6 @@ REDIS_PASSWORD=
|
|||||||
|
|
||||||
JWT_SECRET=your-secret-key-here
|
JWT_SECRET=your-secret-key-here
|
||||||
JWT_EXPIRY_HOURS=24
|
JWT_EXPIRY_HOURS=24
|
||||||
|
|
||||||
|
ADMIN_USERNAME=admin
|
||||||
|
ADMIN_PASSWORD=admin1234
|
||||||
@@ -64,3 +64,18 @@ func (s *Service) Logout(userID uint) {
|
|||||||
key := fmt.Sprintf("session:%d", userID)
|
key := fmt.Sprintf("session:%d", userID)
|
||||||
s.rdb.Del(context.Background(), key)
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
7
main.go
7
main.go
@@ -35,6 +35,13 @@ func main() {
|
|||||||
authSvc := auth.NewService(authRepo, database.RDB)
|
authSvc := auth.NewService(authRepo, database.RDB)
|
||||||
authHandler := auth.NewHandler(authSvc)
|
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)
|
annRepo := announcement.NewRepository(database.DB)
|
||||||
annSvc := announcement.NewService(annRepo)
|
annSvc := announcement.NewService(annRepo)
|
||||||
annHandler := announcement.NewHandler(annSvc)
|
annHandler := announcement.NewHandler(annSvc)
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ type Config struct {
|
|||||||
RedisPassword string
|
RedisPassword string
|
||||||
JWTSecret string
|
JWTSecret string
|
||||||
JWTExpiryHours int
|
JWTExpiryHours int
|
||||||
|
AdminUsername string
|
||||||
|
AdminPassword string
|
||||||
}
|
}
|
||||||
|
|
||||||
var C Config
|
var C Config
|
||||||
@@ -37,6 +39,8 @@ func Load() {
|
|||||||
RedisPassword: getEnv("REDIS_PASSWORD", ""),
|
RedisPassword: getEnv("REDIS_PASSWORD", ""),
|
||||||
JWTSecret: getEnv("JWT_SECRET", "secret"),
|
JWTSecret: getEnv("JWT_SECRET", "secret"),
|
||||||
JWTExpiryHours: hours,
|
JWTExpiryHours: hours,
|
||||||
|
AdminUsername: getEnv("ADMIN_USERNAME", "admin"),
|
||||||
|
AdminPassword: getEnv("ADMIN_PASSWORD", "admin1234"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user