Skip to content

Commit

Permalink
Lager et søtt Spanner image for lokal kjøring
Browse files Browse the repository at this point in the history
Co-authored-by: Hege Haavaldsen <[email protected]>
  • Loading branch information
fraadsbrandth and hegehaav committed Aug 26, 2024
1 parent 512ab45 commit 3198739
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 1 deletion.
35 changes: 35 additions & 0 deletions .github/workflows/lokal-spanner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Bygge lokal-spanner image

on:
push:
branches:
- master

jobs:
build:
name: build docker image
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21.x'
cache: 'gradle'
- name: test and build
run: ./gradlew build -settings-file lokal-spanner.settings.gradle.kts
env:
ORG_GRADLE_PROJECT_githubPassword: ${{ secrets.GITHUB_TOKEN }}
- name: build docker image
uses: nais/docker-build-push@v0
id: docker-build-push
with:
team: tbd
tag: latest
dockerfile: Dockerfile
docker_context: filbackend
image_suffix: lokal
project_id: ${{ vars.NAIS_MANAGEMENT_PROJECT_ID }}
identity_provider: ${{ secrets.NAIS_WORKLOAD_IDENTITY_PROVIDER }}
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
outputs:
image: ${{ steps.docker-build-push.outputs.image }}

deployDev:
deploy:
name: deploy
needs: build
runs-on: ubuntu-latest
Expand Down
9 changes: 9 additions & 0 deletions filbackend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Lokal Spanner

Dette er en readme i startgropen.

```
gcloud auth configure-docker europe-north1-docker.pkg.dev
gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://europe-north1-docker.pkg.dev
docker europe-north1-docker.pkg.dev/nais-management-233d/tbd/helse-spanner-lokal:latest
```
77 changes: 77 additions & 0 deletions filbackend/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import java.nio.file.Paths

val ktorVersion = "2.3.7"

plugins {
kotlin("jvm") version "1.9.22"
}

repositories {
val githubPassword: String? by project
mavenCentral()
/* ihht. https://github.com/navikt/utvikling/blob/main/docs/teknisk/Konsumere%20biblioteker%20fra%20Github%20Package%20Registry.md
så plasseres github-maven-repo (med autentisering) før nav-mirror slik at github actions kan anvende førstnevnte.
Det er fordi nav-mirroret kjører i Google Cloud og da ville man ellers fått unødvendige utgifter til datatrafikk mellom Google Cloud og GitHub
*/
maven {
url = uri("https://maven.pkg.github.com/navikt/maven-release")
credentials {
username = "x-access-token"
password = githubPassword
}
}
maven("https://github-package-registry-mirror.gc.nav.no/cached/maven-release")
}

dependencies {
implementation("io.ktor:ktor-server-core:$ktorVersion")
implementation("io.ktor:ktor-server-cio:$ktorVersion")
implementation("ch.qos.logback:logback-classic:1.4.12")
implementation("net.logstash.logback:logstash-logback-encoder:7.4") {
exclude("com.fasterxml.jackson.core")
exclude("com.fasterxml.jackson.dataformat")
}
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

tasks {
test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
}

jar {
mustRunAfter(":frontend:npm_run_build")

archiveFileName.set("app.jar")

manifest {
attributes["Main-Class"] = "FilbackendKt"
attributes["Class-Path"] = configurations.runtimeClasspath.get().joinToString(separator = " ") {
it.name
}
}


from({ Paths.get(project(":frontend").layout.buildDirectory.get().toString()) }) {
into("static")
}

doLast {
configurations.runtimeClasspath.get()
.filter { it.name != "app.jar" }
.forEach {
val file = File("${layout.buildDirectory.get()}/libs/${it.name}")
if (!file.exists()) it.copyTo(file)
}
}
}
}

61 changes: 61 additions & 0 deletions filbackend/src/main/kotlin/Filbackend.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Filsluse.finnHendelse
import Filsluse.finnPerson
import io.ktor.http.*
import io.ktor.http.ContentType.Application.Json
import io.ktor.http.HttpStatusCode.Companion.BadRequest
import io.ktor.http.HttpStatusCode.Companion.NotFound
import io.ktor.http.HttpStatusCode.Companion.OK
import io.ktor.server.application.*
import io.ktor.server.cio.*
import io.ktor.server.engine.*
import io.ktor.server.http.content.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import java.util.UUID

fun main() {
embeddedServer(CIO, environment = applicationEngineEnvironment {

module {
routing {
frontendRouting()

get("/api/meg") {
call.respondText("""{ "navn": "Viggo Velferdsvenn", "ident": "V1660" } """, Json, OK)
}

get("/api/person/") {
val maskertId = call.request.headers["maskertId"]?.let { UUID.fromString(it) } ?: return@get call.respond(BadRequest)
val person = maskertId.finnPerson() ?: return@get call.respond(NotFound)
call.respondText(person, Json, OK)
}

post("/api/uuid/") {
val maskertId = call.request.headers["maskertId"]?.let { UUID.fromString(it) } ?: return@post call.respond(BadRequest)
val person = maskertId.finnPerson() ?: return@post call.respond(NotFound)
call.respondText(person, Json, OK)
}

get("/api/hendelse/{meldingsreferanse}") {
val meldingsreferanse = call.parameters["meldingsreferanse"]?.let { UUID.fromString(it) } ?: return@get call.respond(BadRequest)
val hendelse = meldingsreferanse.finnHendelse() ?: return@get call.respond(NotFound)
call.respondText(hendelse, Json, OK)
}
}
}

connector {
port = 3000
}
}).start(true)
}

private fun Route.frontendRouting() {
get("/person/*") {
call.respondText(
this::class.java.classLoader.getResource("static/index.html")!!.readText(),
ContentType.Text.Html
)
}
staticResources("/", "/static/")
}
9 changes: 9 additions & 0 deletions filbackend/src/main/kotlin/Filsluse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import java.io.File
import java.util.UUID

object Filsluse {
private fun String.innhold() = File(this).takeIf { it.exists() }?.readText()
private fun spannerRoot() = "${System.getenv("HOME")}/.spanner"
fun UUID.finnPerson() = "${spannerRoot()}/personer/$this.json".innhold()
fun UUID.finnHendelse() = "${spannerRoot()}/hendelser/$this.json".innhold()
}
8 changes: 8 additions & 0 deletions lokal-spanner.settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rootProject.name = "lokal-spanner"

pluginManagement {
repositories {
gradlePluginPortal()
}
}
include("frontend", "filbackend")

0 comments on commit 3198739

Please sign in to comment.