Skip to content

Commit

Permalink
H2 version extracted to properties file, DB configuration keys moved …
Browse files Browse the repository at this point in the history
…to the application.yaml , changed routes and project description at README.md , DatabaseFactory renamed to DatabaseSingleton for consistency
  • Loading branch information
VGoncharova committed Jan 11, 2024
1 parent f238d8d commit 5d41ee8
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 37 deletions.
8 changes: 6 additions & 2 deletions mvc-web/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# MVC application for Ktor
# MVC application with Ktor Server

An application for adding and removing wishes to wishlist that uses Freemarker templates and Exposed.

Expand All @@ -10,4 +10,8 @@ Execute this command to run the sample:
./gradlew run
```

Then, navigate to [http://localhost:8080/](http://localhost:8080/) to see the sample home page.localhost:8080/](http://localhost:8080/) in a browser to create, edit, and delete articles.
Then, navigate to the following pages:

[http://localhost:8080/wish/list](http://localhost:8080/wish/list) to view and modify the wishlist.

[http://localhost:8080/wish/topwishes](http://localhost:8080/wish/topwishes) to see the top popular wishes.
4 changes: 3 additions & 1 deletion mvc-web/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
val kotlin_version: String by project
val logback_version: String by project
val h2_version: String by project
val exposed_version: String by project

plugins {
Expand All @@ -25,10 +26,11 @@ repositories {

dependencies {
implementation("io.ktor:ktor-server-core-jvm")
implementation("io.ktor:ktor-server-config-yaml-jvm")
implementation("org.jetbrains.exposed:exposed-core:$exposed_version")
implementation("org.jetbrains.exposed:exposed-dao:$exposed_version")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version")
implementation("com.h2database:h2:2.1.214")
implementation("com.h2database:h2:$h2_version")
implementation("io.ktor:ktor-server-netty-jvm")
implementation("io.ktor:ktor-server-freemarker")
implementation("ch.qos.logback:logback-classic:$logback_version")
Expand Down
1 change: 1 addition & 0 deletions mvc-web/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
kotlin_version=1.9.20
logback_version=1.4.11
exposed_version=0.41.1
h2_version=2.1.214
kotlin.code.style=official
17 changes: 9 additions & 8 deletions mvc-web/src/main/kotlin/com/example/Application.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.example

import com.example.model.DatabaseFactory
import com.example.plugins.*
import com.example.model.DatabaseSingleton
import com.example.plugins.configureRouting
import com.example.plugins.configureTemplating
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*

fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
.start(wait = true)
}
fun main(args: Array<String>): Unit = EngineMain.main(args)

fun Application.module() {
DatabaseFactory.init()
DatabaseSingleton.init(
environment.config.property("ktor.application.datasource.url").getString(),
environment.config.property("ktor.application.datasource.driverClassName").getString()
)

configureTemplating()
configureRouting()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction

object DatabaseFactory {
fun init() {
val driverClassName = "org.h2.Driver"
val jdbcURL = "jdbc:h2:file:./build/db"
object DatabaseSingleton {
fun init(jdbcURL: String, driverClassName: String) {
val database = Database.connect(jdbcURL, driverClassName)
transaction(database) {
SchemaUtils.create(Wishlists)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.model.dao.wishlist

import com.example.exceptions.DbElementInsertException
import com.example.model.DatabaseFactory.dbQuery
import com.example.model.DatabaseSingleton.dbQuery
import com.example.model.entity.Wishlist
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
Expand Down
9 changes: 9 additions & 0 deletions mvc-web/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ktor:
application:
modules:
- com.example.ApplicationKt.module
datasource:
url: "jdbc:h2:file:./build/db"
driverClassName: "org.h2.Driver"
deployment:
port: 8080
21 changes: 0 additions & 21 deletions mvc-web/src/test/kotlin/com/example/ApplicationTest.kt

This file was deleted.

0 comments on commit 5d41ee8

Please sign in to comment.