-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
236 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
ok-marketplace-app-ktor/src/commonMain/kotlin/plugins/getDatabaseConf.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ru.otus.otuskotlin.marketplace.app.plugins | ||
|
||
import io.ktor.server.application.* | ||
import ru.otus.otuskotlin.marketplace.common.repo.IAdRepository | ||
|
||
expect fun Application.getDatabaseConf(type: AdDbType): IAdRepository | ||
|
||
enum class AdDbType(val confName: String) { | ||
PROD("prod"), TEST("test") | ||
} |
10 changes: 0 additions & 10 deletions
10
ok-marketplace-app-ktor/src/commonMain/resources/application.conf
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
ok-marketplace-app-ktor/src/commonMain/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# application.yaml работает в jvm и native, но не работает в режиме сервлета с Tomcat | ||
# в этом случае необходимо сформировать application.conf | ||
|
||
ktor: | ||
development: true | ||
deployment: | ||
port: 8080 | ||
watch: | ||
- classes | ||
- resources | ||
application: | ||
modules: | ||
- "ru.otus.otuskotlin.marketplace.app.ApplicationKt.module" | ||
|
23 changes: 23 additions & 0 deletions
23
ok-marketplace-app-ktor/src/jvmMain/kotlin/configs/CassandraConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ru.otus.otuskotlin.marketplace.app.configs | ||
|
||
import io.ktor.server.config.* | ||
|
||
data class CassandraConfig( | ||
val host: String = "localhost", | ||
val port: Int = 9042, | ||
val user: String = "cassandra", | ||
val pass: String = "cassandra", | ||
val keyspace: String = "test_keyspace" | ||
) { | ||
constructor(config: ApplicationConfig) : this( | ||
host = config.property("$PATH.host").getString(), | ||
port = config.property("$PATH.port").getString().toInt(), | ||
user = config.property("$PATH.user").getString(), | ||
pass = config.property("$PATH.pass").getString(), | ||
keyspace = config.property("$PATH.keyspace").getString() | ||
) | ||
|
||
companion object { | ||
const val PATH = "${ConfigPaths.repository}.cassandra" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
ok-marketplace-app-ktor/src/jvmMain/kotlin/configs/ConfigPaths.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package ru.otus.otuskotlin.marketplace.app.configs | ||
|
||
object ConfigPaths { | ||
const val mkplRoot = "marketplace" | ||
const val repository = "$mkplRoot.repository" | ||
} |
22 changes: 22 additions & 0 deletions
22
ok-marketplace-app-ktor/src/jvmMain/kotlin/configs/GremlinConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ru.otus.otuskotlin.marketplace.app.configs | ||
|
||
import io.ktor.server.config.* | ||
|
||
data class GremlinConfig( | ||
val host: String = "localhost", | ||
val port: Int = 8182, | ||
val user: String = "root", | ||
val pass: String, | ||
val enableSsl: Boolean = false, | ||
) { | ||
constructor(config: ApplicationConfig): this( | ||
host = config.property("$PATH.host").getString(), | ||
user = config.property("$PATH.user").getString(), | ||
pass = config.property("$PATH.password").getString(), | ||
enableSsl = config.property("$PATH.enableSsl").getString().toBoolean(), | ||
) | ||
|
||
companion object { | ||
const val PATH = "${ConfigPaths.repository}.gremlin" | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
ok-marketplace-app-ktor/src/jvmMain/kotlin/configs/PostgresConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ru.otus.otuskotlin.marketplace.app.configs | ||
|
||
import io.ktor.server.config.* | ||
|
||
data class PostgresConfig( | ||
val url: String = "jdbc:postgresql://localhost:5432/marketplace", | ||
val user: String = "postgres", | ||
val password: String = "marketplace-pass", | ||
val schema: String = "marketplace", | ||
) { | ||
constructor(config: ApplicationConfig): this( | ||
url = config.property("$PATH.url").getString(), | ||
user = config.property("$PATH.user").getString(), | ||
password = config.property("$PATH.password").getString(), | ||
schema = config.property("$PATH.schema").getString(), | ||
) | ||
|
||
companion object { | ||
const val PATH = "${ConfigPaths.repository}.psql" | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
ok-marketplace-app-ktor/src/jvmMain/kotlin/plugins/getDatabaseConf.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package ru.otus.otuskotlin.marketplace.app.plugins | ||
|
||
import io.ktor.server.application.* | ||
import ru.otus.otuskotlin.marketplace.app.configs.CassandraConfig | ||
import ru.otus.otuskotlin.marketplace.app.configs.ConfigPaths | ||
import ru.otus.otuskotlin.marketplace.app.configs.GremlinConfig | ||
import ru.otus.otuskotlin.marketplace.app.configs.PostgresConfig | ||
import ru.otus.otuskotlin.marketplace.backend.repo.cassandra.RepoAdCassandra | ||
import ru.otus.otuskotlin.marketplace.backend.repo.sql.RepoAdSQL | ||
import ru.otus.otuskotlin.marketplace.backend.repo.sql.SqlProperties | ||
import ru.otus.otuskotlin.marketplace.backend.repository.gremlin.AdRepoGremlin | ||
import ru.otus.otuskotlin.marketplace.common.repo.IAdRepository | ||
import ru.otus.otuskotlin.marketplace.repo.inmemory.AdRepoInMemory | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.minutes | ||
|
||
actual fun Application.getDatabaseConf(type: AdDbType): IAdRepository { | ||
val dbSettingPath = "${ConfigPaths.repository}.${type.confName}" | ||
val dbSetting = environment.config.propertyOrNull(dbSettingPath)?.getString()?.lowercase() | ||
return when (dbSetting) { | ||
"in-memory", "inmemory", "memory", "mem" -> initInMemory() | ||
"postgres", "postgresql", "pg", "sql", "psql" -> initPostgres() | ||
"cassandra", "nosql", "cass" -> initCassandra() | ||
"arcade", "arcadedb", "graphdb", "gremlin", "g", "a" -> initGremliln() | ||
else -> throw IllegalArgumentException( | ||
"$dbSettingPath must be set in application.yml to one of: " + | ||
"'inmemory', 'postgres', 'cassandra', 'gremlin'" | ||
) | ||
} | ||
} | ||
|
||
private fun Application.initPostgres(): IAdRepository { | ||
val config = PostgresConfig(environment.config) | ||
return RepoAdSQL( | ||
properties = SqlProperties( | ||
url = config.url, | ||
user = config.user, | ||
password = config.password, | ||
schema = config.schema, | ||
) | ||
) | ||
} | ||
|
||
private fun Application.initCassandra(): IAdRepository { | ||
val config = CassandraConfig(environment.config) | ||
return RepoAdCassandra( | ||
keyspaceName = config.keyspace, | ||
host = config.host, | ||
port = config.port, | ||
user = config.user, | ||
pass = config.pass, | ||
) | ||
} | ||
|
||
private fun Application.initGremliln(): IAdRepository { | ||
val config = GremlinConfig(environment.config) | ||
return AdRepoGremlin( | ||
hosts = config.host, | ||
port = config.port, | ||
user = config.user, | ||
pass = config.pass, | ||
enableSsl = config.enableSsl, | ||
) | ||
} | ||
|
||
private fun Application.initInMemory(): IAdRepository { | ||
val ttlSetting = environment.config.propertyOrNull("db.prod")?.getString()?.let { | ||
Duration.parse(it) | ||
} | ||
return AdRepoInMemory(ttl = ttlSetting ?: 10.minutes) | ||
} |
11 changes: 0 additions & 11 deletions
11
ok-marketplace-app-ktor/src/jvmMain/resources/application.conf
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
ok-marketplace-app-ktor/src/jvmMain/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# application.yaml работает в jvm и native, но не работает в режиме сервлета с Tomcat | ||
# в этом случае необходимо сформировать application.conf | ||
ktor: | ||
application: | ||
modules: | ||
- ru.otus.otuskotlin.marketplace.app.ApplicationJvmKt.moduleJvm | ||
deployment: | ||
port: 8080 | ||
urls: | ||
- "http://127.0.0.1:8080/" | ||
- "http://0.0.0.0:8080/" | ||
- "http://192.168.0.182:8080/" | ||
watch: | ||
- classes | ||
- resources | ||
marketplace: | ||
repository: | ||
test: "$DB_TYPE_TEST:inmemory" | ||
prod: "$DB_TYPE_PROD:inmemory" | ||
cassandra: | ||
hosts: localhost | ||
keyspace: test_keyspace | ||
pass: cassandra | ||
port: 9042 | ||
user: cassandra | ||
psql: | ||
password: marketplace-pass | ||
schema: marketplace | ||
url: "jdbc:postgresql://localhost:5432/marketplace" | ||
user: postgres | ||
gremlin: | ||
host: "$DB_GREMLIN_HOST:localhost" | ||
user: "$DB_GREMLIN_HOST:root" | ||
password: "$DB_GREMLIN_HOST:root_root" | ||
port: "$DB_GREMLIN_PORT:8182" | ||
enableSsl: false |
9 changes: 9 additions & 0 deletions
9
ok-marketplace-app-ktor/src/linuxX64Main/kotlin/getDatabaseConf.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ru.otus.otuskotlin.marketplace.app.plugins | ||
|
||
import io.ktor.server.application.* | ||
import ru.otus.otuskotlin.marketplace.common.repo.IAdRepository | ||
import ru.otus.otuskotlin.marketplace.repo.inmemory.AdRepoInMemory | ||
|
||
actual fun Application.getDatabaseConf(type: AdDbType): IAdRepository { | ||
return AdRepoInMemory() | ||
} |
9 changes: 9 additions & 0 deletions
9
ok-marketplace-app-ktor/src/macosArm64Main/kotlin/getDatabaseConf.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ru.otus.otuskotlin.marketplace.app.plugins | ||
|
||
import io.ktor.server.application.* | ||
import ru.otus.otuskotlin.marketplace.common.repo.IAdRepository | ||
import ru.otus.otuskotlin.marketplace.repo.inmemory.AdRepoInMemory | ||
|
||
actual fun Application.getDatabaseConf(type: AdDbType): IAdRepository { | ||
return AdRepoInMemory() | ||
} |
9 changes: 9 additions & 0 deletions
9
ok-marketplace-app-ktor/src/macosX64Main/kotlin/getDatabaseConf.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ru.otus.otuskotlin.marketplace.app.plugins | ||
|
||
import io.ktor.server.application.* | ||
import ru.otus.otuskotlin.marketplace.common.repo.IAdRepository | ||
import ru.otus.otuskotlin.marketplace.repo.inmemory.AdRepoInMemory | ||
|
||
actual fun Application.getDatabaseConf(type: AdDbType): IAdRepository { | ||
return AdRepoInMemory() | ||
} |