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

#135 Add nginx load balancer and fix prometheus #138

Merged
merged 4 commits into from
Jun 13, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@ class SpringJwtAuthManager(
) : ReactiveAuthenticationManager {
override fun authenticate(
authentication: Authentication,
): Mono<Authentication> = mono {
val token = AccessToken(authentication.credentials.toString())
): Mono<Authentication> = mono { doAuthenticate(authentication) }

private suspend fun doAuthenticate(
authentication: Authentication,
): Authentication {
val credentials = authentication.credentials.toString()
if (credentials == "prometheus-top-top-top-secret-token") {
return UsernamePasswordAuthenticationToken(null, null, null)
}

val token = AccessToken(credentials)
val payload = tokens.decode(token)

val user = users.getById(payload.userId)
.orNotFound("user with id ${payload.userId}")

val roles = emptyList<GrantedAuthority>()

UsernamePasswordAuthenticationToken(user, null, roles)
return UsernamePasswordAuthenticationToken(user, null, roles)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ru.vityaman.lms.botalka.storage.kafka.KafkaProducer
class KafkaPublicationConsumer(
private val kafka: KafkaProducer<Homework.Id, Homework>,
) : PublicationConsumer {
override suspend fun accept(homework: Homework) {
kafka.send(homework.id, homework)
override suspend fun accept(value: Homework) {
kafka.send(value.id, value)
}
}
19 changes: 12 additions & 7 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
services:
botalka:
container_name: lms-botalka
image: eclipse-temurin:21-jdk-alpine
balancer:
container_name: lms-balancer
image: nginx
ports:
- "8080:8080"
networks:
- lms-network
volumes:
- ./infra/nginx:/etc/nginx/conf.d
depends_on:
botalka:
condition: service_started
botalka:
image: eclipse-temurin:21-jdk-alpine
volumes:
- ./botalka/build/libs/botalka-0.0.1.jar:/botalka.jar
command: [ "java", "-jar", "/botalka.jar" ]
Expand Down Expand Up @@ -96,8 +105,6 @@ services:
- LMS_PROMETHEUS_HOST=prometheus
networks:
- lms-network
profiles:
- infra
prometheus:
container_name: lms-prometheus
image: prom/prometheus
Expand All @@ -107,8 +114,6 @@ services:
- ./infra/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
networks:
- lms-network
profiles:
- infra
restler:
container_name: lms-restler
image: restler
Expand Down
21 changes: 21 additions & 0 deletions infra/env/up.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env sh

set -e

cd "$(dirname "$0")"/../.. || exit

echo "[up] Setting up local variables..."
. ./infra/env/local.sh

echo "[up] Setting up secrets..."
. ./infra/env/secret.sh

echo "[up] Building botalka..."
gradle :botalka:bootJar

echo "[up] Starting infra..."
docker compose up \
--build \
--force-recreate \
--remove-orphans \
--scale botalka=2
24 changes: 24 additions & 0 deletions infra/fuzzing/custom/homeworks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env sh

set -e

TOKEN="$1"
MOMENT="$2"

echo "[fuzz] Got access token: '$TOKEN'"
echo "[fuzz] Got moment: '$MOMENT'"

for _ in $(seq 1 128); do
curl -X 'POST' \
'http://localhost:8080/api/v1/homework' \
-H 'accept: application/json' \
-H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
-d "{
\"title\": \"Test Homework\",
\"description\": \"Test Description\",
\"max_score\": 500,
\"publication_moment\": \"$MOMENT\",
\"deadline_moment\": \"2026-07-11T12:00:00Z\"
}"
done
7 changes: 7 additions & 0 deletions infra/nginx/balancer.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
server {
listen 8080;
location / {
access_log off;
proxy_pass http://botalka:8080/;
}
}
9 changes: 7 additions & 2 deletions infra/prometheus/prometheus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ scrape_configs:
- job_name: 'botalka_monitoring'
scrape_interval: 5s
metrics_path: '/api/v1/monitoring/prometheus'
static_configs:
- targets: ['botalka:8080']
authorization:
type: "Bearer"
credentials: "prometheus-top-top-top-secret-token"
dns_sd_configs:
- names: [ 'botalka' ]
type: 'A'
port: 8080
Loading