본문 바로가기

Backend/Kotlin

Ktor Framework 프로젝트 생성

Ktor Framework 알게된 후부터 지켜보기만 하고 있었어요.

귀찮아서 프로젝트도 만들어 본 적이 없었죠..

여기저기서 쓰고 좋다는 평이 많아서 일단 세팅하고 써보려고 합니다.

 

툴은 IntelliJ를 사용할거에요.

IntelliJ에서 하셔도 되고 아래 URL에서 프로젝트를 세팅하실 수도 있어요.

https://start.ktor.io/

 

Generate Ktor project

 

start.ktor.io

 

기본적인 프로젝트 정보입니다.

 

그 다음에 이런저런 플러그인들이 나오는데 스프링 부트를 써보셨으면 대강 머가먼지 이해가 쉬우실거에요.

그래도 먼가 애매해서 선택안하고 완료해봅시다.

 

패키지 구조

이런 모습입니다.

제가 무언가를 잘못한 건 없는 것 같은데 .gitignore 파일은 안 만들어주는 것 같네요?

git에 올릴 때 조심해주세요.

.gitignore 파일을 가져와서 넣었습니다. (Spring Initializr에서 만들어주는 것 그대로 넣어줘도 될 것 같아요.)

 

 

.gitignore

HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

 

Application.kt

fun main() {
  embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
    configureRouting()
  }.start(wait = true)
}

Routing.kt

fun Application.configureRouting() {

  // Starting point for a Ktor app:
  routing {
    get("/") {
      call.respondText("Hello World!")
    }
  }
  routing {
  }
}

logback.xml

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  <root level="trace">
    <appender-ref ref="STDOUT"/>
  </root>
  <logger name="org.eclipse.jetty" level="INFO"/>
  <logger name="io.netty" level="INFO"/>
</configuration>

ApplicationTest.kt

class ApplicationTest {
}

build.gradle.kts

val ktor_version: String by project
val kotlin_version: String by project
val logback_version: String by project

plugins {
  application
  kotlin("jvm") version "1.5.31"
}

group = "com.example"
version = "0.0.1"
application {
  mainClass.set("com.example.ApplicationKt")
}

repositories {
  mavenCentral()
}

dependencies {
  implementation("io.ktor:ktor-server-core:$ktor_version")
  implementation("io.ktor:ktor-server-netty:$ktor_version")
  implementation("ch.qos.logback:logback-classic:$logback_version")
  testImplementation("io.ktor:ktor-server-tests:$ktor_version")
  testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}

 

실행한 모습이구여 콘솔창에 http://0.0.0.0:8080을 클릭하면

Routing.kt에 코딩된대로 Hello World!가 찍히는 것을 볼 수 있어요.

 

플러그인까지 설치해줍시다.

https://plugins.jetbrains.com/plugin/16008-ktor

 

Ktor - IntelliJ IDEs Plugin | Marketplace

Ktor is a web application framework for creating connected systems.

plugins.jetbrains.com

 

위에 버전이 맞지 않는다구 설치가 안되어 힘빼고 싶지 않아 아래껄 설치해봤어요.

다운로드 수가 더 많긴 하네요.

https://plugins.jetbrains.com/plugin/10823-ktor-obsolete-

 

Ktor (Obsolete) - IntelliJ IDEs Plugin | Marketplace

Please note that this plugin is being replaced and will be not supported beyond Ktor 1.5.3.

plugins.jetbrains.com

 

 

먼가 패키지 구조도 Spring이랑 비슷하네요.

API 와 DB 연동부터 다양하게 해볼 계획입니다.

'Backend > Kotlin' 카테고리의 다른 글

Kotlin 랜덤 숫자 000000~999999  (0) 2022.04.17
Ktor Ktorm DB 연동 (매우 간단)  (0) 2021.12.02
Ktor Install Gson  (0) 2021.11.16
Ktor Application 빌드 오류 시 (Shadow Plugin)  (0) 2021.11.12