Skip to content

Commit

Permalink
Extend ConfigPropertyKey enum and Config entity to support analytics …
Browse files Browse the repository at this point in the history
…properties, update database schema and data accordingly
  • Loading branch information
Ziedelth committed Mar 12, 2024
1 parent 37b5766 commit 6f4eedc
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/shikkanime/entities/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class Config(
override val uuid: UUID? = null,
@Column(nullable = false, name = "property_key", unique = true)
val propertyKey: String? = null,
@Column(nullable = false, name = "property_value")
@Column(nullable = false, name = "property_value", length = 5000)
var propertyValue: String? = null,
) : ShikkEntity(uuid)
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ enum class ConfigPropertyKey(val key: String) {
BSKY_SESSION_TIMEOUT("bsky_session_timeout"),
THREADS_SESSION_TIMEOUT("threads_session_timeout"),
SIMULCAST_RANGE_DELAY("simulcast_range_delay"),
ANALYTICS_DOMAIN("analytics_domain"),
ANALYTICS_API("analytics_api"),
ANALYTICS_SCRIPT("analytics_script"),
}
10 changes: 9 additions & 1 deletion src/main/kotlin/fr/shikkanime/modules/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ private val logger = LoggerFactory.getLogger("Routing")
private val callStartTime = AttributeKey<ZonedDateTime>("CallStartTime")

fun Application.configureRouting() {
val configCacheService = Constant.injector.getInstance(ConfigCacheService::class.java)

environment.monitor.subscribe(Routing.RoutingCallStarted) { call ->
call.attributes.put(callStartTime, ZonedDateTime.now())

Expand All @@ -61,7 +63,9 @@ fun Application.configureRouting() {
"img-src data: 'self' 'unsafe-inline' 'unsafe-eval' https://api.shikkanime.fr https://www.shikkanime.fr; " +
"style-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net; " +
"font-src 'self' https://cdn.jsdelivr.net; " +
"script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net"
"script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net; " +
configCacheService.getValueAsString(ConfigPropertyKey.ANALYTICS_API)
?.let { "connect-src 'self' $it; " }
)

context.response.header("X-Frame-Options", "DENY")
Expand Down Expand Up @@ -316,6 +320,10 @@ private suspend fun handleTemplateResponse(
?.let { modelMap["googleSiteVerification"] = it }
simulcastCacheService.currentSimulcast?.let { modelMap["currentSimulcast"] = it }

configCacheService.getValueAsString(ConfigPropertyKey.ANALYTICS_DOMAIN)?.let { modelMap["analyticsDomain"] = it }
configCacheService.getValueAsString(ConfigPropertyKey.ANALYTICS_API)?.let { modelMap["analyticsApi"] = it }
configCacheService.getValueAsString(ConfigPropertyKey.ANALYTICS_SCRIPT)?.let { modelMap["analyticsScript"] = it }

call.respond(response.status, FreeMarkerContent(map["template"] as String, modelMap, "", response.contentType))
}

Expand Down
62 changes: 62 additions & 0 deletions src/main/resources/db/changelog/2024/03/04-changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.26.xsd"
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">
<property global="false" name="id" value="1710284914290"/>
<property global="false" name="author" value="Ziedelth"/>

<changeSet id="${id}-1" author="${author}" dbms="postgresql">
<preConditions onFail="MARK_RAN">
<columnExists columnName="property_value" tableName="config"/>
</preConditions>

<modifyDataType columnName="property_value"
newDataType="VARCHAR(5000)"
tableName="config"/>
</changeSet>

<changeSet id="${id}-2" author="${author}" dbms="postgresql">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">SELECT COUNT(*)
FROM config
WHERE property_key = 'analytics_domain'</sqlCheck>
</preConditions>

<insert tableName="config">
<column name="uuid" valueComputed="gen_random_uuid()"/>
<column name="property_key" value="analytics_domain"/>
<column name="property_value" value="example.com"/>
</insert>
</changeSet>

<changeSet id="${id}-3" author="${author}" dbms="postgresql">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">SELECT COUNT(*)
FROM config
WHERE property_key = 'analytics_api'</sqlCheck>
</preConditions>

<insert tableName="config">
<column name="uuid" valueComputed="gen_random_uuid()"/>
<column name="property_key" value="analytics_api"/>
<column name="property_value" value="http://localhost:8080/api/event"/>
</insert>
</changeSet>

<changeSet id="${id}-4" author="${author}" dbms="postgresql">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">SELECT COUNT(*)
FROM config
WHERE property_key = 'analytics_script'</sqlCheck>
</preConditions>

<insert tableName="config">
<column name="uuid" valueComputed="gen_random_uuid()"/>
<column name="property_key" value="analytics_script"/>
<column name="property_value" value=""/>
</insert>
</changeSet>
</databaseChangeLog>
1 change: 1 addition & 0 deletions src/main/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@
<include file="/db/changelog/2024/03/01-changelog.xml"/>
<include file="/db/changelog/2024/03/02-changelog.xml"/>
<include file="/db/changelog/2024/03/03-changelog.xml"/>
<include file="/db/changelog/2024/03/04-changelog.xml"/>
</databaseChangeLog>
4 changes: 4 additions & 0 deletions src/main/resources/templates/_freemarker_implicit.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@
[#-- @ftlvariable name="footerLinks" type="kotlin.collections.AbstractList<fr.shikkanime.entities.LinkObject>" --]
[#-- @ftlvariable name="seoLinks" type="kotlin.collections.AbstractList<fr.shikkanime.entities.enums.Link>" --]
[#-- @ftlvariable name="query" type="java.lang.String" --]

[#-- @ftlvariable name="analyticsDomain" type="java.lang.String" --]
[#-- @ftlvariable name="analyticsApi" type="java.lang.String" --]
[#-- @ftlvariable name="analyticsScript" type="java.lang.String" --]
6 changes: 6 additions & 0 deletions src/main/resources/templates/site/_layout.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
</#if>

<script defer src="/assets/js/alpinejs.min.js" crossorigin="anonymous"></script>

<#if (analyticsDomain?? && analyticsDomain?length != 0) && (analyticsApi?? && analyticsApi?length != 0) && (analyticsScript?? && analyticsScript?length != 0)>
<script data-domain="${analyticsDomain}" data-api="${analyticsApi}">
${analyticsScript}
</script>
</#if>
</head>
<body>
<#nested 0>
Expand Down

0 comments on commit 6f4eedc

Please sign in to comment.