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

Update #185

Merged
merged 6 commits into from
Oct 11, 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
13 changes: 12 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
interval: "daily"
time: "10:00"
timezone: "Europe/Paris"
assignees:
- "Ziedelth"
reviewers:
- "Ziedelth"

- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "daily"
time: "10:00"
timezone: "Europe/Paris"
assignees:
Expand Down
24 changes: 23 additions & 1 deletion .github/workflows/pull_requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,26 @@ jobs:
distribution: 'adopt'

- name: Build
run: mvn -B package -DskipTests
run: mvn -B package -DskipTests

docker:
runs-on: ubuntu-latest

needs:
- build

steps:
- uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build docker file
uses: docker/build-push-action@v5
with:
context: .
push: false
tags: registry.ziedelth.fr:5000/jais-api:dev
36 changes: 36 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ jobs:
- name: Build
run: mvn -B package -DskipTests

sonar:
if: github.ref == 'refs/heads/master'
name: Sonarqube
runs-on: ubuntu-latest

needs:
- test

permissions: read-all
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'adopt'
- name: Cache SonarQube packages
uses: actions/cache@v3
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
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=jais-api -Dsonar.projectName='jais-api'

docker:
runs-on: ubuntu-latest

Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
FROM maven:3.9.4-amazoncorretto-17 AS build
FROM maven:3-amazoncorretto-21 AS build
COPY . /app
WORKDIR /app
RUN mvn clean package -DskipTests

FROM amazoncorretto:17-alpine
FROM amazoncorretto:21-alpine
COPY --from=build /app/target/api-1.0.0-jar-with-dependencies.jar /app/api.jar
COPY --from=build /app/data/ /app/data/

RUN apk update && apk add gcompat opencv-dev
RUN apk update && apk add gcompat opencv-dev && rm -rf /var/cache/apk/*
EXPOSE 8080
WORKDIR /app
ENTRYPOINT ["java", "-jar", "api.jar"]
7 changes: 3 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
<junit-jupiter.version>5.10.0</junit-jupiter.version>

<sonar.coverage.exclusions>
**/fr/ziedelth/events/**,
**/fr/ziedelth/listeners/**,
**/fr/ziedelth/dtos/**,
**/fr/ziedelth/entities/**,
**/fr/ziedelth/plugins/**,
**/fr/ziedelth/utils/**,
**/fr/ziedelth/Application.kt
</sonar.coverage.exclusions>
</properties>
Expand Down Expand Up @@ -145,7 +144,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.2-jre</version>
<version>32.1.3-jre</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
Expand Down
43 changes: 23 additions & 20 deletions src/main/kotlin/fr/ziedelth/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fun main(args: Array<String>) {
}

println("Loading OpenCV...")
OpenCV.loadShared()
OpenCV.loadLocally()
println("OpenCV loaded")
println("Connecting to database...")
database = Database()
Expand All @@ -33,25 +33,7 @@ fun main(args: Array<String>) {
try {
PluginManager.loadPlugins()
ListenerManager()

Thread {
val scanner = Scanner(System.`in`)

while (true) {
try {
val line = scanner.nextLine()

if (line == "reload") {
PluginManager.reload()
ListenerManager()
} else if (line == "invalid-cache") {
ImageCache.invalidCache(database)
}
} catch (_: Exception) {
Thread.sleep(1000)
}
}
}.start()
handleCommands()
} catch (e: Exception) {
e.printStackTrace()
}
Expand All @@ -65,6 +47,27 @@ fun main(args: Array<String>) {
).start(wait = true)
}

private fun handleCommands() {
Thread {
val scanner = Scanner(System.`in`)

while (true) {
try {
val line = scanner.nextLine()

if (line == "reload") {
PluginManager.reload()
ListenerManager()
} else if (line == "invalid-cache") {
ImageCache.invalidCache(database)
}
} catch (_: Exception) {
Thread.sleep(1000)
}
}
}.start()
}

fun Application.myApplicationModule() {
println("Configure server...")
configureHTTP()
Expand Down
8 changes: 5 additions & 3 deletions src/main/kotlin/fr/ziedelth/controllers/AbstractController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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.*
Expand Down Expand Up @@ -46,17 +48,17 @@ open class AbstractController<T : Serializable>(open val prefix: String) {
return Pair(page, limit)
}

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

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

return false
return@async false
}
}
18 changes: 10 additions & 8 deletions src/main/kotlin/fr/ziedelth/controllers/AnimeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import io.ktor.server.response.*
import io.ktor.server.routing.*
import java.util.*

private const val ANIME_NOT_FOUND_ERROR = "Anime not found"

