mirror of
https://github.com/jlengrand/pluckr.git
synced 2026-03-10 08:41:17 +00:00
First database
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
val ktor_version: String by project
|
val ktor_version: String by project
|
||||||
val kotlin_version: String by project
|
val kotlin_version: String by project
|
||||||
val logback_version: String by project
|
val logback_version: String by project
|
||||||
|
val exposedVersion: String by project
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
application
|
application
|
||||||
@@ -29,6 +30,12 @@ dependencies {
|
|||||||
implementation("io.ktor:ktor-server-call-logging:$ktor_version")
|
implementation("io.ktor:ktor-server-call-logging:$ktor_version")
|
||||||
implementation("io.ktor:ktor-server-metrics-micrometer:$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("io.ktor:ktor-server-tests-jvm:$ktor_version")
|
||||||
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
|
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
|
||||||
}
|
}
|
||||||
2
createPostgis.sh
Executable file
2
createPostgis.sh
Executable 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
|
||||||
@@ -2,3 +2,4 @@ ktor_version=2.0.3
|
|||||||
kotlin_version=1.7.0
|
kotlin_version=1.7.0
|
||||||
logback_version=1.2.11
|
logback_version=1.2.11
|
||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
|
exposedVersion=0.38.2
|
||||||
|
|||||||
@@ -5,13 +5,47 @@ import io.ktor.server.engine.*
|
|||||||
import io.ktor.server.metrics.micrometer.*
|
import io.ktor.server.metrics.micrometer.*
|
||||||
import io.ktor.server.netty.*
|
import io.ktor.server.netty.*
|
||||||
import io.ktor.server.plugins.callloging.*
|
import io.ktor.server.plugins.callloging.*
|
||||||
|
import net.postgis.jdbc.geometry.Point
|
||||||
import nl.lengrand.pluckr.plugins.*
|
import nl.lengrand.pluckr.plugins.*
|
||||||
|
import org.jetbrains.exposed.sql.*
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
fun Application.myapp(){
|
fun Application.myapp(){
|
||||||
|
|
||||||
|
val database = initDb()
|
||||||
|
|
||||||
install(CallLogging)
|
install(CallLogging)
|
||||||
install(MicrometerMetrics)
|
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() {
|
fun main() {
|
||||||
|
|||||||
42
src/main/kotlin/nl/lengrand/pluckr/Database.kt
Normal file
42
src/main/kotlin/nl/lengrand/pluckr/Database.kt
Normal 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
|
||||||
|
}
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
package nl.lengrand.pluckr.plugins
|
package nl.lengrand.pluckr.plugins
|
||||||
|
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
import io.ktor.http.*
|
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
import io.ktor.server.http.content.*
|
import io.ktor.server.http.content.*
|
||||||
import io.ktor.server.response.*
|
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:
|
// Starting point for a Ktor app:
|
||||||
routing {
|
routing {
|
||||||
|
|||||||
Reference in New Issue
Block a user