From 8640d9146daff5c8009de16187744561ca2c45d1 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 11 May 2020 01:50:40 +0200 Subject: [PATCH] Update postgres-json to Add ShadowJar compatibility Add .env for redis and ES change group name of project --- .env | 7 +++-- .idea/runConfigurations/Build.xml | 31 +++++++++++++++++++ .../runConfigurations/Build_without_test.xml | 31 +++++++++++++++++++ .idea/runConfigurations/SQL_Fixtures.xml | 13 ++++++++ .idea/runConfigurations/SQL_Tests.xml | 4 +-- build.gradle.kts | 13 ++++++-- docker-compose.yml | 7 +++-- docker/app/Dockerfile | 1 - src/main/kotlin/fr/dcproject/Application.kt | 2 -- src/main/kotlin/fr/dcproject/Configuration.kt | 14 ++++----- src/main/kotlin/fr/dcproject/Module.kt | 28 ++++++++--------- .../fr/dcproject/event/ConfigNotification.kt | 4 +-- .../fr/dcproject/event/publisher/Publisher.kt | 4 +-- src/main/resources/application.conf | 5 +++ src/test/kotlin/RunCucumberTest.kt | 22 ++++++------- 15 files changed, 135 insertions(+), 51 deletions(-) create mode 100644 .idea/runConfigurations/Build.xml create mode 100644 .idea/runConfigurations/Build_without_test.xml create mode 100644 .idea/runConfigurations/SQL_Fixtures.xml diff --git a/.env b/.env index 9b8243a..60d4429 100644 --- a/.env +++ b/.env @@ -4,10 +4,11 @@ DATABASE_URL=jdbc:postgresql:dc-project APP_PORT=8080 OPENAPI_PORT=8181 -SONARQUBE_PORT=9000 +SONARQUBE_PORT=9002 ELASTIC_REST=9200 ELASTIC_NODES=9300 +ELASTICSEARCH_CONNECTION=http://elasticsearch:9200 POSTGRESQL_PORT=5432 DB_HOST=db @@ -17,9 +18,9 @@ DB_USER=dc-project DB_PWD=dc-project REDIS_PORT=6379 -REDIS_CONNECTION=redis://localhost:6379 +REDIS_CONNECTION=redis://redis:6379 REDIS_COMMANDER_PORT=8081 RABBITMQ_PORT=5672 -RABBITMQ_CONNECTION=amqp://localhost:5672 +RABBITMQ_CONNECTION=amqp://rabbitmq:5672 RABBITMQ_MANAGEMENT_PORT=15672 \ No newline at end of file diff --git a/.idea/runConfigurations/Build.xml b/.idea/runConfigurations/Build.xml new file mode 100644 index 0000000..9d96f27 --- /dev/null +++ b/.idea/runConfigurations/Build.xml @@ -0,0 +1,31 @@ + + + + + + + + + true + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Build_without_test.xml b/.idea/runConfigurations/Build_without_test.xml new file mode 100644 index 0000000..0898a5a --- /dev/null +++ b/.idea/runConfigurations/Build_without_test.xml @@ -0,0 +1,31 @@ + + + + + + + + + true + + + \ No newline at end of file diff --git a/.idea/runConfigurations/SQL_Fixtures.xml b/.idea/runConfigurations/SQL_Fixtures.xml new file mode 100644 index 0000000..2fe9093 --- /dev/null +++ b/.idea/runConfigurations/SQL_Fixtures.xml @@ -0,0 +1,13 @@ + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/SQL_Tests.xml b/.idea/runConfigurations/SQL_Tests.xml index 9472e2c..89d93be 100644 --- a/.idea/runConfigurations/SQL_Tests.xml +++ b/.idea/runConfigurations/SQL_Tests.xml @@ -1,12 +1,12 @@ - diff --git a/build.gradle.kts b/build.gradle.kts index 6d6caf5..8ab36d7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,6 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.owasp.dependencycheck.reporting.ReportGenerator +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar val ktor_version: String by project val kotlin_version: String by project @@ -9,7 +10,7 @@ val koinVersion: String by project val jackson_version: String by project val cucumber_version: String by project -group = "fr.dcproject" +group = "com.github.flecomte" version = "0.0.1" plugins { @@ -19,7 +20,7 @@ plugins { id("maven-publish") id("org.jetbrains.kotlin.jvm") version "1.3.50" - id("com.github.johnrengelman.shadow") version "5.0.0" + id("com.github.johnrengelman.shadow") version "5.2.0" id("org.jlleitschuh.gradle.ktlint") version "8.2.0" id("org.owasp.dependencycheck") version "5.1.0" id("org.sonarqube") version "2.7" @@ -44,6 +45,12 @@ tasks.withType { } } +tasks { + named("shadowJar") { + mergeServiceFiles("META-INF/services") + } +} + jacoco { toolVersion = "0.8.3" } @@ -86,7 +93,7 @@ dependencies { implementation("net.pearx.kasechange:kasechange-jvm:1.1.0") implementation("com.auth0:java-jwt:3.8.2") implementation("com.github.jasync-sql:jasync-postgresql:1.0.7") - implementation("com.github.flecomte:postgres-json:1.0.4") + implementation("com.github.flecomte:postgres-json:1.1.1") implementation("com.github.flecomte:ktor-voter:1.0.1") implementation("com.sendgrid:sendgrid-java:4.4.1") implementation("io.lettuce:lettuce-core:5.2.2.RELEASE") diff --git a/docker-compose.yml b/docker-compose.yml index ce78543..f08040b 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -42,10 +42,11 @@ services: ports: - ${APP_PORT}:8080 environment: - DB_HOST: db + DB_HOST: ${DB_HOST} SEND_GRID_KEY: ${SEND_GRID_KEY} - REDIS_CONNECTION: redis://redis:6379 - RABBITMQ_CONNECTION: amqp://rabbitmq:5671 + REDIS_CONNECTION: ${REDIS_CONNECTION} + RABBITMQ_CONNECTION: ${RABBITMQ_CONNECTION} + ELASTICSEARCH_CONNECTION: ${ELASTICSEARCH_CONNECTION} depends_on: - elasticsearch - db diff --git a/docker/app/Dockerfile b/docker/app/Dockerfile index 7f11568..71c4948 100644 --- a/docker/app/Dockerfile +++ b/docker/app/Dockerfile @@ -9,7 +9,6 @@ RUN chown -R $APPLICATION_USER /app USER $APPLICATION_USER COPY ./libs/dcproject-0.0.1-all.jar /app/dcproject.jar -COPY ./resources /app/resources WORKDIR /app CMD ["java", "-server", "-XX:+UnlockExperimentalVMOptions", "-XX:InitialRAMFraction=2", "-XX:MinRAMFraction=2", "-XX:MaxRAMFraction=2", "-XX:+UseG1GC", "-XX:MaxGCPauseMillis=100", "-XX:+UseStringDeduplication", "-jar", "dcproject.jar"] diff --git a/src/main/kotlin/fr/dcproject/Application.kt b/src/main/kotlin/fr/dcproject/Application.kt index f70aa7f..00092dd 100644 --- a/src/main/kotlin/fr/dcproject/Application.kt +++ b/src/main/kotlin/fr/dcproject/Application.kt @@ -73,7 +73,6 @@ fun Application.module(env: Env = PROD) { } install(DataConversion) { - // TODO move to postgresJson lib convert { decode { values, _ -> values.singleOrNull()?.let { UUID.fromString(it) } @@ -359,7 +358,6 @@ fun Application.module(env: Env = PROD) { maxAge = Duration.ofDays(1) } - // TODO move to postgresJson lib if (env == PROD) { get().run() } diff --git a/src/main/kotlin/fr/dcproject/Configuration.kt b/src/main/kotlin/fr/dcproject/Configuration.kt index 25686f0..d4a0a7c 100644 --- a/src/main/kotlin/fr/dcproject/Configuration.kt +++ b/src/main/kotlin/fr/dcproject/Configuration.kt @@ -5,17 +5,16 @@ import com.auth0.jwt.JWTVerifier import com.auth0.jwt.algorithms.Algorithm import com.typesafe.config.ConfigFactory import fr.dcproject.entity.UserI -import org.eclipse.jetty.util.resource.JarResource -import java.io.File import java.util.* +import java.net.URI -class Config { +object Config { private var config = ConfigFactory.load() - val sqlFiles: File = try { - File(this::class.java.getResource("/sql").toURI()) - } catch (e: IllegalArgumentException) { - JarResource.newResource("./resources/sql").file + object Sql { + val migrationFiles: URI = this::class.java.getResource("/sql/migrations").toURI() + val functionFiles: URI = this::class.java.getResource("/sql/functions").toURI() + val fixtureFiles: URI = this::class.java.getResource("/sql/fixtures").toURI() } val envName: String = config.getString("app.envName") @@ -27,6 +26,7 @@ class Config { var password: String = config.getString("db.password") val port: Int = config.getInt("db.port") val redis: String = config.getString("redis.connection") + val elasticsearch: String = config.getString("elasticsearch.connection") val rabbitmq: String = config.getString("rabbitmq.connection") val exchangeNotificationName = "notification" val sendGridKey: String = config.getString("mail.sendGrid.key") diff --git a/src/main/kotlin/fr/dcproject/Module.kt b/src/main/kotlin/fr/dcproject/Module.kt index ae16703..e9d2a88 100644 --- a/src/main/kotlin/fr/dcproject/Module.kt +++ b/src/main/kotlin/fr/dcproject/Module.kt @@ -39,35 +39,33 @@ import fr.dcproject.repository.VoteComment as VoteCommentRepository import fr.dcproject.repository.VoteConstitution as VoteConstitutionRepository import fr.dcproject.repository.Workgroup as WorkgroupRepository -val config = Config() - @KtorExperimentalAPI val Module = module { - single { config } + single { Config } // SQL connection single { Connection( - host = config.host, - port = config.port, - database = config.database, - username = config.username, - password = config.password + host = Config.host, + port = Config.port, + database = Config.database, + username = Config.username, + password = Config.password ) } // Launch Database migration - single { Migrations(connection = get(), directory = config.sqlFiles) } + single { Migrations(get(), Config.Sql.migrationFiles, Config.Sql.functionFiles) } // Redis client single> { - RedisClient.create(config.redis).connect()?.async() ?: error("Unable to connect to redis") + RedisClient.create(Config.redis).connect()?.async() ?: error("Unable to connect to redis") } // RabbitMQ single { - ConnectionFactory().apply { setUri(config.rabbitmq) } + ConnectionFactory().apply { setUri(Config.rabbitmq) } } // JsonSerializer @@ -93,7 +91,7 @@ val Module = module { single { Requester.RequesterFactory( connection = get(), - functionsDirectory = config.sqlFiles.resolve("functions") + functionsDirectory = Config.Sql.functionFiles ).createRequester() } @@ -117,15 +115,15 @@ val Module = module { // Elasticsearch Client single { RestClient.builder( - HttpHost("localhost", 9200, "http") + HttpHost.create(Config.elasticsearch) ).build() } single { ArticleViewManager(get()) } // Mailler - single { Mailer(config.sendGridKey) } + single { Mailer(Config.sendGridKey) } // SSO Manager for connection - single { SsoManager(get(), config.domain, get()) } + single { SsoManager(get(), Config.domain, get()) } } diff --git a/src/main/kotlin/fr/dcproject/event/ConfigNotification.kt b/src/main/kotlin/fr/dcproject/event/ConfigNotification.kt index 50e04fd..e192a1f 100644 --- a/src/main/kotlin/fr/dcproject/event/ConfigNotification.kt +++ b/src/main/kotlin/fr/dcproject/event/ConfigNotification.kt @@ -3,7 +3,7 @@ package fr.dcproject.event import com.fasterxml.jackson.databind.ObjectMapper import com.rabbitmq.client.* import com.rabbitmq.client.BuiltinExchangeType.DIRECT -import fr.dcproject.config +import fr.dcproject.Config import fr.dcproject.entity.Article import fr.dcproject.event.publisher.Publisher import fr.dcproject.repository.Follow @@ -32,7 +32,7 @@ fun EventSubscriber.Configuration.configEvent( serialiser: ObjectMapper ) { /* Config Rabbit */ - val exchangeName = config.exchangeNotificationName + val exchangeName = Config.exchangeNotificationName rabbitFactory.newConnection().use { connection -> connection.createChannel().use { channel -> channel.queueDeclare("push", true, false, false, null) diff --git a/src/main/kotlin/fr/dcproject/event/publisher/Publisher.kt b/src/main/kotlin/fr/dcproject/event/publisher/Publisher.kt index 28858c4..89bd9a5 100644 --- a/src/main/kotlin/fr/dcproject/event/publisher/Publisher.kt +++ b/src/main/kotlin/fr/dcproject/event/publisher/Publisher.kt @@ -2,7 +2,7 @@ package fr.dcproject.event.publisher import com.fasterxml.jackson.databind.ObjectMapper import com.rabbitmq.client.ConnectionFactory -import fr.dcproject.config +import fr.dcproject.Config import fr.dcproject.event.EntityEvent import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job @@ -19,7 +19,7 @@ class Publisher( return GlobalScope.launch { factory.newConnection().use { connection -> connection.createChannel().use { channel -> - channel.basicPublish(config.exchangeNotificationName, "", null, it.serialize().toByteArray()) + channel.basicPublish(Config.exchangeNotificationName, "", null, it.serialize().toByteArray()) logger.debug("Publish message ${it.target.id}") } } diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index b3fa396..441fcec 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -32,6 +32,11 @@ rabbitmq { connection = ${?RABBITMQ_CONNECTION} } +elasticsearch { + connection = "http://localhost:9200" + connection = ${?ELASTICSEARCH_CONNECTION} +} + mail { sendGrid { key = ${?SEND_GRID_KEY} diff --git a/src/test/kotlin/RunCucumberTest.kt b/src/test/kotlin/RunCucumberTest.kt index 06cf770..75578c5 100644 --- a/src/test/kotlin/RunCucumberTest.kt +++ b/src/test/kotlin/RunCucumberTest.kt @@ -1,6 +1,6 @@ import feature.KtorServerContext +import fr.dcproject.Config import fr.dcproject.Env.CUCUMBER -import fr.dcproject.config import fr.dcproject.module import fr.dcproject.utils.LoggerDelegate import fr.postgresjson.connexion.Connection @@ -36,9 +36,9 @@ class RunCucumberTest : En, KoinTest { init { if (!unitialized) { - config.database = "test" - config.username = "test" - config.password = "test" + Config.database = "test" + Config.username = "test" + Config.password = "test" withTestApplication({ module(CUCUMBER) }) { migrations() @@ -47,9 +47,9 @@ class RunCucumberTest : En, KoinTest { } Before(-1) { _: Scenario -> - config.database = "test" - config.username = "test" - config.password = "test" + Config.database = "test" + Config.username = "test" + Config.password = "test" ktorContext.start() //language=PostgreSQL get().sendQuery("start transaction;", listOf()) @@ -63,9 +63,9 @@ class RunCucumberTest : En, KoinTest { } private fun migrations() { - config.database = "test" - config.username = "test" - config.password = "test" + Config.database = "test" + Config.username = "test" + Config.password = "test" val migrations: Migrations = get() migrations.forceAllDown() migrations.run() @@ -89,7 +89,7 @@ class RunCucumberTest : En, KoinTest { private fun getFixturesRequester(): Requester { return Requester.RequesterFactory( connection = get(), - queriesDirectory = config.sqlFiles.resolve("fixtures") + queriesDirectory = Config.Sql.fixtureFiles ).createRequester() } }