Skip to content

Commit

Permalink
Rearrange components
Browse files Browse the repository at this point in the history
  • Loading branch information
tygern committed Dec 22, 2023
1 parent 2e37a47 commit dea2b52
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 175 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }} . \
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -34,7 +33,7 @@ task<JavaExec>("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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
}
Original file line number Diff line number Diff line change
@@ -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")
}
}
Original file line number Diff line number Diff line change
@@ -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!")
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id("org.jetbrains.kotlin.plugin.serialization")
}

group = "com.initialcapacity.worker"
group = "com.initialcapacity.reportworker"
version = "0.0.1"

repositories {
Expand All @@ -21,7 +21,7 @@ task<JavaExec>("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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.initialcapacity.worker
package com.initialcapacity.reportworker

import org.slf4j.LoggerFactory

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
25 changes: 0 additions & 25 deletions components/web/build.gradle.kts

This file was deleted.

33 changes: 0 additions & 33 deletions components/web/src/main/kotlin/com.initialcapacity.web/BasicWeb.kt

This file was deleted.

Loading

0 comments on commit dea2b52

Please sign in to comment.