class AnimeController : AttachmentController<Anime>("/animes") {
@Inject
private lateinit var countryRepository: CountryRepository
Expand Down Expand Up @@ -97,7 +99,7 @@ class AnimeController : AttachmentController<Anime>("/animes") {
private fun Route.save() {
post {
println("POST $prefix")
if (isUnauthorized()) return@post
if (isUnauthorized().await()) return@post

try {
val anime = call.receive<Anime>()
Expand Down Expand Up @@ -152,14 +154,14 @@ class AnimeController : AttachmentController<Anime>("/animes") {
private fun Route.update() {
put {
println("PUT $prefix")
if (isUnauthorized()) return@put
if (isUnauthorized().await()) return@put

try {
val anime = call.receive<Anime>()
var savedAnime = animeRepository.find(anime.uuid)

if (savedAnime == null) {
call.respond(HttpStatusCode.NotFound, "Anime not found")
call.respond(HttpStatusCode.NotFound, ANIME_NOT_FOUND_ERROR)
return@put
}

Expand Down Expand Up @@ -199,11 +201,11 @@ class AnimeController : AttachmentController<Anime>("/animes") {
try {
val uuid = UUID.fromString(call.parameters["uuid"]!!)
println("DELETE $prefix/$uuid")
if (isUnauthorized()) return@delete
if (isUnauthorized().await()) return@delete
val savedAnime = animeRepository.find(uuid)

if (savedAnime == null) {
call.respond(HttpStatusCode.NotFound, "Anime not found")
call.respond(HttpStatusCode.NotFound, ANIME_NOT_FOUND_ERROR)
return@delete
}

Expand All @@ -220,7 +222,7 @@ class AnimeController : AttachmentController<Anime>("/animes") {
@APIRoute
private fun Route.merge() {
put("/merge") {
if (isUnauthorized()) return@put
if (isUnauthorized().await()) return@put

// Get list of uuids
val uuids = call.receive<List<String>>().map { UUID.fromString(it) }
Expand All @@ -229,8 +231,8 @@ class AnimeController : AttachmentController<Anime>("/animes") {
val animes = uuids.mapNotNull { animeRepository.find(it) }

if (animes.isEmpty()) {
println("Anime not found")
call.respond(HttpStatusCode.NotFound, "Anime not found")
println(ANIME_NOT_FOUND_ERROR)
call.respond(HttpStatusCode.NotFound, ANIME_NOT_FOUND_ERROR)
return@put
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/ziedelth/controllers/AyaneController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AyaneController : AbstractController<Ayane>("/ayane") {
private fun Route.save() {
post {
println("POST $prefix")
if (isUnauthorized()) return@post
if (isUnauthorized().await()) return@post

try {
val ayane = call.receive<Ayane>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CountryController : AbstractController<Country>("/countries") {
private fun Route.save() {
post {
println("POST $prefix")
if (isUnauthorized()) return@post
if (isUnauthorized().await()) return@post

try {
val country = call.receive<Country>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class EpisodeController : AttachmentController<Episode>("/episodes") {
private fun Route.saveMultiple() {
post("/multiple") {
println("POST $prefix/multiple")
if (isUnauthorized()) return@post
if (isUnauthorized().await()) return@post

try {
val episodes = call.receive<List<Episode>>().filter { !episodeRepository.exists("hash", it.hash!!) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class EpisodeTypeController : AbstractController<EpisodeType>("/episodetypes") {
private fun Route.save() {
post {
println("POST $prefix")
if (isUnauthorized()) return@post
if (isUnauthorized().await()) return@post

try {
val episodeType = call.receive<EpisodeType>()
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/ziedelth/controllers/GenreController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GenreController : AbstractController<Genre>("/genres") {
private fun Route.save() {
post {
println("POST $prefix")
if (isUnauthorized()) return@post
if (isUnauthorized().await()) return@post

try {
val genre = call.receive<Genre>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LangTypeController : AbstractController<LangType>("/langtypes") {
private fun Route.save() {
post {
println("POST $prefix")
if (isUnauthorized()) return@post
if (isUnauthorized().await()) return@post

try {
val langType = call.receive<LangType>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PlatformController : AttachmentController<Platform>("/platforms") {
private fun Route.save() {
post {
println("POST $prefix")
if (isUnauthorized()) return@post
if (isUnauthorized().await()) return@post

try {
val platform = call.receive<Platform>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ open class AbstractRepository<T> {
@Inject
protected lateinit var database: Database

private val entityClass: Class<T> =
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<T>
private val entityClass: Class<T> = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<T> // NOSONAR
private val entityName: String = entityClass.simpleName

fun find(uuid: UUID): T? {
Expand Down
9 changes: 0 additions & 9 deletions src/main/kotlin/fr/ziedelth/repositories/AnimeRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ class AnimeRepository : AbstractRepository<Anime>(),
)
}

override fun getByPageWithList(list: List<UUID>, page: Int, limit: Int): List<Anime> {
return super.getByPage(
page,
limit,
"FROM Anime WHERE uuid IN :list ORDER BY name",
"list" to list
)
}

fun getDiary(tag: String, day: Int): List<Anime> {
return database.inTransaction { session ->
val query = session.createQuery(
Expand Down
Loading