Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic auth #2

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ indent_size = 2
[*.{yml,yaml}]
indent_size = 2

[*.tf]
[*.md]
max_line_length = 100
indent_size = 2

9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Spring example project

This is a simple example project that demonstrates how to use Spring Boot with Kotlin.

## Docs

- Blueprints
- [Gradle Build](docs%2Fblueprints%2FGradleBuild.md)
- [Gradle Build for Kotlin JVM projects](docs%2Fblueprints%2FGradleBuildKotlinJvm.md)
35 changes: 27 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.springframework.boot") version "2.7.0"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.6.21"
kotlin("plugin.spring") version "1.6.21"
kotlin("plugin.jpa") version "1.6.21"
id("org.jlleitschuh.gradle.ktlint") version "10.2.1"
id("org.springframework.boot") version "3.2.5"
id("io.spring.dependency-management") version "1.1.4"
kotlin("jvm") version "1.9.23"
kotlin("plugin.spring") version "1.9.23"
kotlin("plugin.jpa") version "1.9.23"
id("org.jlleitschuh.gradle.ktlint") version "12.1.0"
id("com.github.davidmc24.gradle.plugin.avro") version "1.9.1"
id("com.github.ben-manes.versions") version "0.51.0"
}

group = "com.vacuumlabs.example"
Expand All @@ -15,27 +17,44 @@ java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
mavenCentral()
maven("https://packages.confluent.io/maven")
}

extra["springCloudVersion"] = "2021.0.3"
extra["springCloudVersion"] = "2023.0.1"
extra["testcontainersVersion"] = "1.19.7"

dependencies {
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.cloud:spring-cloud-starter-stream-kafka")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("net.logstash.logback:logstash-logback-encoder:6.6")
implementation("net.logstash.logback:logstash-logback-encoder:7.4") // 6.6
implementation("io.confluent:kafka-avro-serializer:7.6.1")
runtimeOnly("org.postgresql:postgresql")
runtimeOnly("io.micrometer:micrometer-registry-prometheus")
developmentOnly("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.kafka:spring-kafka-test")
testImplementation("org.awaitility:awaitility:3.0.0") // 4.2.1
testImplementation("org.testcontainers:junit-jupiter")
testImplementation("org.testcontainers:postgresql")
testImplementation("org.testcontainers:kafka")
testImplementation("org.springframework.security:spring-security-test")
}

dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
mavenBom("org.testcontainers:testcontainers-bom:${property("testcontainersVersion")}")
}
}

tasks.withType<KotlinCompile> {
dependsOn("generateAvroJava")
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
Expand Down
59 changes: 59 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
version: '2'
services:
postgres:
image: postgres:14-alpine
ports:
- '5432:5432'
environment:
POSTGRES_DB: spring-example
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_HOST_AUTH_METHOD: trust

kafdrop:
image: obsidiandynamics/kafdrop
ports:
- '9000:9000'
environment:
KAFKA_BROKERCONNECT: 'kafka:9092'
JVM_OPTS: '-Xms16M -Xmx48M -Xss180K -XX:-TieredCompilation -XX:+UseStringDeduplication'
depends_on:
- 'kafka'

zookeeper:
image: confluentinc/cp-zookeeper:7.1.2
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ports:
- '22181:2181'

kafka:
image: confluentinc/cp-kafka:7.1.2
depends_on:
- zookeeper
ports:
- '29092:29092'
- '9092:29092'
environment:
ALLOW_PLAINTEXT_LISTENER: 'yes'
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: "PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092"
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'

schemaregistry:
image: confluentinc/cp-schema-registry:7.1.2
depends_on:
- zookeeper
environment:
SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: "PLAINTEXT://kafka:9092"
SCHEMA_REGISTRY_HOST_NAME: schemaregistry
SCHEMA_REGISTRY_LISTENERS: "http://0.0.0.0:8081"
ports:
- 8081:8081
112 changes: 112 additions & 0 deletions docs/blueprints/GradleBuild.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Gradle Build Blueprint

This is a simple gradle project without submodules.

## Directory layout

- `gradle` -- gradle files that are commited to git
- `wrapper` -- [gradle wrapper](https://docs.gradle.org/current/userguide/gradle_wrapper.html)
files, config and mini jar for running a gradle version pinned to the project
using `./gradlew`
- `libs.versions.toml` -- (optional) Centralized version management for dependencies
using [Gradle Version Catalogs](https://docs.gradle.org/current/userguide/platforms.html#sub:central-declaration-of-dependencies)
- `.gradle` -- local cache directory for gradle files, can be safely deleted if needed
- `build` -- output directory for files generated during the build process, `.gitignored`
- `gradle clean` task will remove it
- `src` -- directory containing source files
- the actual structure depends on the applied gradle plugins, e.g. java / kotlin JVM plugin

See also:
- https://docs.gradle.org/current/userguide/organizing_gradle_projects.html
- https://docs.gradle.org/current/userguide/gradle_wrapper.html
- https://docs.gradle.org/current/userguide/platforms.html#sub:central-declaration-of-dependencies
- https://docs.gradle.org/current/userguide/java_plugin.html#source_sets

## Build files

- `settings.gradle.kts` -- this is what turns a directory into a standalone gradle project (vs
submodule)
- `build.gradle.kts` -- build script, for each (sub)project
- `gradle.properties` -- (optional) -- for setting gradle properties (e.g. max memory) and possibly
project properties
- `gradlew` -- gradle wrapper script for UNIX systems
- `gradlew.bat` -- gradle wrapper script for Windows systems

Note: settings and build files can also written in Groovy (without `.kts` extension), but Kotlin DSL
is recommended. Kotlin is more strongly typed so you get better IDE support,
and is expressive with DSLs, etc.

See also:

- https://docs.gradle.org/current/userguide/kotlin_dsl.html

## Configuring `settings.gradle.kts`

### Root project

At a minimum set the root project name. Gradle uses the directory name by default, but it's better
to be explicit, in case someone checks out the git repo under a different name.
This name will be used for naming produced artifacts, etc.

```kotlin
rootProject.name = "test"
```

### Subprojects

If you have subprojects, you can include them like this. This is useful for monorepos, where you
have multiple projects in the same repo.

Note: subprojects don't need a `settings.gradle.kts` file.

```kotlin
include("sub-project")
```

See also:

- https://docs.gradle.org/current/userguide/multi_project_builds.html#sub:building_the_tree

### Composite builds

If you'd like to develop multiple standalone projects at the same time you can use composite builds.
This way you can:
-
- add dependency without needing to publish other project to a local or remote repository
- open the other projects automatically in the IDE
- do refactoring across multiple projects
- you can also put independent projects in a monorepo and develop them using a composite build, but
build and release them independently in a CICD pipeline

Note: Subprojects DO need a `settings.gradle.kts` file -- because they can also exist
independently.

```kotlin
includeBuild("../another-project")
```

See also:
- https://docs.gradle.org/current/userguide/composite_builds.html

### Toolchain management (optional)

This section is just to document something you might come across in generated projects, but is not
yet tested .

We generally install JDKs separately:
- on dev machines: using[SDKMAN](https://sdkman.io/) or similar
- on CI : using a base image with the desired JDK version

If you generate a new project with `gradle init`, it will configure this plugin in `settings.gradle.kts` :

```kotlin
plugins {
// Apply the foojay-resolver plugin to allow automatic download of JDKs
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}
```

See also:
- https://docs.gradle.org/current/userguide/toolchains.html#sub:download_repositories
- https://github.com/gradle/foojay-toolchains
- https://github.com/foojayio/discoapi
Loading