forked from kugchennai/KtorServer-Mongo-Thymeleaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabases.kt
87 lines (80 loc) · 3.35 KB
/
Databases.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package example.com.plugins
import com.mongodb.client.MongoClients
import com.mongodb.client.MongoDatabase
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.config.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
fun Application.configureDatabases() {
val mongoDatabase = connectToMongoDB()
val carService = CarService(mongoDatabase)
routing {
// Create car
post("/cars") {
val car = call.receive<Car>()
val id = carService.create(car)
call.respond(HttpStatusCode.Created, id)
}
// Read car
get("/cars/{id}") {
val id = call.parameters["id"] ?: throw IllegalArgumentException("No ID found")
carService.read(id)?.let { car ->
call.respond(car)
} ?: call.respond(HttpStatusCode.NotFound)
}
// GetAll
get("/cars") {
call.respond(carService.getAll())
}
// Update car
put("/cars/{id}") {
val id = call.parameters["id"] ?: throw IllegalArgumentException("No ID found")
val car = call.receive<Car>()
carService.update(id, car)?.let {
call.respond(HttpStatusCode.OK)
} ?: call.respond(HttpStatusCode.NotFound)
}
// Delete car
delete("/cars/{id}") {
val id = call.parameters["id"] ?: throw IllegalArgumentException("No ID found")
carService.delete(id)?.let {
call.respond(HttpStatusCode.OK)
} ?: call.respond(HttpStatusCode.NotFound)
}
}
}
/**
* Establishes connection with a MongoDB database.
*
* The following configuration properties (in application.yaml/application.conf) can be specified:
* * `db.mongo.user` username for your database
* * `db.mongo.password` password for the user
* * `db.mongo.host` host that will be used for the database connection
* * `db.mongo.port` port that will be used for the database connection
* * `db.mongo.maxPoolSize` maximum number of connections to a MongoDB server
* * `db.mongo.database.name` name of the database
*
* IMPORTANT NOTE: in order to make MongoDB connection working, you have to start a MongoDB server first.
* See the instructions here: https://www.mongodb.com/docs/manual/administration/install-community/
* all the paramaters above
*
* @returns [MongoDatabase] instance
* */
fun Application.connectToMongoDB(): MongoDatabase {
val user = environment.config.tryGetString("db.mongo.user")
val password = environment.config.tryGetString("db.mongo.password")
val host = environment.config.tryGetString("db.mongo.host") ?: "127.0.0.1"
val port = environment.config.tryGetString("db.mongo.port") ?: "27017"
val maxPoolSize = environment.config.tryGetString("db.mongo.maxPoolSize")?.toInt() ?: 20
val databaseName = environment.config.tryGetString("db.mongo.database.name") ?: "myDatabase"
val credentials = user?.let { userVal -> password?.let { passwordVal -> "$userVal:$passwordVal@" } }.orEmpty()
val uri = "mongodb://$credentials$host:$port/?maxPoolSize=$maxPoolSize&w=majority"
val mongoClient = MongoClients.create(uri)
val database = mongoClient.getDatabase(databaseName)
environment.monitor.subscribe(ApplicationStopped) {
mongoClient.close()
}
return database
}