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

Commit

Permalink
Merge pull request #46 from Z-Jais/master
Browse files Browse the repository at this point in the history
Quality and tests
  • Loading branch information
Ziedelth authored Nov 22, 2022
2 parents 063a360 + 465a6ba commit e81e80f
Show file tree
Hide file tree
Showing 72 changed files with 3,692 additions and 1,369 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Build
on:
push:
branches-ignore:
- stable

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: Cache SonarQube packages
uses: actions/cache@v1
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=API
54 changes: 52 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
<kotlin.code.style>official</kotlin.code.style>
<kotlin_version>1.7.20</kotlin_version>
<kotlin.compiler.jvmTarget>11</kotlin.compiler.jvmTarget>
<logback_version>1.4.4</logback_version>
<logback_version>1.4.5</logback_version>
<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.9.1</junit-jupiter.version>

<sonar.coverage.exclusions>
**/fr/ziedelth/events/**,
**/fr/ziedelth/listeners/**,
**/fr/ziedelth/plugins/**,
**/fr/ziedelth/utils/**,
**/fr/ziedelth/Application.kt
</sonar.coverage.exclusions>
</properties>

<repositories>
Expand Down Expand Up @@ -47,6 +55,17 @@
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-test-host-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-core-jvm</artifactId>
Expand All @@ -67,6 +86,12 @@
<artifactId>ktor-server-content-negotiation-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-client-content-negotiation-jvm</artifactId>
<version>${ktor_version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-serialization-gson-jvm</artifactId>
Expand Down Expand Up @@ -199,9 +224,34 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${kotlin.compiler.jvmTarget}</source>
<target>${kotlin.compiler.jvmTarget}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.9.1.2184</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<phase>test-compile</phase>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
118 changes: 118 additions & 0 deletions src/main/kotlin/fr/ziedelth/controllers/AbstractController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package fr.ziedelth.controllers

import com.google.gson.Gson
import fr.ziedelth.repositories.IPageRepository
import fr.ziedelth.utils.Decoder
import fr.ziedelth.utils.ImageCache
import fr.ziedelth.utils.RequestCache
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.util.pipeline.*
import java.io.Serializable
import java.lang.reflect.ParameterizedType
import java.util.*

const val UNKNOWN_MESSAGE_ERROR = "Unknown error"
const val MISSING_PARAMETERS_MESSAGE_ERROR = "Missing parameters"

open class IController<T : Serializable>(val prefix: String) {
val entityName: String = ((javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<*>).simpleName
val uuidRequest: UUID = UUID.randomUUID()

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")

if (page < 1 || limit < 1) {
throw IllegalArgumentException("Page or limit is not valid")
}

if (limit > 30) {
throw IllegalArgumentException("Limit is too high")
}

return Pair(page, limit)
}

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

protected fun Route.getWithPage(iPageRepository: IPageRepository<T>) {
get("/country/{country}/page/{page}/limit/{limit}") {
try {
val country = call.parameters["country"]!!
val (page, limit) = getPageAndLimit()
println("GET $prefix/country/$country/page/$page/limit/$limit")
val request = RequestCache.get(uuidRequest, country, page, limit)

if (request == null || request.isExpired()) {
val list = iPageRepository.getByPage(country, page, limit)
request?.update(list) ?: RequestCache.put(uuidRequest, country, page, limit, value = list)
}

call.respond(RequestCache.get(uuidRequest, country, page, limit)!!.value!!)
} catch (e: Exception) {
printError(call, e)
}
}
}

protected fun Route.getAnimeWithPage(iPageRepository: IPageRepository<T>) {
get("/anime/{uuid}/page/{page}/limit/{limit}") {
try {
val animeUuid = call.parameters["uuid"]!!
val (page, limit) = getPageAndLimit()
println("GET $prefix/anime/$animeUuid/page/$page/limit/$limit")
call.respond(iPageRepository.getByPageWithAnime(UUID.fromString(animeUuid), page, limit))
} catch (e: Exception) {
printError(call, e)
}
}
}

protected fun Route.getWatchlistWithPage(iPageRepository: IPageRepository<T>) {
post("/watchlist/page/{page}/limit/{limit}") {
try {
val watchlist = call.receive<String>()
val (page, limit) = getPageAndLimit()
println("POST $prefix/watchlist/page/$page/limit/$limit")
val dataFromGzip =
Gson().fromJson(Decoder.fromGzip(watchlist), Array<String>::class.java).map { UUID.fromString(it) }
call.respond(iPageRepository.getByPageWithList(dataFromGzip, page, limit))
} catch (e: Exception) {
printError(call, e)
}
}
}

fun Route.getAttachment() {
get("/attachment/{uuid}") {
val string = call.parameters["uuid"]!!
val uuidRegex =
"^[0-9(a-f|A-F)]{8}-[0-9(a-f|A-F)]{4}-4[0-9(a-f|A-F)]{3}-[89ab][0-9(a-f|A-F)]{3}-[0-9(a-f|A-F)]{12}\$".toRegex()

if (!uuidRegex.matches(string)) {
println("GET $prefix/attachment/$string : Invalid UUID")
return@get call.respond(HttpStatusCode.BadRequest)
}

val uuid = UUID.fromString(string)
println("GET ${prefix}/attachment/$uuid")

if (!ImageCache.contains(uuid)) {
println("Attachment $uuid not found")
call.respond(HttpStatusCode.NoContent)
return@get
}

val image = ImageCache.get(uuid)!!
println("Attachment $uuid found (${image.bytes.size} bytes)")
call.respondBytes(image.bytes, ContentType("image", "webp"))
}
}
}
Loading

0 comments on commit e81e80f

Please sign in to comment.