Skip to content

Commit

Permalink
chore: Move dockerfile so we can have multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
LichtHund committed Jun 26, 2024
1 parent a501dab commit 2febc76
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
jobs:
build-and-push-image:
runs-on: ubuntu-latest

permissions:
contents: read
packages: write
Expand Down Expand Up @@ -88,6 +89,7 @@ jobs:
uses: docker/build-push-action@v4.0.0
with:
context: .
file: backend/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2 changes: 1 addition & 1 deletion Dockerfile → backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM eclipse-temurin:21-jre-jammy

ADD backend/build/libs/backend.jar /app/backend.jar
ADD build/libs/backend.jar /app/backend.jar

WORKDIR /app
CMD ["java", "-jar", "/app/backend.jar"]
39 changes: 39 additions & 0 deletions backend/src/main/kotlin/website/Html.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dev.triumphteam.backend.website

import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.TextContent
import io.ktor.http.withCharset
import io.ktor.server.application.ApplicationCall
import io.ktor.server.response.respond
import kotlinx.html.HTML
import kotlinx.html.html
import kotlinx.html.stream.appendHTML
import kotlin.time.Duration.Companion.minutes
import kotlin.time.toJavaDuration

private val pageCache: Cache<String, TextContent> = Caffeine.newBuilder()
.expireAfterWrite(10.minutes.toJavaDuration())
.build()

public suspend fun ApplicationCall.respondHtmlCached(id: String, block: HTML.() -> Unit) {

val cached = pageCache.getIfPresent(id)
if (cached != null) {
respond(cached)
return
}

val text = buildString {
append("<!DOCTYPE html>\n")
appendHTML().html(block = block)
}

respond(
message = TextContent(text, ContentType.Text.Html.withCharset(Charsets.UTF_8), HttpStatusCode.OK).also {
pageCache.put(id, it)
}
)
}
21 changes: 4 additions & 17 deletions backend/src/main/kotlin/website/pages/docs/DocsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import dev.triumphteam.backend.website.pages.docs.components.dropDown
import dev.triumphteam.backend.website.pages.docs.components.search
import dev.triumphteam.backend.website.pages.docs.components.toast
import dev.triumphteam.backend.website.pages.setupHead
import dev.triumphteam.backend.website.respondHtmlCached
import dev.triumphteam.website.project.Navigation
import dev.triumphteam.website.project.PageSummary
import dev.triumphteam.website.project.SummaryEntry
Expand All @@ -20,6 +21,7 @@ import io.ktor.http.HttpStatusCode
import io.ktor.http.content.TextContent
import io.ktor.http.withCharset
import io.ktor.server.application.call
import io.ktor.server.html.respondHtml
import io.ktor.server.request.uri
import io.ktor.server.response.respond
import io.ktor.server.response.respondRedirect
Expand Down Expand Up @@ -92,24 +94,9 @@ public fun Routing.docsRoutes(developmentMode: Boolean) {
}
}

val cacheId = cacheId(project, currentVersion, page)
val cached = pageCache.getIfPresent(cacheId)
if (cached != null) {
return@get call.respond(cached)
call.respondHtmlCached(cacheId(project, currentVersion, page)) {
renderFullPage(developmentMode, project, currentVersion, page)
}

val text = buildString {
append("<!DOCTYPE html>\n")
appendHTML().html {
renderFullPage(developmentMode, project, currentVersion, page)
}
}

call.respond(
message = TextContent(text, ContentType.Text.Html.withCharset(Charsets.UTF_8), HttpStatusCode.OK).also {
pageCache.put(cacheId, it)
}
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions backend/src/main/kotlin/website/pages/home/HomePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import dev.triumphteam.backend.website.pages.backgroundBlob
import dev.triumphteam.backend.website.pages.createIconPath
import dev.triumphteam.backend.website.pages.home.resource.Home
import dev.triumphteam.backend.website.pages.setupHead
import dev.triumphteam.backend.website.respondHtmlCached
import io.ktor.server.application.call
import io.ktor.server.html.respondHtml
import io.ktor.server.resources.get
import io.ktor.server.routing.Routing
import kotlinx.html.FlowContent
Expand All @@ -30,7 +30,7 @@ import org.jetbrains.exposed.sql.transactions.transaction
public fun Routing.homeRoutes(developmentMode: Boolean) {

get<Home> {
call.respondHtml {
call.respondHtmlCached("home") {
val projects = transaction {
ProjectEntity.all().map { project ->
val version = DocVersionEntity.find {
Expand Down

0 comments on commit 2febc76

Please sign in to comment.