Skip to content

Commit

Permalink
Merge pull request Co-Epi#26 from jmccance/separate-api-and-impl
Browse files Browse the repository at this point in the history
Restructure project to multi-module
  • Loading branch information
ramnanib2 authored Apr 30, 2020
2 parents 8e618c6 + a08fcc9 commit 128c22c
Show file tree
Hide file tree
Showing 19 changed files with 195 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.gradle*
.swagger-codegen*
Dockerfile*
/build/
build/

# Terraform

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ terraform init
./gradlew build
```

Then run:
Then build the shadow jar for the Lambda:

```sh
./gradlew shadowJar
./gradlew api-aws-lambda:shadowJar
```

## AWS Deploy via Terraform
## Deploy

These steps assume you have already initialized Terraform as described in "Infrastructure Setup" above.

Expand Down
21 changes: 21 additions & 0 deletions api-aws-lambda/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
id("project-defaults")
id("com.github.johnrengelman.shadow") version "5.2.0"
}

dependencies {
implementation(project(":core"))

implementation("com.amazonaws:aws-lambda-java-core:1.2.0")
implementation("com.amazonaws:aws-lambda-java-events:2.2.7")
implementation("com.amazonaws:aws-lambda-java-log4j2:1.1.0")
implementation("com.fasterxml.jackson.core:jackson-annotations")
implementation("com.fasterxml.jackson.core:jackson-core")
implementation("com.fasterxml.jackson.core:jackson-databind")
implementation("commons-io:commons-io:2.6")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0")
}

shadowJar {
exclude "**/Log4j2Plugins.dat"
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ class TCNCloudAPIHandler: RequestHandler<APIGatewayProxyRequestEvent, APIGateway
const val DATE_KEY = "date"
const val INTERVAL_NUMBER_KEY = "intervalNumber"
const val INTERVAL_LENGTH_MS_KEY = "intervalLengthMs"
const val INTERVAL_LENGTH_MS: Long = 6 * 3600 * 1000

fun generateIntervalForCurrentTime(): Long {
return generateIntervalForTimestamp(Instant.now().toEpochMilli())
}

fun generateIntervalForTimestamp(timestamp: Long): Long {
return timestamp / INTERVAL_LENGTH_MS
}
}

override fun handleRequest(input: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent {
Expand Down Expand Up @@ -113,9 +104,9 @@ class TCNCloudAPIHandler: RequestHandler<APIGatewayProxyRequestEvent, APIGateway
}
val intervalLengthMs = queryParameters[INTERVAL_LENGTH_MS_KEY]?.toLong()

if (intervalLengthMs != INTERVAL_LENGTH_MS) {
if (intervalLengthMs != Intervals.INTERVAL_LENGTH_MS) {
throw UnexpectedIntervalLengthException("$intervalLengthMs is invalid for the date " +
"$DATE_KEY. Please use $INTERVAL_LENGTH_MS to calculate $INTERVAL_NUMBER_KEY")
"$DATE_KEY. Please use ${Intervals.INTERVAL_LENGTH_MS} to calculate $INTERVAL_NUMBER_KEY")
}
}
} catch (ex: DateTimeParseException) {
Expand Down
File renamed without changes.
80 changes: 5 additions & 75 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,79 +1,9 @@
buildscript {
ext.kotlin_version = '1.3.71'
group "org.coepi"
version "1.0.0"

repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.github.jengelman.gradle.plugins:shadow:5.2.0"
}
}

plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.71'
}

apply plugin: 'kotlin'
apply plugin: 'com.github.johnrengelman.shadow'

group 'org.coepi'
version '1.0.0'

repositories {
mavenCentral()
maven {
url 'http://dynamodb-local.s3-website-us-west-2.amazonaws.com/release'
}
}

configurations {
dynamodb
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.apache.commons:commons-lang3:3.10"
implementation "com.amazonaws:aws-java-sdk-dynamodb:1.11.755"
implementation "com.amazonaws:aws-java-sdk-s3:1.11.755"
implementation "com.amazonaws:aws-lambda-java-core:1.2.0"
implementation "com.amazonaws:aws-lambda-java-events:2.2.7"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0"
implementation "commons-io:commons-io:2.6"
implementation "com.fasterxml.jackson.core:jackson-databind:2.10.3"
implementation "com.fasterxml.jackson.core:jackson-core:2.10.3"
implementation "com.fasterxml.jackson.core:jackson-annotations:2.10.3"

implementation "org.apache.logging.log4j:log4j-api:2.13.0"
implementation "org.apache.logging.log4j:log4j-core:2.13.0"
implementation "org.apache.logging.log4j:log4j-slf4j18-impl:2.13.0"
implementation "com.amazonaws:aws-lambda-java-log4j2:1.1.0"

testImplementation(
'org.assertj:assertj-core:3.12.2',
'org.junit.jupiter:junit-jupiter-api:5.4.2',
'com.amazonaws:DynamoDBLocal:1.11.477'
)
testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.4.2')
dynamodb fileTree (dir: 'lib', include: ["*.dylib", "*.so", "*.dll"])
dynamodb 'com.amazonaws:DynamoDBLocal:1.11.477'

task copyNativeDeps(type: Copy) {
from configurations.dynamodb
into "$project.buildDir/libs/"
}

test.dependsOn copyNativeDeps
test.doFirst {
systemProperty "java.library.path", "./build/libs"
}
test {
useJUnitPlatform()
}
}

shadowJar {
exclude "**/Log4j2Plugins.dat"
wrapper {
gradleVersion = "6.3"
distributionType = Wrapper.DistributionType.ALL
}

// TODO: Use a gradle plugin to generate models, example (https://github.com/Yelp/swagger-gradle-codegen).
Expand Down
22 changes: 22 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
`kotlin-dsl`
}

gradlePlugin {
plugins {
register("project-defaults-plugin") {
id = "project-defaults"
implementationClass = "ProjectDefaultsPlugin"
description = "Applies common project settings"
}
}
}

repositories {
jcenter()
}

dependencies {
implementation(gradleApi())
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72")
}
84 changes: 84 additions & 0 deletions buildSrc/src/main/kotlin/ProjectDefaultsPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.tasks.testing.Test
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

class ProjectDefaultsPlugin : Plugin<Project> {
override fun apply(project: Project) {
with(project) {
configurePlugins()
configureBuild()
configureDependencies()
configureTests()
}
}
}

private fun Project.configurePlugins() {
plugins.apply("java-library")
plugins.apply("org.jetbrains.kotlin.jvm")
}

private fun Project.configureBuild() {
configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

tasks {
withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += listOf(
"-progressive",
"-Xjsr305=strict"
)
javaParameters = true
jvmTarget = "11"
}
}
}
}

private fun Project.configureDependencies() {
repositories {
mavenCentral()
}

dependencies {
// Apply platform dependencies to constrain versions across projects

"implementation"(platform("com.amazonaws:aws-java-sdk-bom:1.11.769"))
"implementation"(platform("com.fasterxml.jackson:jackson-bom:2.10.3"))
"implementation"(platform("org.apache.logging.log4j:log4j-bom:2.13.2"))

// Common dependencies for all projects

"implementation"("org.apache.logging.log4j:log4j-api")
"implementation"("org.apache.logging.log4j:log4j-core")
"implementation"("org.apache.logging.log4j:log4j-slf4j18-impl")
"implementation"("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

"testImplementation"(platform("org.junit:junit-bom:5.6.2"))

"testImplementation"("org.assertj:assertj-core:3.12.2")
"testImplementation"("org.junit.jupiter:junit-jupiter-api")

"testRuntimeOnly"("org.junit.jupiter:junit-jupiter-engine")
}
}

private fun Project.configureTests() {
tasks {
withType(Test::class) {
useJUnitPlatform()

testLogging {
events = setOf(TestLogEvent.PASSED, TestLogEvent.FAILED, TestLogEvent.SKIPPED)
}
}
}
}
33 changes: 33 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
plugins {
id("project-defaults")
}

configurations {
dynamodb
}

repositories {
maven {
url("http://dynamodb-local.s3-website-us-west-2.amazonaws.com/release")
}
}

dependencies {
implementation("com.amazonaws:aws-java-sdk-dynamodb")
implementation("org.apache.commons:commons-lang3:3.10")

testImplementation("com.amazonaws:DynamoDBLocal:1.11.477")

dynamodb(fileTree(dir: 'lib', include: ["*.dylib", "*.so", "*.dll"]))
dynamodb("com.amazonaws:DynamoDBLocal:1.11.477")
}

task copyNativeDeps(type: Copy) {
from(configurations.dynamodb)
into("${project.buildDir}/libs")
}

test.dependsOn(copyNativeDeps)
test.doFirst {
systemProperty("java.library.path", "./build/libs")
}
File renamed without changes.
13 changes: 13 additions & 0 deletions core/src/main/kotlin/org/coepi/api/v4/Intervals.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.coepi.api.v4

import java.time.Instant

object Intervals {
const val INTERVAL_LENGTH_MS: Long = 6 * 3600 * 1000
}

fun generateIntervalForTimestamp(timestamp: Long): Long =
timestamp / Intervals.INTERVAL_LENGTH_MS

fun generateIntervalForCurrentTime(): Long =
generateIntervalForTimestamp(Instant.now().toEpochMilli())
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TCNReportsDao {

val reportId = generateReportId(date, intervalNumber)
val randomId = UUID.randomUUID().toString()
val reportRecord = TCNReportRecord(reportId, randomId, timestamp, reportData)
val reportRecord = TCNReportRecord(reportId, randomId, timestamp, reportData)
this.dynamoMapper.save(reportRecord)
return reportRecord
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.coepi.api.v4.dao

import org.coepi.api.v4.TCNCloudAPIHandler
import org.coepi.api.v4.generateIntervalForTimestamp
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
Expand All @@ -19,7 +19,7 @@ class TCNReportsDaoTest {
fun addReport_sanity() {
val date = LocalDate.now(ZoneId.of("UTC"))
val now = Instant.now().toEpochMilli()
val intervalNumber = TCNCloudAPIHandler.generateIntervalForTimestamp(now)
val intervalNumber = generateIntervalForTimestamp(now)
dao.addReport(reportData, date, intervalNumber, now)
val reports = dao.queryReports(date, intervalNumber)
Assertions.assertTrue(reports.isNotEmpty())
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
7 changes: 6 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
rootProject.name = 'coepi-backend-kotlin'
rootProject.name = 'coepi-backend-kotlin'

include(
":api-aws-lambda",
":core"
)
2 changes: 1 addition & 1 deletion terraform/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ EOF
}

locals {
jarfile = "../build/libs/coepi-backend-kotlin-1.0.0-all.jar"
jarfile = "../api-aws-lambda/build/libs/api-aws-lambda-all.jar"
}

resource "aws_lambda_function" "tcn_lambda" {
Expand Down

0 comments on commit 128c22c

Please sign in to comment.