-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
252 additions
and
308 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package fr.shikkanime.modules | ||
|
||
import fr.shikkanime.entities.LinkObject | ||
import fr.shikkanime.entities.enums.ConfigPropertyKey | ||
import fr.shikkanime.services.caches.ConfigCacheService | ||
import fr.shikkanime.services.caches.SimulcastCacheService | ||
import fr.shikkanime.utils.Constant | ||
import fr.shikkanime.utils.StringUtils | ||
|
||
private const val ADMIN = "/admin" | ||
|
||
fun setGlobalAttributes( | ||
modelMap: MutableMap<String, Any?>, | ||
controller: Any, | ||
replacedPath: String, | ||
title: String? | ||
) { | ||
val configCacheService = Constant.injector.getInstance(ConfigCacheService::class.java) | ||
val simulcastCacheService = Constant.injector.getInstance(SimulcastCacheService::class.java) | ||
|
||
modelMap["su"] = StringUtils | ||
modelMap["links"] = getLinks(controller, replacedPath, simulcastCacheService) | ||
modelMap["footerLinks"] = getFooterLinks(controller) | ||
modelMap["title"] = getTitle(title) | ||
modelMap["seoDescription"] = configCacheService.getValueAsString(ConfigPropertyKey.SEO_DESCRIPTION) | ||
modelMap["googleSiteVerification"] = | ||
configCacheService.getValueAsString(ConfigPropertyKey.GOOGLE_SITE_VERIFICATION_ID) | ||
modelMap["currentSimulcast"] = simulcastCacheService.currentSimulcast | ||
modelMap["analyticsDomain"] = configCacheService.getValueAsString(ConfigPropertyKey.ANALYTICS_DOMAIN) | ||
modelMap["analyticsApi"] = configCacheService.getValueAsString(ConfigPropertyKey.ANALYTICS_API) | ||
modelMap["analyticsScript"] = configCacheService.getValueAsString(ConfigPropertyKey.ANALYTICS_SCRIPT) | ||
} | ||
|
||
private fun getLinks(controller: Any, replacedPath: String, simulcastCacheService: SimulcastCacheService) = | ||
LinkObject.list() | ||
.filter { it.href.startsWith(ADMIN) == controller.javaClass.simpleName.startsWith("Admin") && !it.footer } | ||
.map { link -> | ||
link.href = link.href.replace("{currentSimulcast}", simulcastCacheService.currentSimulcast?.slug ?: "") | ||
link.active = if (link.href == "/") replacedPath == link.href else replacedPath.startsWith(link.href) | ||
link | ||
} | ||
|
||
private fun getFooterLinks(controller: Any) = LinkObject.list() | ||
.filter { it.href.startsWith(ADMIN) == controller.javaClass.simpleName.startsWith("Admin") && it.footer } | ||
|
||
private fun getTitle(title: String?): String = | ||
title?.takeIf { it.contains(Constant.NAME) } ?: "$title - ${Constant.NAME}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package fr.shikkanime.modules | ||
|
||
import fr.shikkanime.utils.routes.openapi.OpenAPI | ||
import fr.shikkanime.utils.routes.param.PathParam | ||
import fr.shikkanime.utils.routes.param.QueryParam | ||
import io.github.smiley4.ktorswaggerui.dsl.BodyTypeDescriptor | ||
import io.github.smiley4.ktorswaggerui.dsl.OpenApiRoute | ||
import io.github.smiley4.ktorswaggerui.dsl.OpenApiSimpleBody | ||
import io.ktor.http.* | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.full.findAnnotation | ||
import kotlin.reflect.full.hasAnnotation | ||
import kotlin.reflect.jvm.jvmErasure | ||
|
||
fun swagger( | ||
method: KFunction<*>, | ||
routeTags: List<String>, | ||
hiddenRoute: Boolean | ||
): OpenApiRoute.() -> Unit { | ||
val openApi = method.findAnnotation<OpenAPI>() ?: return { | ||
tags = routeTags | ||
hidden = hiddenRoute | ||
} | ||
|
||
return { | ||
tags = routeTags | ||
hidden = hiddenRoute || openApi.hidden | ||
description = openApi.description | ||
swaggerRequest(method) | ||
swaggerResponse(openApi) | ||
} | ||
} | ||
|
||
private fun OpenApiRoute.swaggerRequest(method: KFunction<*>) { | ||
request { | ||
method.parameters.filter { it.hasAnnotation<QueryParam>() || it.hasAnnotation<PathParam>() } | ||
.forEach { parameter -> | ||
val name = parameter.name!! | ||
val type = parameter.type.jvmErasure | ||
|
||
when { | ||
parameter.hasAnnotation<QueryParam>() -> { | ||
val qp = parameter.findAnnotation<QueryParam>()!! | ||
queryParameter(name, type) { | ||
description = qp.description | ||
required = qp.required | ||
} | ||
} | ||
|
||
parameter.hasAnnotation<PathParam>() -> { | ||
val pp = parameter.findAnnotation<PathParam>()!! | ||
pathParameter(name, type) { | ||
description = pp.description | ||
required = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun OpenApiRoute.swaggerResponse(openApi: OpenAPI) { | ||
response { | ||
openApi.responses.forEach { response -> | ||
HttpStatusCode.fromValue(response.status) to { | ||
description = response.description | ||
val block: OpenApiSimpleBody.() -> Unit = { mediaType(ContentType.parse(response.contentType)) } | ||
|
||
when { | ||
response.type.java.isArray -> body( | ||
BodyTypeDescriptor.multipleOf(response.type.java.componentType.kotlin), | ||
block | ||
) | ||
|
||
else -> body(response.type, block) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters