From b5e45d882f91149616eec672ef4d0eb88e5da0ee Mon Sep 17 00:00:00 2001 From: Mathieu Ancelin Date: Mon, 28 Aug 2023 14:52:24 +0200 Subject: [PATCH] resources refactoring --- otoroshi/app/api/api.scala | 28 +++++++++++++++------------- otoroshi/app/env/Env.scala | 2 ++ otoroshi/app/storage/storage.scala | 2 +- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/otoroshi/app/api/api.scala b/otoroshi/app/api/api.scala index c8b417a9e1..cf59e4a377 100644 --- a/otoroshi/app/api/api.scala +++ b/otoroshi/app/api/api.scala @@ -294,18 +294,8 @@ case class GenericResourceAccessApiWithState[T <: EntityLocationSupport]( override def update(values: Seq[T]): Unit = stateUpdate(values) } -class GenericApiController(ApiAction: ApiAction, cc: ControllerComponents)(implicit env: Env) - extends AbstractController(cc) { - - private val sourceBodyParser = BodyParser("GenericApiController BodyParser") { _ => - Accumulator.source[ByteString].map(Right.apply)(env.otoroshiExecutionContext) - } - - private implicit val ec = env.otoroshiExecutionContext - - private implicit val mat = env.otoroshiMaterializer - - private lazy val resources = Seq( +class OtoroshiResources(env: Env) { + lazy val resources = Seq( /////// Resource( "Route", @@ -578,6 +568,18 @@ class GenericApiController(ApiAction: ApiAction, cc: ControllerComponents)(impli ) ) ) ++ env.adminExtensions.resources() +} + +class GenericApiController(ApiAction: ApiAction, cc: ControllerComponents)(implicit env: Env) + extends AbstractController(cc) { + + private val sourceBodyParser = BodyParser("GenericApiController BodyParser") { _ => + Accumulator.source[ByteString].map(Right.apply)(env.otoroshiExecutionContext) + } + + private implicit val ec = env.otoroshiExecutionContext + + private implicit val mat = env.otoroshiMaterializer private def filterPrefix: Option[String] = "filter.".some @@ -854,7 +856,7 @@ class GenericApiController(ApiAction: ApiAction, cc: ControllerComponents)(impli request: RequestHeader, bulk: Boolean = false )(f: Resource => Future[Result]): Future[Result] = { - resources + env.allResources.resources .filter(_.version.served) .find(r => (group == "any" || r.group == group) && (version == "any" || r.version.name == version) && r.pluralName == entity diff --git a/otoroshi/app/env/Env.scala b/otoroshi/app/env/Env.scala index 568abd8b70..fc7391cc7d 100644 --- a/otoroshi/app/env/Env.scala +++ b/otoroshi/app/env/Env.scala @@ -973,6 +973,8 @@ class Env( val servers = TcpService.runServers(this) + lazy val allResources = new otoroshi.api.OtoroshiResources(this) + lazy val adminExtensionsConfig = AdminExtensionConfig( enabled = configuration.getOptionalWithFileSupport[Boolean]("otoroshi.admin-extensions.enabled").getOrElse(true) ) diff --git a/otoroshi/app/storage/storage.scala b/otoroshi/app/storage/storage.scala index 32781bca1f..8e2be050e7 100644 --- a/otoroshi/app/storage/storage.scala +++ b/otoroshi/app/storage/storage.scala @@ -259,7 +259,7 @@ trait OptimizedRedisLike { case _ if key.startsWith(ds.backendsDataStore.key("")) => "backend".some case _ if key.startsWith(ds.wasmPluginsDataStore.key("")) => "wasm-plugin".some case _ => { - env.adminExtensions.resources() + env.allResources.resources .map { res => (key.startsWith(res.access.key("")), res.singularName) }