Skip to content

Commit

Permalink
refacto documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Dec 3, 2024
1 parent cbdd087 commit da62b87
Show file tree
Hide file tree
Showing 11 changed files with 464 additions and 1,251 deletions.
589 changes: 2 additions & 587 deletions cli/README.md

Large diffs are not rendered by default.

54 changes: 0 additions & 54 deletions cli/src/commands/commands.md

This file was deleted.

74 changes: 9 additions & 65 deletions daikoku/app/controllers/HomeController.scala
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
package fr.maif.otoroshi.daikoku.ctrls

import com.github.blemale.scaffeine.{Cache, Scaffeine}
import com.nimbusds.jose.util.StandardCharset
import controllers.Assets
import daikoku.BuildInfo
import fr.maif.otoroshi.daikoku.actions.{DaikokuAction, DaikokuActionMaybeWithGuest, DaikokuActionMaybeWithoutUser, DaikokuActionMaybeWithoutUserContext}
import fr.maif.otoroshi.daikoku.audit.AuditTrailEvent
import fr.maif.otoroshi.daikoku.ctrls.authorizations.async.{DaikokuAdminOrSelf, TenantAdminOnly}
import fr.maif.otoroshi.daikoku.ctrls.authorizations.sync.TeamMemberOnly
import fr.maif.otoroshi.daikoku.ctrls.authorizations.async.TenantAdminOnly
import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.domain.json.{CmsFileFormat, CmsPageFormat, CmsRequestRenderingFormat}
import fr.maif.otoroshi.daikoku.domain.json.CmsRequestRenderingFormat
import fr.maif.otoroshi.daikoku.env.Env
import fr.maif.otoroshi.daikoku.logger.AppLogger
import fr.maif.otoroshi.daikoku.utils.Errors
import org.apache.pekko.http.scaladsl.util.FastFuture
import org.joda.time.DateTime
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.json._
import play.api.mvc._

import java.io.{ByteArrayOutputStream, File, FileInputStream, FileOutputStream}
import java.util
import java.util.concurrent.TimeUnit
import java.util.zip.{ZipEntry, ZipInputStream, ZipOutputStream}
import scala.collection.mutable
import scala.concurrent.duration.DurationInt
import scala.concurrent.{ExecutionContext, Future}
import scala.util.matching.Regex

