๐Ÿ’ก Ktor์—์„œ HoconApplicationConfig๋กœ YAML ์„ค์ •์ด ์•ˆ๋  ๋•Œ ํ•ด๊ฒฐ๋ฒ• (.conf ๋„์ž…) (์‚ฝ์งˆ๊ธฐ๋ก)

์—…๋ฐ์ดํŠธ:
1 ๋ถ„ ์†Œ์š”

๐Ÿ’ก Ktor์—์„œ HoconApplicationConfig๋กœ YAML ์„ค์ •์ด ์•ˆ๋  ๋•Œ ํ•ด๊ฒฐ๋ฒ• (.conf ๋„์ž…)


๐Ÿงฉ ๋ฐœ์ƒ ์ƒํ™ฉ

Ktor ํ”„๋กœ์ ํŠธ์—์„œ ์„ค์ • ์ •๋ณด๋ฅผ application.yaml์— ์ž‘์„ฑํ•˜๊ณ  ์•„๋ž˜์ฒ˜๋Ÿผ ๋ถˆ๋Ÿฌ์˜ค๋ ค ํ–ˆ์Šต๋‹ˆ๋‹ค.

val config = HoconApplicationConfig(ConfigFactory.load("application.yaml"))

ํ•˜์ง€๋งŒ ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์ด๋‚˜ ์ปค์Šคํ…€ ๋Ÿฐํƒ€์ž„ ํ™˜๊ฒฝ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค:

Unresolved substitution
Unresolved reference
Missing configuration for 'jwt.issuer'

๐Ÿ“Œ ์›์ธ ๋ถ„์„

ConfigFactory.load("application.yaml")
  • โŒ application.yaml์€ ๊ธฐ๋ณธ ์ง€์›ํ•˜์ง€ ์•Š์Œ
  • โœ… ConfigFactory๋Š” .conf, .json, .properties, .env ๋“ฑ๋งŒ ์ง์ ‘ ํŒŒ์‹ฑ ๊ฐ€๋Šฅ
  • .yaml์€ Typesafe Config ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(HOCON)์—์„œ ์ง์ ‘ ํŒŒ์‹ฑํ•˜์ง€ ์•Š์Œ
  • ๋ณ„๋„์˜ YAML ํŒŒ์„œ๋ฅผ ๋ถ™์ด์ง€ ์•Š๋Š” ์ด์ƒ, YAML์€ ์ฝํžˆ์ง€ ์•Š์Œ

๐Ÿ”ง ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•: .yaml โ†’ .conf ์ „ํ™˜

๐Ÿ” ๊ธฐ์กด .yaml ์„ค์ • ์˜ˆ์‹œ

# application.yaml
jwt:
  secret: "secret-key"
  issuer: "issuer-value"

โœ… ๋ณ€ํ™˜๋œ .conf ์„ค์ •

# application-test.conf
jwt {
  secret = "secret-key"
  issuer = "issuer-value"
}

๐Ÿ’ก ๋ณ€๊ฒฝ ํ›„ ์ฝ”๋“œ

val config = HoconApplicationConfig(ConfigFactory.load("application-test.conf"))

โœ… .conf๋Š” HOCON ํฌ๋งท์œผ๋กœ, .yaml๋ณด๋‹ค ๋” ์œ ์—ฐํ•˜๊ณ  include, reference ๋“ฑ๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿงช ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ์˜ ํ™œ์šฉ ์˜ˆ์‹œ

class AuthServiceTest {

    private val config = HoconApplicationConfig(ConfigFactory.load("application-test.conf"))

    private val database = DatabaseFactory.init(config)
    private val jwtConfig = JwtConfig.init(config) // custom config
    private val authService = AuthService(repository, jwtConfig)

    // ...ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ
}

โœ… ํŒ

  • ํ…Œ์ŠคํŠธ์šฉ .conf ํŒŒ์ผ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ํ™˜๊ฒฝ์— ๋งž๋Š” ์„ค์ •๋งŒ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ
  • ์—ฌ๋Ÿฌ ํ™˜๊ฒฝ๋ณ„ ์„ค์ •์„ include๋กœ ๋‚˜๋ˆ ๋„ ๊ฐ€๋Šฅ

๐Ÿง  ์ •๋ฆฌ

ํ•ญ๋ชฉ ๋‚ด์šฉ
๋ฌธ์ œ application.yaml์„ HoconApplicationConfig๋กœ ๋กœ๋“œํ•˜๋ ค ํ–ˆ์ง€๋งŒ ์‹คํŒจ
์›์ธ Typesafe Config๋Š” .yaml ํฌ๋งท์„ ๊ธฐ๋ณธ ์ง€์›ํ•˜์ง€ ์•Š์Œ
ํ•ด๊ฒฐ์ฑ… .yaml โ†’ .conf ๋ณ€ํ™˜ ํ›„ ConfigFactory.load()๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
์žฅ์  .conf(HOCON)์€ ์œ ์—ฐํ•˜๋ฉฐ, include ๋ฐ ๋ณ€์ˆ˜ ์น˜ํ™˜ ๊ธฐ๋Šฅ๋„ ์ œ๊ณต

โœ… ๊ฒฐ๋ก 

  • Spring๊ณผ ๋‹ค๋ฅด๊ฒŒ Ktor๋Š” ์„ค์ • ๋กœ๋”ฉ ์‹œ .yaml ์ง€์›์ด ์ œํ•œ์ ์ž…๋‹ˆ๋‹ค.
  • Kotlin/Ktor ํ™˜๊ฒฝ์—์„œ๋Š” ๊ฐ€๋Šฅํ•˜๋ฉด .conf ํฌ๋งท์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์•ˆ์ •์ ์ด๊ณ  ์œ ์—ฐํ•ฉ๋‹ˆ๋‹ค.
  • .conf๋Š” ๋ฌธ๋ฒ•๋„ ์ง๊ด€์ ์ด๋ฉฐ include, fallback, substitution ๋“ฑ์„ ์ง€์›ํ•ด ํ™˜๊ฒฝ ๊ตฌ์„ฑ์— ๋งค์šฐ ๊ฐ•๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ Ktor ํ…Œ์ŠคํŠธ์—์„œ ์„ค์ •์ด ์ œ๋Œ€๋กœ ์•ˆ ์ฝํžŒ๋‹ค๋ฉด, YAML์ด ์•„๋‹Œ .conf ํŒŒ์ผ์„ ์“ฐ๋Š” ์Šต๊ด€์„ ๋“ค์ด์ž! ๐Ÿ’ช

์ˆ˜์ •๋œ ์ฝ”๋“œ

refactor : issue 1 ํ•ด๊ฒฐ

ํƒœ๊ทธ: , , , , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ: , ,

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