Skip to content

Commit

Permalink
refs #2 Spannerを導入
Browse files Browse the repository at this point in the history
  • Loading branch information
averak committed Oct 15, 2023
1 parent bcc1f13 commit 27b13b1
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ jobs:
distribution: corretto
java-version: 17
cache: gradle
- uses: google-github-actions/setup-gcloud@v1

- name: start database
run: |
make init_spanner_emulator
- name: backend test
run: |
Expand Down
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
GOOGLE_CLOUD_PROJECT_ID="gsync"

.PHONY: init_spanner_emulator
init_spanner_emulator:
docker-compose up -d
gcloud config configurations create emulator || true
gcloud config set auth/disable_credentials true
gcloud config set project ${GOOGLE_CLOUD_PROJECT_ID}
gcloud config set api_endpoint_overrides/spanner http://localhost:9020/
gcloud spanner instances create gsync --config=emulator-config --description="gsync" --nodes=1
gcloud spanner databases create gsync --instance=gsync

.PHONY: test
test:
./gradlew test

.PHONY: lint
lint:
./gradlew spotlessCheck

.PHONY: format
format:
./gradlew spotlessApply

.PHONY: check_dependencies
check_dependencies:
./gradlew dependencyUpdates -Drevision=release
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ This component provides only reusable features that can be used across various g
* Java OpenJDK 17
* Kotlin 1.9
* Quarkus 3.4
* TiDB 7.1
* Cloud Spanner

### Running the application in dev mode

You can run your application in dev mode that enables live coding.

```shell
docker compose up -d
make init_spanner_emulator
./gradlew quarkusDev
```

Expand Down Expand Up @@ -66,13 +66,13 @@ Or, if you don't have GraalVM installed, you can run the native executable build
[Gradle Versions Plugin](https://github.com/ben-manes/gradle-versions-plugin) checks outdated dependencies.

```shell
$ ./gradlew dependencyUpdates -Drevision=release
make check_dependencies
```

### Run code formatter

This codebase is formatted by [ktlint](https://github.com/pinterest/ktlint).

```shell
./gradlew spotlessApply
make format
```
7 changes: 7 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ val quarkusPlatformArtifactId: String by project
val quarkusPlatformVersion: String by project

dependencies {
// Quarkus
implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}"))
implementation("io.quarkus:quarkus-kotlin")
implementation("io.quarkus:quarkus-resteasy-reactive-jackson")
Expand All @@ -44,6 +45,12 @@ dependencies {
implementation("io.quarkus:quarkus-config-yaml")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

// GCP
implementation("io.quarkiverse.googlecloudservices:quarkus-google-cloud-spanner:2.5.0")

// utils
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.15.2")

// test
testImplementation("io.quarkus:quarkus-junit5")
testImplementation("io.github.dvgaba:easy-random-core:6.2.0")
Expand Down
6 changes: 6 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
spanner:
build: ./docker/spanner
ports:
- "9010:9010"
- "9020:9020"
1 change: 1 addition & 0 deletions docker/spanner/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM gcr.io/cloud-spanner-emulator/emulator
42 changes: 42 additions & 0 deletions src/main/kotlin/net/averak/gsync/infrastructure/json/JsonUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.averak.gsync.infrastructure.json

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.KotlinFeature
import com.fasterxml.jackson.module.kotlin.KotlinModule

class JsonUtils {

companion object {

private val objectMapper = ObjectMapper()
.registerModule(
KotlinModule.Builder()
.withReflectionCacheSize(512)
.configure(KotlinFeature.NullToEmptyCollection, false)
.configure(KotlinFeature.NullToEmptyMap, false)
.configure(KotlinFeature.NullIsSameAsDefault, false)
.configure(KotlinFeature.SingletonSupport, false)
.configure(KotlinFeature.StrictNullChecks, false)
.build()
)
.registerModule(JavaTimeModule())
.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, true)

@JvmStatic
fun encode(value: Any?): String {
return if (value == null) {
"{}"
} else {
objectMapper.writeValueAsString(value)
}
}

@JvmStatic
fun <T> decode(json: String, clazz: Class<T>): T {
return objectMapper.readValue(json, clazz)
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.averak.gsync.infrastructure.spanner

import com.google.cloud.spanner.Spanner
import jakarta.inject.Singleton

@Singleton
class SpannerClient(
private val spanner: Spanner,
)
16 changes: 16 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
quarkus:
application:
name: "gsync"
version: "1.0.0-SNAPSHOT"
datasource:
db-kind: spanner
jdbc:
url: jdbc:cloudspanner:/projects/${quarkus.google.cloud.project-id}/instances/${quarkus.google.cloud.instance-id}/databases/${quarkus.google.cloud.database-id}
driver: com.google.cloud.spanner.jdbc.JdbcDriver
google:
cloud:
project-id: ${GOOGLE_CLOUD_PROJECT_ID:gsync}
instance-id: ${GOOGLE_CLOUD_SPANNER_INSTANCE_ID:gsync}
database-id: ${GOOGLE_CLOUD_SPANNER_DATABASE_NAME:gsync}
spanner:
emulator-host: http://localhost:9020
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.averak.gsync.adapter.handler.controller

import net.averak.gsync.AbstractSpec

class AbstractController_IT extends AbstractSpec {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ import io.quarkus.test.junit.QuarkusTest
import spock.lang.Specification

@QuarkusTest
abstract class AbstractSpec extends Specification {
}
abstract class AbstractSpec : Specification()
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ package net.averak.gsync
import jakarta.enterprise.context.ApplicationScoped

@ApplicationScoped
class TestConfig {
}
class TestConfig
29 changes: 0 additions & 29 deletions src/test/kotlin/net/averak/gsync/testutils/JsonUtils.kt

This file was deleted.

Empty file.

0 comments on commit 27b13b1

Please sign in to comment.