First database

This commit is contained in:
Julien Lengrand-Lambert
2022-07-07 16:25:49 +02:00
parent b6f014da40
commit ac86da3381
6 changed files with 89 additions and 4 deletions

View File

@@ -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")
}

2
createPostgis.sh Executable file
View File

@@ -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

View File

@@ -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

View File

@@ -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() {

View File

@@ -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<Point>
= 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<Int>) : IntEntity(id) {
companion object : IntEntityClass<Tree>(Trees)
var name by Trees.name
var description by Trees.description
var location by Trees.location
}

View File

@@ -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 {