mirror of
https://github.com/Matte23/circolapp.git
synced 2025-12-05 23:19:10 +00:00
Add backend module
This commit is contained in:
1
backend/.gitignore
vendored
Normal file
1
backend/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
23
backend/build.gradle.kts
Normal file
23
backend/build.gradle.kts
Normal 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"
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ buildscript {
|
||||
classpath(Config.Plugin.ktlint)
|
||||
classpath(Config.Plugin.aboutLibraries)
|
||||
classpath(Config.Plugin.dependencies)
|
||||
classpath(Config.Plugin.shadow)
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ object Config {
|
||||
"com.mikepenz.aboutlibraries.plugin:aboutlibraries-plugin:${Dependencies.AboutLibraries.version}"
|
||||
const val dependencies =
|
||||
"com.github.ben-manes:gradle-versions-plugin:0.36.0"
|
||||
const val shadow =
|
||||
"com.github.jengelman.gradle.plugins:shadow:5.2.0"
|
||||
}
|
||||
|
||||
object Android {
|
||||
|
||||
@@ -30,6 +30,8 @@ object Dependencies {
|
||||
object Firebase {
|
||||
const val bom = "com.google.firebase:firebase-bom:26.1.1"
|
||||
const val messaging = "com.google.firebase:firebase-messaging-ktx"
|
||||
|
||||
const val adminSDK = "com.google.firebase:firebase-admin:7.0.1"
|
||||
}
|
||||
|
||||
object Ktor {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
include(":backend")
|
||||
include(":shared")
|
||||
include(":app")
|
||||
rootProject.name = "Circolapp"
|
||||
|
||||
1
shared/src/jvmMain/kotlin/net/underdesk/circolapp/shared/server
Symbolic link
1
shared/src/jvmMain/kotlin/net/underdesk/circolapp/shared/server
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../../../../androidMain/kotlin/net/underdesk/circolapp/shared/server/
|
||||
Reference in New Issue
Block a user