Add database path --do-not-notify parameters to backend

This commit is contained in:
Matte23
2021-01-22 15:21:30 +01:00
parent 6246a47b8f
commit 78bfec6919
4 changed files with 37 additions and 17 deletions

View File

@@ -27,15 +27,15 @@ object JavaDatabase {
@Volatile @Volatile
private var instance: AppDatabase? = null private var instance: AppDatabase? = null
private fun getInstance(): AppDatabase { private fun getInstance(path: String): AppDatabase {
return instance ?: synchronized(this) { return instance ?: synchronized(this) {
instance ?: AppDatabase( instance ?: AppDatabase(
DatabaseDriverFactory().createDriver() DatabaseDriverFactory(path).createDriver()
).also { instance = it } ).also { instance = it }
} }
} }
fun getDaoInstance(): CircularDao { fun getDaoInstance(path: String): CircularDao {
return CircularDao(getInstance()) return CircularDao(getInstance(path))
} }
} }

View File

@@ -28,13 +28,29 @@ const val CHECK_DELAY_MILLIS = 900000L
fun main(args: Array<String>) { fun main(args: Array<String>) {
print("Starting Circolapp Push microservice \n") print("Starting Circolapp Push microservice \n")
if (args.isEmpty()) {
print("ERROR: Database path not specified! Please specify the database path as a parameter \n")
return
} else {
print("Database path is: " + args[0] + " \n")
}
var enableNotifications = true
if (args.size >= 2 && args[1] == "--do-not-notify") {
print("Notifications are disabled \n")
enableNotifications = false
}
print("Initializing Firebase SDK \n")
val options = FirebaseOptions.builder() val options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.getApplicationDefault()) .setCredentials(GoogleCredentials.getApplicationDefault())
.build() .build()
FirebaseApp.initializeApp(options) FirebaseApp.initializeApp(options)
val serverUtils = ServerUtils() print("Initializing database and server interfaces \n")
val serverUtils = ServerUtils(args[0], enableNotifications)
print("Microservice started! \n") print("Microservice started! \n")
runBlocking { runBlocking {

View File

@@ -23,10 +23,10 @@ import net.underdesk.circolapp.shared.server.KtorFactory
import net.underdesk.circolapp.shared.server.Server import net.underdesk.circolapp.shared.server.Server
import net.underdesk.circolapp.shared.server.ServerAPI import net.underdesk.circolapp.shared.server.ServerAPI
class ServerUtils { class ServerUtils(databasePath: String, private val enableNotifications: Boolean = true) {
private val serverList: MutableList<Server> = mutableListOf() private val serverList: MutableList<Server> = mutableListOf()
private val ktorClient = KtorFactory().createClient() private val ktorClient = KtorFactory().createClient()
private val circularDao = JavaDatabase.getDaoInstance() private val circularDao = JavaDatabase.getDaoInstance(databasePath)
init { init {
for (serverId in ServerAPI.Companion.Servers.values()) { for (serverId in ServerAPI.Companion.Servers.values()) {
@@ -75,14 +75,18 @@ class ServerUtils {
if (newCirculars.second >= 0 && newCirculars.first.isNotEmpty()) { if (newCirculars.second >= 0 && newCirculars.first.isNotEmpty()) {
for (circular in newCirculars.first) { for (circular in newCirculars.first) {
PushNotificationUtils.createPushNotification( if (enableNotifications) {
circular, PushNotificationUtils.createPushNotification(
ServerAPI.getServerTopic(server.serverID) circular,
) ServerAPI.getServerTopic(server.serverID)
PushNotificationUtils.createPushNotificationiOS( )
circular, PushNotificationUtils.createPushNotificationiOS(
ServerAPI.getServerTopic(server.serverID) circular,
) ServerAPI.getServerTopic(server.serverID)
)
} else {
print("Skipping notification for circular ${circular.id} of school ${circular.school} \n")
}
} }
} }
} }

View File

@@ -21,9 +21,9 @@ package net.underdesk.circolapp.shared.data
import com.squareup.sqldelight.db.SqlDriver import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
actual class DatabaseDriverFactory { actual class DatabaseDriverFactory(private val path: String) {
actual fun createDriver(): SqlDriver { actual fun createDriver(): SqlDriver {
return JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY + "circolapp.db") return JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY + path)
.also { .also {
val currentVer = getVersion(it) val currentVer = getVersion(it)
if (currentVer == 0) { if (currentVer == 0) {