From dea2b525a55dad6d1c79850f01a1245963fcb409 Mon Sep 17 00:00:00 2001 From: Tyson Gern Date: Fri, 22 Dec 2023 10:54:21 -0700 Subject: [PATCH] Rearrange components --- .github/workflows/build.yml | 8 ++-- Dockerfile | 4 +- README.md | 6 +-- .../{webapp => report-app}/build.gradle.kts | 5 +-- .../initialcapacity/reportapp/Application.kt | 38 +++++++++++++++++++ .../reportapp/SubscriptionsRoutes.kt | 14 +++++++ .../reportapp/SubscriptionsRoutesTest.kt | 17 +++++++++ .../initialcapacity/reportapp/TestSupport.kt | 20 ++++++++++ .../build.gradle.kts | 4 +- .../reportworker}/Application.kt | 2 +- .../reportworker}/ApplicationTest.kt | 4 +- .../com/initialcapacity/webapp/Application.kt | 20 ---------- .../initialcapacity/webapp/ApplicationTest.kt | 35 ----------------- .../com/initialcapacity/database/Databases.kt | 2 +- .../database/DatabaseTemplateTest.kt | 4 +- .../stripe/SubscriptionsGatewayTest.kt | 4 +- .../stripe/SubscriptionsServiceTest.kt | 4 +- components/web/build.gradle.kts | 25 ------------ .../com.initialcapacity.web/BasicWeb.kt | 33 ---------------- .../test/initialcapacity/io/BasicWebTest.kt | 35 ----------------- settings.gradle.kts | 5 +-- 21 files changed, 114 insertions(+), 175 deletions(-) rename applications/{webapp => report-app}/build.gradle.kts (91%) create mode 100644 applications/report-app/src/main/kotlin/com/initialcapacity/reportapp/Application.kt create mode 100644 applications/report-app/src/main/kotlin/com/initialcapacity/reportapp/SubscriptionsRoutes.kt create mode 100644 applications/report-app/src/test/kotlin/test/initialcapacity/reportapp/SubscriptionsRoutesTest.kt create mode 100644 applications/report-app/src/test/kotlin/test/initialcapacity/reportapp/TestSupport.kt rename applications/{worker => report-worker}/build.gradle.kts (89%) rename applications/{worker/src/main/kotlin/com/initialcapacity/worker => report-worker/src/main/kotlin/com/initialcapacity/reportworker}/Application.kt (88%) rename applications/{worker/src/test/kotlin/test/initialcapacity/worker => report-worker/src/test/kotlin/test/initialcapacity/reportworker}/ApplicationTest.kt (62%) delete mode 100644 applications/webapp/src/main/kotlin/com/initialcapacity/webapp/Application.kt delete mode 100644 applications/webapp/src/test/kotlin/test/initialcapacity/webapp/ApplicationTest.kt delete mode 100644 components/web/build.gradle.kts delete mode 100644 components/web/src/main/kotlin/com.initialcapacity.web/BasicWeb.kt delete mode 100644 components/web/src/test/kotlin/test/initialcapacity/io/BasicWebTest.kt diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bbb5503..5b628b7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -38,8 +38,8 @@ jobs: run: ./gradlew build - uses: actions/upload-artifact@v4 with: - name: webapp.jar - path: applications/webapp/build/libs/webapp.jar + name: report-app.jar + path: applications/report-app/build/libs/report-app.jar retention-days: 1 artifact-registry: @@ -52,8 +52,8 @@ jobs: - uses: actions/checkout@v4 - uses: actions/download-artifact@v4 with: - name: webapp.jar - path: applications/webapp/build/libs + name: report-app.jar + path: applications/report-app/build/libs - name: "pack" run: | docker build -t us-central1-docker.pkg.dev/${{ secrets.GOOGLE_PROJECT_ID }}/basic-ktor-project-review/basic-ktor-project:${{ github.sha }} . \ diff --git a/Dockerfile b/Dockerfile index 828a666..9c5e039 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,5 +14,5 @@ ENV PATH "${JAVA_HOME}/bin:${PATH}" COPY --from=jre-build /javaruntime $JAVA_HOME RUN mkdir /opt/applications -COPY applications/webapp/build/libs/webapp.jar /opt/applications/ -CMD [ "java", "-jar", "/opt/applications/webapp.jar" ] +COPY applications/report-app/build/libs/report-app.jar /opt/applications/ +CMD [ "java", "-jar", "/opt/applications/report-app.jar" ] diff --git a/README.md b/README.md index 2d78d4c..9101a27 100644 --- a/README.md +++ b/README.md @@ -27,17 +27,17 @@ Install dependencies. 1. Run the application locally. ```bash - java -jar applications/webapp/build/libs/webapp.jar + java -jar applications/report-app/build/libs/report-app.jar ``` ## Run with Docker 1. Build with Docker. ```bash - docker build -t webapp . --platform linux/amd64 + docker build -t report-app . --platform linux/amd64 ``` 1. Run with Docker. ```bash - docker run -p 8080:8080 --env-file .env.docker webapp + docker run -p 8080:8080 --env-file .env.docker report-app ``` diff --git a/applications/webapp/build.gradle.kts b/applications/report-app/build.gradle.kts similarity index 91% rename from applications/webapp/build.gradle.kts rename to applications/report-app/build.gradle.kts index df020b3..4f948f2 100644 --- a/applications/webapp/build.gradle.kts +++ b/applications/report-app/build.gradle.kts @@ -2,7 +2,7 @@ plugins { id("org.jetbrains.kotlin.plugin.serialization") } -group = "com.initialcapacity.webapp" +group = "com.initialcapacity.reportapp" val ktorVersion: String by project val stripeVersion: String by project @@ -16,7 +16,6 @@ dependencies { implementation(project(":components:database-support")) implementation(project(":components:stripe-support")) implementation(project(":components:subscriptions")) - implementation(project(":components:web")) implementation("io.ktor:ktor-server-core-jvm:$ktorVersion") implementation("io.ktor:ktor-server-freemarker-jvm:$ktorVersion") @@ -34,7 +33,7 @@ task("run") { tasks { jar { - manifest { attributes("Main-Class" to "com.initialcapacity.webapp.ApplicationKt") } + manifest { attributes("Main-Class" to "com.initialcapacity.reportapp.ApplicationKt") } duplicatesStrategy = DuplicatesStrategy.INCLUDE from({ configurations.runtimeClasspath.get() diff --git a/applications/report-app/src/main/kotlin/com/initialcapacity/reportapp/Application.kt b/applications/report-app/src/main/kotlin/com/initialcapacity/reportapp/Application.kt new file mode 100644 index 0000000..27fc791 --- /dev/null +++ b/applications/report-app/src/main/kotlin/com/initialcapacity/reportapp/Application.kt @@ -0,0 +1,38 @@ +package com.initialcapacity.reportapp + +import com.initialcapacity.database.DatabaseTemplate +import com.initialcapacity.database.dataSource +import com.initialcapacity.stripe.StripeGateway +import com.initialcapacity.subscriptions.SubscriptionsGateway +import com.initialcapacity.subscriptions.SubscriptionsService +import com.stripe.Stripe +import io.ktor.server.application.* +import io.ktor.server.engine.* +import io.ktor.server.netty.* +import io.ktor.server.routing.* + +fun Application.module(databaseUrl: String, stripeApiKey: String) { + Stripe.apiKey = stripeApiKey + + val stripeSupport = StripeGateway() + val template = DatabaseTemplate(dataSource(databaseUrl)) + val gateway = SubscriptionsGateway(template) + val subscriptionsService = SubscriptionsService(stripeSupport, gateway) + + routing { + subscriptions(subscriptionsService) + } +} + +fun main() { + embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = { + module( + databaseUrl = requiredEnvironmentVariable("DATABASE_URL"), + stripeApiKey = requiredEnvironmentVariable("STRIPE_API_KEY") + ) + }).start(wait = true) +} + +fun requiredEnvironmentVariable(value: String): String { + return System.getenv()[value] ?: throw RuntimeException("missing configuration: $value") +} diff --git a/applications/report-app/src/main/kotlin/com/initialcapacity/reportapp/SubscriptionsRoutes.kt b/applications/report-app/src/main/kotlin/com/initialcapacity/reportapp/SubscriptionsRoutes.kt new file mode 100644 index 0000000..607cda2 --- /dev/null +++ b/applications/report-app/src/main/kotlin/com/initialcapacity/reportapp/SubscriptionsRoutes.kt @@ -0,0 +1,14 @@ +package com.initialcapacity.reportapp + +import com.initialcapacity.subscriptions.SubscriptionsService +import io.ktor.server.application.call +import io.ktor.server.response.respondText +import io.ktor.server.routing.Route +import io.ktor.server.routing.get + +fun Route.subscriptions(service: SubscriptionsService) { + get("/") { + val list = service.list() + call.respondText("hi! found=${list.size} subscriptions") + } +} diff --git a/applications/report-app/src/test/kotlin/test/initialcapacity/reportapp/SubscriptionsRoutesTest.kt b/applications/report-app/src/test/kotlin/test/initialcapacity/reportapp/SubscriptionsRoutesTest.kt new file mode 100644 index 0000000..8cb3a6e --- /dev/null +++ b/applications/report-app/src/test/kotlin/test/initialcapacity/reportapp/SubscriptionsRoutesTest.kt @@ -0,0 +1,17 @@ +package test.initialcapacity.reportapp + +import io.ktor.client.request.get +import io.ktor.client.statement.bodyAsText +import io.ktor.http.HttpStatusCode +import org.junit.Test +import kotlin.test.assertContains +import kotlin.test.assertEquals + +class SubscriptionsRoutesTest { + @Test + fun testList() = testApp { + val response = client.get("/") + assertEquals(HttpStatusCode.OK, response.status) + assertContains(response.bodyAsText(), "hi!") + } +} diff --git a/applications/report-app/src/test/kotlin/test/initialcapacity/reportapp/TestSupport.kt b/applications/report-app/src/test/kotlin/test/initialcapacity/reportapp/TestSupport.kt new file mode 100644 index 0000000..f3803b8 --- /dev/null +++ b/applications/report-app/src/test/kotlin/test/initialcapacity/reportapp/TestSupport.kt @@ -0,0 +1,20 @@ +package test.initialcapacity.reportapp + +import com.initialcapacity.reportapp.module +import com.stripe.Stripe +import io.ktor.server.testing.ApplicationTestBuilder +import io.ktor.server.testing.testApplication + +fun testApp(block: suspend ApplicationTestBuilder.() -> Unit) { + Stripe.overrideApiBase("http://localhost:12111") + + testApplication { + application { + module( + databaseUrl = "jdbc:postgresql://localhost/example_test?user=initialdev&password=initialdev", + stripeApiKey = "sk_test_xxxxxxxxxxxxxxxxxxxxxxxx" + ) + } + block() + } +} diff --git a/applications/worker/build.gradle.kts b/applications/report-worker/build.gradle.kts similarity index 89% rename from applications/worker/build.gradle.kts rename to applications/report-worker/build.gradle.kts index a8e08b8..1c63d98 100644 --- a/applications/worker/build.gradle.kts +++ b/applications/report-worker/build.gradle.kts @@ -2,7 +2,7 @@ plugins { id("org.jetbrains.kotlin.plugin.serialization") } -group = "com.initialcapacity.worker" +group = "com.initialcapacity.reportworker" version = "0.0.1" repositories { @@ -21,7 +21,7 @@ task("run") { tasks { jar { - manifest { attributes("Main-Class" to "com.initialcapacity.worker.ApplicationKt") } + manifest { attributes("Main-Class" to "com.initialcapacity.reportworker.ApplicationKt") } duplicatesStrategy = DuplicatesStrategy.INCLUDE from({ configurations.runtimeClasspath.get() diff --git a/applications/worker/src/main/kotlin/com/initialcapacity/worker/Application.kt b/applications/report-worker/src/main/kotlin/com/initialcapacity/reportworker/Application.kt similarity index 88% rename from applications/worker/src/main/kotlin/com/initialcapacity/worker/Application.kt rename to applications/report-worker/src/main/kotlin/com/initialcapacity/reportworker/Application.kt index 06b195c..6d12853 100644 --- a/applications/worker/src/main/kotlin/com/initialcapacity/worker/Application.kt +++ b/applications/report-worker/src/main/kotlin/com/initialcapacity/reportworker/Application.kt @@ -1,4 +1,4 @@ -package com.initialcapacity.worker +package com.initialcapacity.reportworker import org.slf4j.LoggerFactory diff --git a/applications/worker/src/test/kotlin/test/initialcapacity/worker/ApplicationTest.kt b/applications/report-worker/src/test/kotlin/test/initialcapacity/reportworker/ApplicationTest.kt similarity index 62% rename from applications/worker/src/test/kotlin/test/initialcapacity/worker/ApplicationTest.kt rename to applications/report-worker/src/test/kotlin/test/initialcapacity/reportworker/ApplicationTest.kt index ac005a7..5dde540 100644 --- a/applications/worker/src/test/kotlin/test/initialcapacity/worker/ApplicationTest.kt +++ b/applications/report-worker/src/test/kotlin/test/initialcapacity/reportworker/ApplicationTest.kt @@ -1,6 +1,6 @@ -package test.initialcapacity.worker +package test.initialcapacity.reportworker -import com.initialcapacity.worker.Application +import com.initialcapacity.reportworker.Application import org.junit.Test import kotlin.test.assertTrue diff --git a/applications/webapp/src/main/kotlin/com/initialcapacity/webapp/Application.kt b/applications/webapp/src/main/kotlin/com/initialcapacity/webapp/Application.kt deleted file mode 100644 index 5c840e5..0000000 --- a/applications/webapp/src/main/kotlin/com/initialcapacity/webapp/Application.kt +++ /dev/null @@ -1,20 +0,0 @@ -package com.initialcapacity.webapp - -import com.initialcapacity.database.configureDatabases -import com.initialcapacity.web.basic -import com.initialcapacity.web.requiredEnvironmentVariable -import io.ktor.server.application.Application -import io.ktor.server.engine.embeddedServer -import io.ktor.server.netty.Netty - -fun Application.module() { - basic( - configureDatabases(requiredEnvironmentVariable("DATABASE_URL")), - requiredEnvironmentVariable("STRIPE_API_KEY") - ) -} - -fun main() { - embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module) - .start(wait = true) -} diff --git a/applications/webapp/src/test/kotlin/test/initialcapacity/webapp/ApplicationTest.kt b/applications/webapp/src/test/kotlin/test/initialcapacity/webapp/ApplicationTest.kt deleted file mode 100644 index b3ce570..0000000 --- a/applications/webapp/src/test/kotlin/test/initialcapacity/webapp/ApplicationTest.kt +++ /dev/null @@ -1,35 +0,0 @@ -package test.initialcapacity.webapp - -import com.initialcapacity.database.configureDatabases -import com.initialcapacity.web.basic -import com.stripe.Stripe -import io.ktor.client.request.get -import io.ktor.client.statement.bodyAsText -import io.ktor.http.HttpStatusCode -import io.ktor.server.testing.testApplication -import org.junit.Before -import org.junit.Test -import kotlin.test.assertContains -import kotlin.test.assertEquals - -class ApplicationTest { - - @Before - fun setUp() { - Stripe.apiKey = "sk_test_xxxxxxxxxxxxxxxxxxxxxxxx" - Stripe.overrideApiBase("http://localhost:12111") - } - - @Test - fun test() = testApplication { - application { - basic( - configureDatabases("jdbc:postgresql://localhost/example_test?user=initialdev&password=initialdev"), - "sk_test_xxxxxxxxxxxxxxxxxxxxxxxx" - ) - } - val response = client.get("/") - assertEquals(HttpStatusCode.OK, response.status) - assertContains(response.bodyAsText(), "hi!") - } -} diff --git a/components/database-support/src/main/kotlin/com/initialcapacity/database/Databases.kt b/components/database-support/src/main/kotlin/com/initialcapacity/database/Databases.kt index a0d540a..7d91812 100644 --- a/components/database-support/src/main/kotlin/com/initialcapacity/database/Databases.kt +++ b/components/database-support/src/main/kotlin/com/initialcapacity/database/Databases.kt @@ -4,7 +4,7 @@ import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource import javax.sql.DataSource -fun configureDatabases(url: String): DataSource { +fun dataSource(url: String): DataSource { return HikariDataSource(HikariConfig().apply { jdbcUrl = url validate() diff --git a/components/database-support/src/test/kotlin/test/initialcapacity/database/DatabaseTemplateTest.kt b/components/database-support/src/test/kotlin/test/initialcapacity/database/DatabaseTemplateTest.kt index bd87ea1..80a16a5 100644 --- a/components/database-support/src/test/kotlin/test/initialcapacity/database/DatabaseTemplateTest.kt +++ b/components/database-support/src/test/kotlin/test/initialcapacity/database/DatabaseTemplateTest.kt @@ -1,14 +1,14 @@ package test.initialcapacity.database import com.initialcapacity.database.DatabaseTemplate -import com.initialcapacity.database.configureDatabases +import com.initialcapacity.database.dataSource import org.junit.Before import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertNull class DatabaseTemplateTest { - private val source = configureDatabases("jdbc:postgresql://localhost/example_test?user=initialdev&password=initialdev") + private val source = dataSource("jdbc:postgresql://localhost/example_test?user=initialdev&password=initialdev") private val template = DatabaseTemplate(source) @Before diff --git a/components/subscriptions/src/test/kotlin/test/initialcapacity/stripe/SubscriptionsGatewayTest.kt b/components/subscriptions/src/test/kotlin/test/initialcapacity/stripe/SubscriptionsGatewayTest.kt index d849821..5eb1b08 100644 --- a/components/subscriptions/src/test/kotlin/test/initialcapacity/stripe/SubscriptionsGatewayTest.kt +++ b/components/subscriptions/src/test/kotlin/test/initialcapacity/stripe/SubscriptionsGatewayTest.kt @@ -1,14 +1,14 @@ package test.initialcapacity.stripe import com.initialcapacity.database.DatabaseTemplate -import com.initialcapacity.database.configureDatabases +import com.initialcapacity.database.dataSource import com.initialcapacity.subscriptions.SubscriptionsGateway import org.junit.Before import org.junit.Test import kotlin.test.assertEquals class SubscriptionsGatewayTest { - private val source = configureDatabases("jdbc:postgresql://localhost/example_test?user=initialdev&password=initialdev") + private val source = dataSource("jdbc:postgresql://localhost/example_test?user=initialdev&password=initialdev") private val template = DatabaseTemplate(source) @Before diff --git a/components/subscriptions/src/test/kotlin/test/initialcapacity/stripe/SubscriptionsServiceTest.kt b/components/subscriptions/src/test/kotlin/test/initialcapacity/stripe/SubscriptionsServiceTest.kt index 7f96b3b..13990ad 100644 --- a/components/subscriptions/src/test/kotlin/test/initialcapacity/stripe/SubscriptionsServiceTest.kt +++ b/components/subscriptions/src/test/kotlin/test/initialcapacity/stripe/SubscriptionsServiceTest.kt @@ -1,7 +1,7 @@ package test.initialcapacity.stripe import com.initialcapacity.database.DatabaseTemplate -import com.initialcapacity.database.configureDatabases +import com.initialcapacity.database.dataSource import com.initialcapacity.stripe.StripeGateway import com.initialcapacity.subscriptions.SubscriptionsGateway import com.initialcapacity.subscriptions.SubscriptionsService @@ -11,7 +11,7 @@ import org.junit.Test import kotlin.test.assertEquals class SubscriptionsServiceTest { - private val source = configureDatabases("jdbc:postgresql://localhost/example_test?user=initialdev&password=initialdev") + private val source = dataSource("jdbc:postgresql://localhost/example_test?user=initialdev&password=initialdev") private val template = DatabaseTemplate(source) @Before diff --git a/components/web/build.gradle.kts b/components/web/build.gradle.kts deleted file mode 100644 index 89b8d4f..0000000 --- a/components/web/build.gradle.kts +++ /dev/null @@ -1,25 +0,0 @@ -plugins { - id("org.jetbrains.kotlin.plugin.serialization") -} - -val ktorVersion: String by project -val stripeVersion: String by project -val postgresVersion: String by project -val hikariVersion: String by project - -dependencies { - implementation(project(":components:database-support")) - implementation(project(":components:stripe-support")) - implementation(project(":components:subscriptions")) - - implementation("io.ktor:ktor-server-core-jvm:$ktorVersion") - implementation("io.ktor:ktor-server-freemarker-jvm:$ktorVersion") - implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktorVersion") - implementation("io.ktor:ktor-server-netty-jvm:$ktorVersion") - - implementation("com.stripe:stripe-java:$stripeVersion") - implementation("org.postgresql:postgresql:$postgresVersion") - implementation("com.zaxxer:HikariCP:$hikariVersion") - - testImplementation("io.ktor:ktor-server-tests-jvm:$ktorVersion") -} diff --git a/components/web/src/main/kotlin/com.initialcapacity.web/BasicWeb.kt b/components/web/src/main/kotlin/com.initialcapacity.web/BasicWeb.kt deleted file mode 100644 index 2fb4650..0000000 --- a/components/web/src/main/kotlin/com.initialcapacity.web/BasicWeb.kt +++ /dev/null @@ -1,33 +0,0 @@ -package com.initialcapacity.web - -import com.initialcapacity.database.DatabaseTemplate -import com.initialcapacity.stripe.StripeGateway -import com.initialcapacity.subscriptions.SubscriptionsGateway -import com.initialcapacity.subscriptions.SubscriptionsService -import com.stripe.Stripe -import io.ktor.server.application.Application -import io.ktor.server.application.call -import io.ktor.server.response.respondText -import io.ktor.server.routing.get -import io.ktor.server.routing.routing -import javax.sql.DataSource - -fun Application.basic(dataSource: DataSource, stripeKey: String) { - Stripe.apiKey = stripeKey - - val stripeSupport = StripeGateway() - val template = DatabaseTemplate(dataSource) - val gateway = SubscriptionsGateway(template) - val subscription = SubscriptionsService(stripeSupport, gateway) - - routing { - get("/") { - val list = subscription.list() - call.respondText("hi! found=${list.size} subscriptions") - } - } -} - -fun requiredEnvironmentVariable(value: String): String { - return System.getenv().get(value) ?: throw RuntimeException("missing configuration: $value") -} \ No newline at end of file diff --git a/components/web/src/test/kotlin/test/initialcapacity/io/BasicWebTest.kt b/components/web/src/test/kotlin/test/initialcapacity/io/BasicWebTest.kt deleted file mode 100644 index bc81f8d..0000000 --- a/components/web/src/test/kotlin/test/initialcapacity/io/BasicWebTest.kt +++ /dev/null @@ -1,35 +0,0 @@ -package test.initialcapacity.io - -import com.initialcapacity.database.configureDatabases -import com.initialcapacity.web.basic -import com.stripe.Stripe -import io.ktor.client.request.get -import io.ktor.client.statement.bodyAsText -import io.ktor.http.HttpStatusCode -import io.ktor.server.testing.testApplication -import org.junit.Before -import org.junit.Test -import kotlin.test.assertContains -import kotlin.test.assertEquals - -class BasicWebTest { - @Before - fun setUp() { - Stripe.apiKey = "sk_test_xxxxxxxxxxxxxxxxxxxxxxxx" - Stripe.overrideApiBase("http://localhost:12111") - } - - @Test - fun test() = testApplication { - val dataSource = configureDatabases("jdbc:postgresql://localhost/example_test?user=initialdev&password=initialdev") - application { - basic( - dataSource, - "sk_test_xxxxxxxxxxxxxxxxxxxxxxxx", - ) - } - val response = client.get("/") - assertEquals(HttpStatusCode.OK, response.status) - assertContains(response.bodyAsText(), "hi!") - } -} diff --git a/settings.gradle.kts b/settings.gradle.kts index 7849468..dabfefb 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,11 +1,10 @@ rootProject.name = "basic-ktor-project" include( - "applications:webapp", - "applications:worker", + "applications:report-app", + "applications:report-worker", "components:database-support", "components:stripe-support", "components:subscriptions", - "components:web", ) \ No newline at end of file