Skip to content

Commit d411542

Browse files
committed
Fixed method to get information from MongoDb
1 parent 96b4781 commit d411542

File tree

5 files changed

+226
-98
lines changed

5 files changed

+226
-98
lines changed

Dockerfile

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ RUN gradle buildFatJar --no-daemon
2323
FROM amazoncorretto:21 AS runtime
2424
EXPOSE 8080/tcp
2525
RUN mkdir /app
26-
COPY --from=build /home/gradle/src/build/libs/*.jar /app/app.jar
27-
COPY entrypoint.sh /app/entrypoint.sh
28-
RUN chmod +x /app/entrypoint.sh
29-
ENTRYPOINT ["/app/entrypoint.sh"]
26+
COPY --from=build /home/gradle/src/build/libs/aruppi-api-all.jar /app/app.jar
27+
COPY entrypoint.sh /entrypoint.sh
28+
RUN chmod +x /entrypoint.sh
29+
ENTRYPOINT ["/entrypoint.sh"]

entrypoint.sh

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
# Check if MONGO_CONNECTION_STRING is defined
44
if [ -z "$MONGO_CONNECTION_STRING" ]; then
5-
echo "ERROR: The environment variable for the database, MONGO_CONNECTION_STRING is not defined."
5+
>&2 echo -e "ERROR: The environment variable MONGO_CONNECTION_STRING is not defined.\n\tYou must provide a valid Connection String"
66
exit 1
77
fi
88

99
# Verificar si MONGO_DATABASE_NAME está definido
1010
if [ -z "$MONGO_DATABASE_NAME" ]; then
11-
echo "WARNING: The environment variable for the database, MONGO_DATABASE_NAME, is not defined. It is using ‘mongodb’ as the default value."
12-
MONGO_DATABASE_NAME="mongodb"
11+
>&2 echo -e "WARNING: The environment variable for the database, MONGO_DATABASE_NAME, is not defined.\n\tUsing ‘aruppi’ as default value."
12+
MONGO_DATABASE_NAME="aruppi"
1313
fi
1414

1515

@@ -25,10 +25,12 @@ ktor:
2525
2626
db:
2727
mongo:
28-
connectionStrings: "${MONGO_CONNECTION_STRING}"
28+
connectionStrings: ${MONGO_CONNECTION_STRING}
2929
database:
30-
name: "${MONGO_DATABASE_NAME:-mongodb}"
30+
name: ${MONGO_DATABASE_NAME:-mongodb}
3131
EOF
3232

33+
cd /app/
34+
3335
# Run the application with the specified configuration
3436
exec java -Dconfig.file=/app/application.yaml -jar /app/app.jar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.jeluchu.core.extensions
2+
3+
import org.bson.Document
4+
import java.text.SimpleDateFormat
5+
import java.util.*
6+
7+
fun Document.getStringSafe(key: String, defaultValue: String = ""): String {
8+
return try {
9+
when (val value = this[key]) {
10+
is String -> value
11+
is Number, is Boolean -> value.toString()
12+
else -> defaultValue
13+
}
14+
} catch (e: Exception) {
15+
defaultValue
16+
}
17+
}
18+
19+
fun Document.getIntSafe(key: String, defaultValue: Int = 0): Int {
20+
return try {
21+
when (val value = this[key]) {
22+
is Int -> value
23+
is Number -> value.toInt()
24+
is String -> value.toIntOrNull() ?: defaultValue
25+
else -> defaultValue
26+
}
27+
} catch (e: Exception) {
28+
defaultValue
29+
}
30+
}
31+
32+
fun Document.getDoubleSafe(key: String, defaultValue: Double = 0.0): Double {
33+
return try {
34+
when (val value = this[key]) {
35+
is Double -> value
36+
is Number -> value.toDouble()
37+
is String -> value.toDoubleOrNull() ?: defaultValue
38+
else -> defaultValue
39+
}
40+
} catch (e: Exception) {
41+
defaultValue
42+
}
43+
}
44+
45+
fun Document.getFloatSafe(key: String, defaultValue: Float = 0.0f): Float {
46+
return try {
47+
when (val value = this[key]) {
48+
is Float -> value
49+
is Number -> value.toFloat()
50+
is String -> value.toFloatOrNull() ?: defaultValue
51+
else -> defaultValue
52+
}
53+
} catch (e: Exception) {
54+
defaultValue
55+
}
56+
}
57+
58+
fun Document.getLongSafe(key: String, defaultValue: Long = 0L): Long {
59+
return try {
60+
val value = this.get(key)
61+
when (value) {
62+
is Long -> value
63+
is Number -> value.toLong()
64+
is String -> value.toLongOrNull() ?: defaultValue
65+
else -> defaultValue
66+
}
67+
} catch (e: Exception) {
68+
defaultValue
69+
}
70+
}
71+
72+
fun Document.getBooleanSafe(key: String, defaultValue: Boolean = false): Boolean {
73+
return try {
74+
when (val value = this[key]) {
75+
is Boolean -> value
76+
is String -> value.toBoolean()
77+
is Number -> value.toInt() != 0
78+
else -> defaultValue
79+
}
80+
} catch (e: Exception) {
81+
defaultValue
82+
}
83+
}
84+
85+
fun Document.getDateSafe(key: String, defaultValue: Date = Date(0)): Date {
86+
return try {
87+
when (val value = this[key]) {
88+
is Date -> value
89+
is String -> SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(value) ?: defaultValue
90+
else -> defaultValue
91+
}
92+
} catch (e: Exception) {
93+
defaultValue
94+
}
95+
}
96+
97+
inline fun <reified T> Document.getListSafe(key: String, defaultValue: List<T> = emptyList()): List<T> {
98+
return try {
99+
when (val value = this[key]) {
100+
is List<*> -> value.filterIsInstance<T>()
101+
else -> defaultValue
102+
}
103+
} catch (e: Exception) {
104+
defaultValue
105+
}
106+
}
107+
108+
fun Document.getDocumentSafe(key: String): Document? {
109+
return try {
110+
val value = this[key]
111+
if (value is Document) value else null
112+
} catch (e: Exception) {
113+
null
114+
}
115+
}

0 commit comments

Comments
 (0)