Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Change route system and update dependencies #201

Merged
merged 5 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<description>api</description>

<properties>
<ktor_version>2.3.5</ktor_version>
<ktor_version>2.3.6</ktor_version>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.version>1.9.20</kotlin.version>
<kotlin.compiler.languageVersion>2.0</kotlin.compiler.languageVersion>
Expand All @@ -18,7 +18,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<main.class>fr.ziedelth.ApplicationKt</main.class>
<junit-jupiter.version>5.10.0</junit-jupiter.version>
<junit-jupiter.version>5.10.1</junit-jupiter.version>

<sonar.coverage.exclusions>
**/fr/ziedelth/dtos/**,
Expand Down Expand Up @@ -190,11 +190,11 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.1</version>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.1</version>
<version>3.2.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
42 changes: 2 additions & 40 deletions src/main/kotlin/fr/ziedelth/controllers/AbstractController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@ package fr.ziedelth.controllers
import fr.ziedelth.utils.Constant
import fr.ziedelth.utils.Decoder
import fr.ziedelth.utils.Logger
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.util.pipeline.*
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import java.io.Serializable
import java.lang.reflect.ParameterizedType
import java.util.*
import java.util.logging.Level

const val UNKNOWN_MESSAGE_ERROR = "Unknown error"
const val MISSING_PARAMETERS_MESSAGE_ERROR = "Missing parameters"
Expand All @@ -25,42 +18,11 @@ open class AbstractController<T : Serializable>(open val prefix: String) {
val langTypes: List<UUID> = listOf(), // Lang types wanted to see
)

val entityName: String =
((javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<*>).simpleName
val uuidRequest: UUID = UUID.randomUUID()
val entityName: String = ((javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<*>).simpleName

fun decode(watchlist: String): FilterData {
val filterData = Constant.gson.fromJson(Decoder.fromGzip(watchlist), FilterData::class.java)
Logger.config("$watchlist - Episodes: ${filterData.episodes.size} - Animes: ${filterData.animes.size}")
Logger.config("Episodes: ${filterData.episodes.size} - Animes: ${filterData.animes.size}")
return filterData
}

suspend fun printError(call: ApplicationCall, e: Exception) {
Logger.log(Level.SEVERE, e.message, e)
call.respond(HttpStatusCode.InternalServerError, e.message ?: UNKNOWN_MESSAGE_ERROR)
}

protected fun PipelineContext<Unit, ApplicationCall>.getPageAndLimit(): Pair<Int, Int> {
val page = call.parameters["page"]!!.toIntOrNull() ?: throw IllegalArgumentException("Page is not valid")
val limit = call.parameters["limit"]!!.toIntOrNull() ?: throw IllegalArgumentException("Limit is not valid")

require(!(page < 1 || limit < 1)) { "Page or limit is not valid" }
require(limit <= 30) { "Limit is too high" }

return Pair(page, limit)
}

protected fun PipelineContext<Unit, ApplicationCall>.isUnauthorized(): Deferred<Boolean> = async {
if (!Constant.secureKey.isNullOrBlank()) {
val authorization = call.request.headers[HttpHeaders.Authorization]

if (Constant.secureKey != authorization) {
Logger.warning("Unauthorized request")
call.respond(HttpStatusCode.Unauthorized, "Secure key not equals")
return@async true
}
}

return@async false
}
}
Loading