version 1.1.6
All checks were successful
continuous-integration/drone/push Build is passing

* API version 1.1.4
* added /health, returns 200 (OK, for status checks)
* moved status code to a separate class
* added status.mosad.xyz reporting
This commit is contained in:
2019-10-21 18:46:21 +02:00
parent 132cf2df0e
commit db57059727
5 changed files with 205 additions and 94 deletions

View File

@ -1,34 +1,75 @@
/**
* 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.mosad.thecitadelofricks.controller.StatusController.Companion.getTotalRequests
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
import java.io.OutputStreamWriter
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
fun postTotalRequests(totalRequests: Int) {
logger.info("sending post request ...")
private var oldTotalRequests = 0
val currentTime = System.currentTimeMillis() / 1000
val data = mapOf(Pair("value", totalRequests.toString()), Pair("timestamp", currentTime.toString()))
fun postTotalRequests() {
try {
val url = URL("$baseURL/api/v1/metrics/1/points")
val jsonInputString = "{\"value\": ${getTotalRequests() -oldTotalRequests}, \"timestamp\": \"${(System.currentTimeMillis() / 1000)}\"}"
oldTotalRequests = getTotalRequests()
val url = URL("https://status.mosad.xyz/api/v1/metrics/1/points")
val connection = url.openConnection() as HttpURLConnection
connection.setRequestProperty("X-Cachet-Token", "h0103Wix5xhhNH5tk5KU")
connection.requestMethod = "POST"
connection.doOutput = true
val con = url.openConnection() as HttpURLConnection
con.requestMethod = "POST"
con.setRequestProperty("Content-Type", "application/json; utf-8")
con.setRequestProperty("Accept", "application/json")
con.setRequestProperty("X-Cachet-Token", apiKey)
con.doOutput = true
val writer = OutputStreamWriter(connection.outputStream)
writer.write(data.toString())
writer.flush()
writer.close()
val os = con.outputStream
val input = jsonInputString.toByteArray(charset("utf-8"))
os.write(input, 0, input.size)
val br = BufferedReader(InputStreamReader(con.inputStream, "utf-8"))
val response = StringBuilder()
var responseLine: String?
while (br.readLine().also { responseLine = it } != null) {
response.append(responseLine!!.trim { it <= ' ' })
}
logger.info(response.toString())
} catch (e1: IOException) {
e1.printStackTrace()
}
}
}