Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade pdfgen-core dependency #153

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ The template and data directory structure both follow the `<application>/<templa
To enable HTML document support, use the environment variable `ENABLE_HTML_ENDPOINT=true`. This will enable the
HTML endpoints on `/api/v1/genhtml/<application>/<template>`.

By default pdfgen will load all assets (`templates`, `resources`, `data`) to memory on startup. Any change on files inside these folders will not be loaded before a restart of the application. However if you are developing templates you can make the application to reload the assets on every request by setting `DEV_MODE=true`.

### Upgrading the gradle wrapper
Find the newest version of gradle here: https://gradle.org/releases/ Then run this command:

Expand Down
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ val junitJupiterVersion = "5.10.1"
val verapdfVersion = "1.24.1"
val ktfmtVersion = "0.44"
val testcontainersVersion= "1.19.3"
val pdfgencoreVersion = "1.0.3"
val pdfgencoreVersion = "1.1.0"


plugins {
Expand Down Expand Up @@ -68,6 +68,7 @@ tasks {

repositories {
mavenCentral()
mavenLocal()
maven {
url = uri("https://github-package-registry-mirror.gc.nav.no/cached/maven-release")
}
Expand Down
23 changes: 16 additions & 7 deletions src/main/kotlin/no/nav/pdfgen/Bootstrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.ktor.http.content.TextContent
import io.ktor.http.withCharset
import io.ktor.serialization.jackson.jackson
import io.ktor.server.application.call
import io.ktor.server.application.createApplicationPlugin
import io.ktor.server.application.install
import io.ktor.server.engine.ApplicationEngine
import io.ktor.server.engine.embeddedServer
Expand All @@ -30,26 +31,29 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import no.nav.pdfgen.api.setupGeneratePdfApi
import no.nav.pdfgen.core.PDFgen
import no.nav.pdfgen.core.template.loadTemplates
import no.nav.pdfgen.core.Environment
import no.nav.pdfgen.core.PDFGenCore

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.verapdf.gf.foundry.VeraGreenfieldFoundryProvider

val log: Logger = LoggerFactory.getLogger("pdfgen")


fun main() {
initializeApplication(8080).start(wait = true)
initializeApplication(no.nav.pdfgen.Environment().port).start(wait = true)
}

fun initializeApplication(port: Int): ApplicationEngine {
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider")
VeraGreenfieldFoundryProvider.initialise()

PDFgen.init(no.nav.pdfgen.core.Environment())
val environment = no.nav.pdfgen.Environment()
val coreEnvironment = Environment()
PDFGenCore.init(coreEnvironment)

val env = no.nav.pdfgen.core.Environment()
val templates = loadTemplates()
val templates = coreEnvironment.templates
val collectorRegistry: CollectorRegistry = CollectorRegistry.defaultRegistry

XRLog.setLoggerImpl(Slf4jLogger())
Expand All @@ -75,6 +79,11 @@ fun initializeApplication(port: Int): ApplicationEngine {
)
}
}
install(createApplicationPlugin(name = "ReloadPDFGenCorePlugin") {
onCallReceive { call ->
if (environment.isDevMode) PDFGenCore.reloadEnvironment()
}
})
routing {
get("/internal/is_ready") { call.respondText("I'm ready") }
get("/internal/is_alive") { call.respondText("I'm alive") }
Expand All @@ -91,7 +100,7 @@ fun initializeApplication(port: Int): ApplicationEngine {
}
}
}
setupGeneratePdfApi(env)
setupGeneratePdfApi(environment)
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/no/nav/pdfgen/Environment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package no.nav.pdfgen

data class Environment(
val port: Int = System.getenv("SERVER_PORT")?.toInt() ?: 8080,
val isDevMode: Boolean = System.getenv("DEV_MODE")?.let { it == "true" } ?: false,
val disablePdfGet: Boolean = System.getenv("DISABLE_PDF_GET")?.let { it == "true" } ?: false,
val enableHtmlEndpoint: Boolean =
System.getenv("ENABLE_HTML_ENDPOINT")?.let { it == "true" } ?: false,
)
4 changes: 2 additions & 2 deletions src/main/kotlin/no/nav/pdfgen/api/GeneratePdfApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import io.ktor.server.routing.post
import io.ktor.server.routing.route
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import no.nav.pdfgen.core.Environment
import no.nav.pdfgen.Environment
import no.nav.pdfgen.core.OPENHTMLTOPDF_RENDERING_SUMMARY
import no.nav.pdfgen.core.pdf.createHtml
import no.nav.pdfgen.core.pdf.createHtmlFromTemplateData
Expand All @@ -25,7 +25,7 @@ import no.nav.pdfgen.log
import java.io.ByteArrayOutputStream
import java.io.InputStream

fun Routing.setupGeneratePdfApi(env: Environment) {
fun Routing.setupGeneratePdfApi(env: Environment = Environment()) {
route("/api/v1/genpdf") {
if (!env.disablePdfGet) {
get("/{applicationName}/{template}") {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</appender>

<logger name="pdfgen" level="INFO"/>
<!-- <logger name="no.nav.pdfgen.core" level="DEBUG"/>-->

<root level="INFO">
<appender-ref ref="CONSOLE"/>
Expand Down
3 changes: 2 additions & 1 deletion src/test/kotlin/no/nav/pdfgen/PdfGenITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import java.nio.file.Files
import java.nio.file.Paths
import java.util.concurrent.Executors
import kotlinx.coroutines.*
import no.nav.pdfgen.core.Environment
import no.nav.pdfgen.core.template.loadTemplates
import org.apache.pdfbox.io.IOUtils
import org.apache.pdfbox.pdmodel.PDDocument
Expand All @@ -28,7 +29,7 @@ internal class PdfGenITest {
private val applicationPort = getRandomPort()
private val application = initializeApplication(applicationPort)
private val client = HttpClient(CIO) { expectSuccess = false }
private val templates = loadTemplates()
private val templates = Environment().templates
private val timeoutSeconds: Long = 10

@AfterEach
Expand Down
3 changes: 2 additions & 1 deletion src/test/kotlin/no/nav/pdfgen/RenderingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.nav.pdfgen

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import no.nav.pdfgen.core.Environment
import java.io.ByteArrayInputStream
import no.nav.pdfgen.core.pdf.createPDFA
import no.nav.pdfgen.core.pdf.render
Expand All @@ -15,7 +16,7 @@ import org.verapdf.pdfa.flavours.PDFAFlavour
import org.verapdf.pdfa.results.TestAssertion

internal class RenderingTest {
private val templates = loadTemplates()
private val templates = Environment().templates
private val objectMapper = ObjectMapper()

@BeforeEach
Expand Down
Loading