Expand All @@ -42,13 +33,6 @@ class HomeController(
implicit val e: Env = env
implicit val m: MessagesApi = messagesApi

case class CmsPageCache(contentType: String, content: String)

private val cache: Cache[String, CmsPageCache] = Scaffeine()
.expireAfterWrite(60.seconds)
.maximumSize(100)
.build[String, CmsPageCache]()

private def manageCmsHome[A](
ctx: DaikokuActionMaybeWithoutUserContext[A],
redirectTo: Result
Expand Down Expand Up @@ -234,7 +218,7 @@ class HomeController(
)) =>
redirectToLoginPage(ctx)
case Some(r) if !r.visible() => cmsPageNotFound(ctx)
case Some(page) => render(ctx, page.toCmsPage(ctx.tenant.id), Some(req), skipCache = true, req.fields)
case Some(page) => render(ctx, page.toCmsPage(ctx.tenant.id), Some(req), req.fields)
case None => cmsPageNotFound(ctx)
}
}
Expand Down Expand Up @@ -309,7 +293,7 @@ class HomeController(
case None => render(ctx, page)
case Some(layout) =>
val fields = Map("email" -> JsString(page.body))
render(ctx, layout, skipCache = true, fields = fields)
render(ctx, layout, fields = fields)
}
} else {
render(ctx, page)
Expand Down Expand Up @@ -364,55 +348,17 @@ class HomeController(
ctx: DaikokuActionMaybeWithoutUserContext[A],
r: CmsPage,
req: Option[CmsRequestRendering] = None,
skipCache: Boolean = false,
fields: Map[String, JsValue] = Map.empty[String, JsValue]
) = {
val forceReloading: Boolean =
ctx.request
.getQueryString("force_reloading")
.contains("true") || skipCache

val cacheId =
s"${ctx.user.map(_.id.value).getOrElse("")}-${r.path.getOrElse("")}"

cache
.policy()
.expireAfterWrite()
.ifPresent(eviction => {
val ttl: Long = ctx.tenant.style
.map(_.cacheTTL)
.getOrElse(60000)
.asInstanceOf[Number]
.longValue
if (eviction.getExpiresAfter(TimeUnit.MILLISECONDS) != ttl) {
cache.invalidateAll()
eviction.setExpiresAfter(ttl, TimeUnit.MILLISECONDS)
}
r.render(ctx, None, req = req, jsonToCombine = fields)
.map(res => {
Ok(res._1).as(res._2)
})

if (forceReloading) {
r.render(ctx, None, req = req, jsonToCombine = fields)
.map(res => Ok(res._1).as(res._2))
} else
cache.getIfPresent(cacheId) match {
case Some(value) =>
FastFuture.successful(Ok(value.content).as(value.contentType))
case _ =>
r.render(ctx, None, req = req, jsonToCombine = fields)
.map(res => {
cache.put(
cacheId,
CmsPageCache(content = res._1, contentType = res._2)
)
Ok(res._1).as(res._2)
})
}
}

private def cmsPageByIdWithoutAction[A](
ctx: DaikokuActionMaybeWithoutUserContext[A],
id: String,
skipCache: Boolean = false,
fields: Map[String, JsValue] = Map.empty
) = {
env.dataStore.cmsRepo.forTenant(ctx.tenant).findByIdNotDeleted(id).flatMap {
Expand All @@ -424,22 +370,20 @@ class HomeController(
s"/auth/${ctx.tenant.authProvider.name}/login?redirect=${ctx.request.path}"
)
)
case Some(page) =>
render(ctx, page, skipCache = skipCache, fields = fields)
case Some(page) => render(ctx, page, fields = fields)
}
}

def cmsPageById(id: String) =
DaikokuActionMaybeWithoutUser.async { ctx =>
cmsPageByIdWithoutAction(ctx, id, skipCache = true)
cmsPageByIdWithoutAction(ctx, id)
}

def advancedRenderCmsPageById(id: String) =
DaikokuActionMaybeWithoutUser.async(parse.json) { ctx =>
cmsPageByIdWithoutAction(
ctx,
id,
skipCache = true,
fields = ctx.request.body
.asOpt[JsObject]
.flatMap(body => (body \ "fields").asOpt[Map[String, JsValue]])
Expand Down
5 changes: 0 additions & 5 deletions daikoku/app/controllers/admin-api-openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,6 @@
"type": "string",
"nullable": true
},
"cacheTTL": {
"type": "integer",
"format": "int32",
"nullable": true
},
"logo": {
"type": "string",
"nullable": true
Expand Down
4 changes: 1 addition & 3 deletions daikoku/app/domain/json.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2083,8 +2083,7 @@ object json {
.asOpt[String]
.getOrElse("/assets/images/daikoku.svg"),
footer = (json \ "footer")
.asOpt[String],
cacheTTL = (json \ "cacheTTL").asOpt[Int].getOrElse(60000)
.asOpt[String]
)
)
} recover {
Expand Down Expand Up @@ -2121,7 +2120,6 @@ object json {
.map(JsString.apply)
.getOrElse(JsNull)
.as[JsValue],
"cacheTTL" -> o.cacheTTL,
"homePageVisible" -> o.homePageVisible,
"logo" -> o.logo,
"footer" -> o.footer
Expand Down
1 change: 0 additions & 1 deletion daikoku/app/domain/tenantEntities.scala
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ case class DaikokuStyle(
homeCmsPage: Option[String] = None,
notFoundCmsPage: Option[String] = None,
authenticatedCmsPage: Option[String] = None,
cacheTTL: Int = 60000,
logo: String = "/assets/images/daikoku.svg",
footer: Option[String] = None
) extends CanJson[DaikokuStyle] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,6 @@ export const CustomizationForm = ({ tenant, updateTenant }: { tenant?: ITenantFu
options: queryCMSPages.data?.map((t) => ({ label: `${t.path}`, value: t.id })),

},
cacheTTL: {
type: 'number',
visible: tenant?.style?.homePageVisible,
props: {
label: translate('tenant_edit.cache'),
help: translate('tenant_edit.cache_help'),
disabled: !tenant?.style?.homePageVisible,
},
},
logo: urlWithAssetButton(translate('Logo'), translate({ key: 'set.from.assets', replacements: [translate('set.logo')] }), MimeTypeFilter.image),
cssUrl: urlWithAssetButton(translate('CSS URL'), translate({ key: 'set.from.assets', replacements: [translate('set.css')] }), MimeTypeFilter.css),
css: {
Expand Down Expand Up @@ -164,7 +155,7 @@ export const CustomizationForm = ({ tenant, updateTenant }: { tenant?: ITenantFu
},
{
label: translate('Pages'),
flow: ['homePageVisible', 'homeCmsPage', 'notFoundCmsPage', 'authenticatedCmsPage', 'cacheTTL', 'footer'],
flow: ['homePageVisible', 'homeCmsPage', 'notFoundCmsPage', 'authenticatedCmsPage', 'footer'],
collapsed: true
}
]
Expand Down
1 change: 0 additions & 1 deletion daikoku/javascript/src/types/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ interface ITenantStyle {
homeCmsPage?: string;
notFoundCmsPage?: string;
authenticatedCmsPage?: string;
cacheTTL: number;
logo: string;
footer?: string;
}
Expand Down
10 changes: 0 additions & 10 deletions daikoku/public/swaggers/admin-api-openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,6 @@
"type": "string",
"nullable": true
},
"cacheTTL": {
"type": "integer",
"format": "int32",
"nullable": true
},
"cmsHistoryLength": {
"type": "integer",
"format": "int32",
"nullable": true
},
"logo": {
"type": "string",
"nullable": true
Expand Down
8 changes: 0 additions & 8 deletions daikoku/public/swaggers/admin-api-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,6 @@ components:
authenticatedCmsPage:
type: string
nullable: true
cacheTTL:
type: integer
format: int32
nullable: true
cmsHistoryLength:
type: integer
format: int32
nullable: true
logo:
type: string
nullable: true
Expand Down
Loading

0 comments on commit da62b87

Please sign in to comment.