Add backend module

This commit is contained in:
Matte23
2020-12-29 23:38:19 +01:00
parent 13abf510a9
commit c4e2c1950a
11 changed files with 257 additions and 0 deletions

1
backend/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

23
backend/build.gradle.kts Normal file
View File

@@ -0,0 +1,23 @@
plugins {
id("kotlin-platform-jvm")
id("application")
id("com.github.johnrengelman.shadow")
}
group = "net.underdesk"
version = "0.0.1"
dependencies {
implementation(project(":shared"))
implementation(Dependencies.Kotlin.core)
implementation(Dependencies.Kotlin.coroutinesCore)
implementation(Dependencies.Ktor.ktorCore)
implementation(Dependencies.Firebase.adminSDK)
}
application {
mainClass.set("net.underdesk.circolapp.backend.ServerKt")
mainClassName = "net.underdesk.circolapp.backend.ServerKt"
}

View File

@@ -0,0 +1,41 @@
/*
* Circolapp
* Copyright (C) 2019-2020 Matteo Schiff
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp.backend
import net.underdesk.circolapp.shared.data.AppDatabase
import net.underdesk.circolapp.shared.data.CircularDao
import net.underdesk.circolapp.shared.data.DatabaseDriverFactory
object JavaDatabase {
@Volatile
private var instance: AppDatabase? = null
private fun getInstance(): AppDatabase {
return instance ?: synchronized(this) {
instance ?: AppDatabase(
DatabaseDriverFactory().createDriver()
).also { instance = it }
}
}
fun getDaoInstance(): CircularDao {
return CircularDao(getInstance())
}
}

View File

@@ -0,0 +1,59 @@
/*
* Circolapp
* Copyright (C) 2019-2020 Matteo Schiff
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp.backend
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.Message
import com.google.firebase.messaging.Notification
import java.time.LocalDateTime
object PushNotificationUtils {
fun createPushNotification(topic: String) {
val message = Message.builder()
.putData("fetchCircular", "true")
.setTopic(topic)
.build()
val response = FirebaseMessaging.getInstance().send(message)
val current = LocalDateTime.now()
print("Sent data push notification for topic $topic with response $response at time $current \n")
}
fun createPushNotificationiOS(topic: String) {
val realTopic = topic + "IOS"
val message = Message.builder()
.setNotification(
Notification.builder()
.setTitle("Nuove circolari")
.setBody("La tua scuola ha pubblicato nuove circolari")
.build()
)
.setTopic(realTopic)
.build()
val response = FirebaseMessaging.getInstance().send(message)
val current = LocalDateTime.now()
print("Sent managed push notification for topic $realTopic with response $response at time $current \n")
}
}

View File

@@ -0,0 +1,47 @@
/*
* Circolapp
* Copyright (C) 2019-2020 Matteo Schiff
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp.backend
import com.google.auth.oauth2.GoogleCredentials
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
const val CHECK_DELAY_MILLIS = 900000L
fun main(args: Array<String>) {
print("Starting Circolapp Push microservice \n")
val options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.build()
FirebaseApp.initializeApp(options)
val serverUtils = ServerUtils()
print("Microservice started! \n")
runBlocking {
while (true) {
serverUtils.checkServers()
delay(CHECK_DELAY_MILLIS)
}
}
}

View File

@@ -0,0 +1,79 @@
/*
* Circolapp
* Copyright (C) 2019-2020 Matteo Schiff
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp.backend
import net.underdesk.circolapp.shared.data.Circular
import net.underdesk.circolapp.shared.server.KtorFactory
import net.underdesk.circolapp.shared.server.Server
import net.underdesk.circolapp.shared.server.ServerAPI
class ServerUtils {
private val serverList: MutableList<Server> = mutableListOf()
private val ktorClient = KtorFactory().createClient()
private val circularDao = JavaDatabase.getDaoInstance()
init {
for (serverId in ServerAPI.Companion.Servers.values()) {
serverList.add(ServerAPI.createServer(serverId, ktorClient))
}
}
suspend fun checkServers() {
for (server in serverList) {
checkServer(server)
}
}
private suspend fun updateCirculars(server: Server): Pair<List<Circular>, Int> {
var onlyNewCirculars = listOf<Circular>()
var errorCode = 0
val result = server.getCircularsFromServer()
if (result.second == ServerAPI.Companion.Result.ERROR)
return Pair(emptyList(), -1)
val oldCirculars = circularDao.getCirculars(server.serverID)
val newCirculars = result.first
if (newCirculars.size != oldCirculars.size) {
if (newCirculars.size < oldCirculars.size) {
circularDao.deleteAll()
errorCode = 1
}
val oldCircularsSize =
if (newCirculars.size < oldCirculars.size) 0 else oldCirculars.size
val circularCount = newCirculars.size - oldCircularsSize
onlyNewCirculars = newCirculars.subList(0, circularCount)
circularDao.insertAll(newCirculars)
}
return Pair(onlyNewCirculars, errorCode)
}
private suspend fun checkServer(server: Server) {
val newCirculars = updateCirculars(server)
if (newCirculars.second != -1 && newCirculars.first.isNotEmpty()) {
PushNotificationUtils.createPushNotification(ServerAPI.getServerTopic(server.serverID))
PushNotificationUtils.createPushNotificationiOS(ServerAPI.getServerTopic(server.serverID))
}
}
}