diff --git a/build.gradle.kts b/build.gradle.kts index f01d4b0..1013c2c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,6 +1,7 @@ val ktor_version: String by project val kotlin_version: String by project val logback_version: String by project +val exposedVersion: String by project plugins { application @@ -29,6 +30,12 @@ dependencies { implementation("io.ktor:ktor-server-call-logging:$ktor_version") implementation("io.ktor:ktor-server-metrics-micrometer:$ktor_version") + implementation("org.jetbrains.exposed:exposed-core:$exposedVersion") + implementation("org.jetbrains.exposed:exposed-dao:$exposedVersion") + implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion") + implementation("org.postgresql:postgresql:42.3.6") + implementation("net.postgis:postgis-jdbc:2021.1.0") + testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version") testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version") } \ No newline at end of file diff --git a/createPostgis.sh b/createPostgis.sh new file mode 100755 index 0000000..6ee5da8 --- /dev/null +++ b/createPostgis.sh @@ -0,0 +1,2 @@ +#!/bin/zsh +docker run --name postgresql-pluckr -e POSTGRES_USER=pluckr -e POSTGRES_PASSWORD=$PLUCKR_PASSWORD -p 5432:5432 -v pluckr-data:/var/lib/postgresql/data -d postgis/postgis diff --git a/gradle.properties b/gradle.properties index 4ce4300..5453c37 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,3 +2,4 @@ ktor_version=2.0.3 kotlin_version=1.7.0 logback_version=1.2.11 kotlin.code.style=official +exposedVersion=0.38.2 diff --git a/src/main/kotlin/nl/lengrand/pluckr/Application.kt b/src/main/kotlin/nl/lengrand/pluckr/Application.kt index 3898e1e..d91bb49 100644 --- a/src/main/kotlin/nl/lengrand/pluckr/Application.kt +++ b/src/main/kotlin/nl/lengrand/pluckr/Application.kt @@ -5,13 +5,47 @@ import io.ktor.server.engine.* import io.ktor.server.metrics.micrometer.* import io.ktor.server.netty.* import io.ktor.server.plugins.callloging.* +import net.postgis.jdbc.geometry.Point import nl.lengrand.pluckr.plugins.* +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.transactions.transaction fun Application.myapp(){ + + val database = initDb() + install(CallLogging) install(MicrometerMetrics) - configureRouting() + configureRouting(database) +} + +fun initDb(): Database { + val database = Database.connect("jdbc:postgresql://localhost:5432/pluckr", driver = "org.postgresql.Driver", + user = "pluckr", password = System.getenv("PLUCKR_PASSWORD")) + + transaction { + addLogger(StdOutSqlLogger) + + SchemaUtils.create(Trees) + +// val first = Tree.new { +// name = "Laurier" +// description = "un laurier accessible à tous" +// location = Point(52.04681865145196, 5.079779509938945) +// } + + Trees.insert { + it[name] = "Laurier 2" + it[description] = "un laurier accessible à tous" + it[location] = Point(52.04681865145196, 5.079779509938945) + } + + +// println("Trees: ${Tree.all().joinToString {it.location.value}}") + + } + return database } fun main() { diff --git a/src/main/kotlin/nl/lengrand/pluckr/Database.kt b/src/main/kotlin/nl/lengrand/pluckr/Database.kt new file mode 100644 index 0000000..1dc350b --- /dev/null +++ b/src/main/kotlin/nl/lengrand/pluckr/Database.kt @@ -0,0 +1,42 @@ +package nl.lengrand.pluckr + +import net.postgis.jdbc.PGgeometry +import net.postgis.jdbc.geometry.Point +import org.jetbrains.exposed.dao.IntEntity +import org.jetbrains.exposed.dao.IntEntityClass +import org.jetbrains.exposed.dao.id.EntityID +import org.jetbrains.exposed.dao.id.IntIdTable +import org.jetbrains.exposed.sql.Column +import org.jetbrains.exposed.sql.ColumnType +import org.jetbrains.exposed.sql.Table + +object Trees : IntIdTable() { + val name = varchar("name", 100) + val description = text("description") + val location = point("location") +} + +fun Table.point(name: String, srid: Int = 4326): Column + = registerColumn(name, PointColumnType()) + +private class PointColumnType(val srid: Int = 4326): ColumnType() { + override fun sqlType() = "GEOMETRY(Point, $srid)" + + override fun valueFromDB(value: Any) = if (value is PGgeometry) value.geometry else value + + override fun notNullValueToDB(value: Any): Any { + if (value is Point) { + if (value.srid == Point.UNKNOWN_SRID) value.srid = srid + return PGgeometry(value) + } + return value + } +} + +class Tree(id: EntityID) : IntEntity(id) { + companion object : IntEntityClass(Trees) + + var name by Trees.name + var description by Trees.description + var location by Trees.location +} \ No newline at end of file diff --git a/src/main/kotlin/nl/lengrand/pluckr/plugins/Routing.kt b/src/main/kotlin/nl/lengrand/pluckr/plugins/Routing.kt index 42e9817..eb41f95 100644 --- a/src/main/kotlin/nl/lengrand/pluckr/plugins/Routing.kt +++ b/src/main/kotlin/nl/lengrand/pluckr/plugins/Routing.kt @@ -1,13 +1,12 @@ package nl.lengrand.pluckr.plugins import io.ktor.server.routing.* -import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.http.content.* import io.ktor.server.response.* -import io.ktor.server.request.* +import org.jetbrains.exposed.sql.Database -fun Application.configureRouting() { +fun Application.configureRouting(database: Database) { // Starting point for a Ktor app: routing {