added a configuration via config.xml file
continuous-integration/drone/push Build is passing Details

* the config file contains the Mensa name and URL, the Cachet Base-URL and API-Key
This commit is contained in:
Jannik 2019-10-28 18:39:44 +01:00
parent 3177be1bf0
commit dd064d63af
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
4 changed files with 154 additions and 21 deletions

View File

@ -28,6 +28,7 @@ import org.mosad.thecitadelofricks.controller.CacheController.Companion.getLesso
import org.mosad.thecitadelofricks.controller.CacheController.Companion.getLessonSubjectList
import org.mosad.thecitadelofricks.controller.CacheController.Companion.getTimetable
import org.mosad.thecitadelofricks.controller.CacheController.Companion.mensaMenu
import org.mosad.thecitadelofricks.controller.StartupController
import org.mosad.thecitadelofricks.controller.StatusController.Companion.getStatus
import org.mosad.thecitadelofricks.controller.StatusController.Companion.updateMensaMenuRequests
import org.mosad.thecitadelofricks.controller.StatusController.Companion.updateTimetableRequests
@ -52,7 +53,8 @@ class APIController {
}
init {
CacheController() // initialize the CacheController
StartupController()
CacheController()
}
// TODO remove this with API version 1.2.0

View File

@ -42,11 +42,6 @@ class CacheController {
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
// hso parser links (hardcoded)
private val courseListURL = "https://www.hs-offenburg.de/studium/vorlesungsplaene/"
private val mensaMenuURL = "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/"
private val mensaName = "Offenburg"
init {
initUpdates()
scheduledUpdates()
@ -154,7 +149,7 @@ class CacheController {
* during the update process the old data will be returned for a API request
*/
private fun asyncUpdateCourseList() = GlobalScope.launch {
CourseListParser().getCourseLinks(courseListURL)?.let {
CourseListParser().getCourseLinks(StartupController.getCourseListURL())?.let {
courseList = CourseList(
CourseMeta(System.currentTimeMillis() / 1000, it.size), it
)
@ -168,13 +163,13 @@ class CacheController {
* during the update process the old data will be returned for a API request
*/
private fun asyncUpdateMensa() = GlobalScope.launch {
val mensaCurrentWeek = MensaParser().getMensaMenu(mensaMenuURL)
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(mensaMenuURL))
val mensaCurrentWeek = MensaParser().getMensaMenu(StartupController.getMensaMenuURL())
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(StartupController.getMensaMenuURL()))
// only update if we get valid data
if (mensaCurrentWeek != null && mensaNextWeek != null) {
mensaMenu = MensaMenu(
MensaMeta(System.currentTimeMillis() / 1000, mensaName), mensaCurrentWeek, mensaNextWeek
MensaMeta(System.currentTimeMillis() / 1000, StartupController.getMensaName()), mensaCurrentWeek, mensaNextWeek
)
}
@ -214,7 +209,7 @@ class CacheController {
private fun initUpdates() = runBlocking {
// get all courses on startup
val jobCourseUpdate = GlobalScope.async {
CourseListParser().getCourseLinks(courseListURL)?.let {
CourseListParser().getCourseLinks(StartupController.getCourseListURL())?.let {
courseList = CourseList(
CourseMeta(System.currentTimeMillis() / 1000, it.size), it
)
@ -223,13 +218,13 @@ class CacheController {
// get the current and next weeks mensa menus
val jobMensa = GlobalScope.async{
val mensaCurrentWeek = MensaParser().getMensaMenu(mensaMenuURL)
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(mensaMenuURL))
val mensaCurrentWeek = MensaParser().getMensaMenu(StartupController.getMensaMenuURL())
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(StartupController.getMensaMenuURL()))
// only update if we get valid data
if (mensaCurrentWeek != null && mensaNextWeek != null) {
mensaMenu = MensaMenu(
MensaMeta(System.currentTimeMillis() / 1000, mensaName), mensaCurrentWeek, mensaNextWeek
MensaMeta(System.currentTimeMillis() / 1000, StartupController.getMensaName()), mensaCurrentWeek, mensaNextWeek
)
}
}
@ -265,9 +260,11 @@ class CacheController {
asyncUpdateMensa()
}
// post to status.mosad.xyz every hour
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
CachetAPIController.postTotalRequests()
// post to status.mosad.xyz every hour, if an API key is present
if (StartupController.getCachetAPIKey() != "0") {
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
CachetAPIController.postTotalRequests()
}
}
}

View File

@ -36,14 +36,12 @@ class CachetAPIController {
companion object {
private val logger: Logger = LoggerFactory.getLogger(CachetAPIController::class.java)
private const val baseURL = "https://status.mosad.xyz"
private const val apiKey = "" //TODO
private var oldTotalRequests = 0
fun postTotalRequests() {
try {
val url = URL("$baseURL/api/v1/metrics/1/points")
val url = URL("${StartupController.getCachetBaseURL()}/api/v1/metrics/1/points")
val jsonInputString = "{\"value\": ${getTotalRequests() -oldTotalRequests}, \"timestamp\": \"${(System.currentTimeMillis() / 1000)}\"}"
oldTotalRequests = getTotalRequests()
@ -51,7 +49,7 @@ class CachetAPIController {
con.requestMethod = "POST"
con.setRequestProperty("Content-Type", "application/json; utf-8")
con.setRequestProperty("Accept", "application/json")
con.setRequestProperty("X-Cachet-Token", apiKey)
con.setRequestProperty("X-Cachet-Token", StartupController.getCachetAPIKey())
con.doOutput = true
val os = con.outputStream

View File

@ -0,0 +1,136 @@
/**
* TheCitadelofRicks
*
* Copyright 2019 <seil0@mosad.xyz>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
package org.mosad.thecitadelofricks.controller
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.*
class StartupController {
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
private val userHome = System.getProperty("user.home")
private val tcorHome = "$userHome/.tcor"
private val dirTcorHome = File(tcorHome)
private val dirTcorCache = File("$tcorHome/cache")
private val fileConfig = File("$tcorHome/config.xml")
init {
// if the tcor directory doesn't exist, create it
if (!dirTcorHome.exists()) {
dirTcorHome.mkdir()
}
// if the cache directory doesn't exist, create it
if (!dirTcorCache.exists()) {
dirTcorCache.mkdir()
}
// check if the config file exist, if so load it
if (fileConfig.exists()) {
loadConfig()
} else {
createConfig()
}
}
private fun loadConfig() = try {
val properties = Properties()
properties.loadFromXML(FileInputStream(fileConfig))
cachetAPIKey = try {
properties.getProperty("cachetAPIKey")
} catch (ex: Exception) {
"0"
}
cachetBaseURL = try {
properties.getProperty("cachetBaseURL")
} catch (ex: Exception) {
"https://status.mosad.xyz"
}
mensaMenuURL = try {
properties.getProperty("mensaMenuURL")
} catch (ex: Exception) {
"https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/"
}
mensaName = try {
properties.getProperty("mensaName")
} catch (ex: Exception) {
"Offenburg"
}
} catch (ex: Exception) {
logger.error("error while loading config", ex)
}
private fun createConfig() = try {
val properties = Properties()
properties.setProperty("cachetAPIKey", "0")
properties.setProperty("cachetBaseURL", "https://status.mosad.xyz")
properties.setProperty("mensaMenuURL", "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/")
properties.setProperty("mensaName", "Offenburg")
val outputStream = FileOutputStream(fileConfig)
properties.storeToXML(outputStream, "tcor configuration")
outputStream.close()
} catch (ex: Exception) {
logger.error("error while creating config", ex)
}
companion object {
private var cachetAPIKey = "0"
private var cachetBaseURL = "https://status.mosad.xyz"
private var courseListURL = "https://www.hs-offenburg.de/studium/vorlesungsplaene/"
private var mensaMenuURL = "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/"
private var mensaName = "Offenburg"
fun getCachetAPIKey(): String {
return cachetAPIKey
}
fun getCachetBaseURL(): String {
return cachetBaseURL
}
fun getCourseListURL(): String {
return courseListURL
}
fun getMensaMenuURL(): String {
return mensaMenuURL
}
fun getMensaName(): String {
return mensaName
}
}
}