From 8c7b0e733479f2cf537c1a2542d4b75bd4d9de67 Mon Sep 17 00:00:00 2001 From: Yannick Lamprecht Date: Fri, 29 Apr 2022 00:25:38 +0200 Subject: [PATCH 1/5] added context sensitivity --- bot/src/main/java/de/chojo/gamejam/api/Api.java | 9 +++------ .../de/chojo/gamejam/configuration/elements/Api.java | 6 ++++++ conf/dev/temp.config.json | 1 + docker/kubernetes/ingress.yaml | 4 +++- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/bot/src/main/java/de/chojo/gamejam/api/Api.java b/bot/src/main/java/de/chojo/gamejam/api/Api.java index 2434280..c912c18 100644 --- a/bot/src/main/java/de/chojo/gamejam/api/Api.java +++ b/bot/src/main/java/de/chojo/gamejam/api/Api.java @@ -13,15 +13,11 @@ import de.chojo.gamejam.data.JamData; import de.chojo.gamejam.data.TeamData; import io.javalin.Javalin; -import io.javalin.apibuilder.ApiBuilder; import io.javalin.http.Context; -import io.javalin.http.HttpCode; import io.javalin.plugin.openapi.OpenApiOptions; import io.javalin.plugin.openapi.OpenApiPlugin; import io.javalin.plugin.openapi.ui.ReDocOptions; import io.javalin.plugin.openapi.ui.SwaggerOptions; -import io.swagger.models.Info; -import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.sharding.ShardManager; import org.slf4j.Logger; @@ -54,16 +50,17 @@ public Api(Configuration configuration, ShardManager shardManager, TeamData team private void build() { app = Javalin.create(config -> { + config.contextPath = configuration.api().contextPath(); config.registerPlugin(getConfiguredOpenApiPlugin()); config.accessManager((handler, ctx, routeRoles) -> { - if(ctx.path().startsWith("/swagger") || ctx.path().startsWith("/redoc")){ + if(ctx.matchedPath().startsWith("/swagger") || ctx.matchedPath().startsWith("/redoc")){ handler.handle(ctx); return; } var token = ctx.req.getHeader("authorization"); if (token == null) { - ctx.status(HttpServletResponse.SC_UNAUTHORIZED).result("Please provde a valid token in the authorization header."); + ctx.status(HttpServletResponse.SC_UNAUTHORIZED).result("Please provide a valid token in the authorization header."); } else if (!token.equals(configuration.api().token())) { ctx.status(HttpServletResponse.SC_UNAUTHORIZED).result("Unauthorized"); } else { diff --git a/bot/src/main/java/de/chojo/gamejam/configuration/elements/Api.java b/bot/src/main/java/de/chojo/gamejam/configuration/elements/Api.java index 3dfd6fd..8e54c0d 100644 --- a/bot/src/main/java/de/chojo/gamejam/configuration/elements/Api.java +++ b/bot/src/main/java/de/chojo/gamejam/configuration/elements/Api.java @@ -8,10 +8,16 @@ @SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"}) public class Api { + + private String contextPath = "/"; private String host = "localhost"; private int port = 8888; private String token = "letmein"; + public String contextPath() { + return contextPath; + } + public String host() { return host; } diff --git a/conf/dev/temp.config.json b/conf/dev/temp.config.json index 468fd12..f3f8cc6 100644 --- a/conf/dev/temp.config.json +++ b/conf/dev/temp.config.json @@ -13,6 +13,7 @@ "poolSize" : 5 }, "api" : { + "contextPath": "game-jam/", "host" : "0.0.0.0", "port" : 8888, "token" : "letmein" diff --git a/docker/kubernetes/ingress.yaml b/docker/kubernetes/ingress.yaml index 563e29d..3322155 100644 --- a/docker/kubernetes/ingress.yaml +++ b/docker/kubernetes/ingress.yaml @@ -4,7 +4,9 @@ metadata: name: game-jam-bot-ingres annotations: kubernetes.io/ingress.class: traefik - traefik.ingress.kubernetes.io/router.middlewares: default-game-jam-strip-prefix@kubernetescrd +# Stripping the prefix and providing it as `X-Forwarded-Prefix` isn't supported by Javalin and the Swagger module +# Thus we deactivate the middleware for now +# traefik.ingress.kubernetes.io/router.middlewares: default-game-jam-strip-prefix@kubernetescrd spec: rules: - http: From bcb798a4bbc7bb4f28e659a48799fbf4d36d07b7 Mon Sep 17 00:00:00 2001 From: Yannick Lamprecht Date: Sat, 30 Apr 2022 19:14:31 +0200 Subject: [PATCH 2/5] bump version of JDA and javalin, there was a javalin swagger-ui bug (version difference) --- bot/build.gradle.kts | 4 ++-- docker/bot.DockerFile | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/build.gradle.kts b/bot/build.gradle.kts index 649ec47..0465ba0 100644 --- a/bot/build.gradle.kts +++ b/bot/build.gradle.kts @@ -14,12 +14,12 @@ repositories { dependencies { // discord - implementation("net.dv8tion", "JDA", "5.0.0-alpha.9") { + implementation("net.dv8tion", "JDA", "5.0.0-alpha.11") { exclude(module = "opus-java") } implementation("de.chojo", "cjda-util", "2.4.0+alpha.11") - implementation("io.javalin", "javalin-bundle", "4.4.0") + implementation("io.javalin", "javalin-bundle", "4.5.0") // database diff --git a/docker/bot.DockerFile b/docker/bot.DockerFile index 22f9d27..d8995e7 100644 --- a/docker/bot.DockerFile +++ b/docker/bot.DockerFile @@ -1,4 +1,4 @@ -FROM openjdk:18 +FROM docker.io/openjdk:18 WORKDIR /app From 0f89a253fb4a38874cdab14499a8a33a7dfbc2f4 Mon Sep 17 00:00:00 2001 From: Yannick Lamprecht Date: Sun, 1 May 2022 01:26:37 +0200 Subject: [PATCH 3/5] Separated persistent storage claim definition from database deployment --- docker/kubernetes/database-persistence.yaml | 10 ++++++++++ docker/kubernetes/database.yaml | 11 ----------- 2 files changed, 10 insertions(+), 11 deletions(-) create mode 100644 docker/kubernetes/database-persistence.yaml diff --git a/docker/kubernetes/database-persistence.yaml b/docker/kubernetes/database-persistence.yaml new file mode 100644 index 0000000..6008f45 --- /dev/null +++ b/docker/kubernetes/database-persistence.yaml @@ -0,0 +1,10 @@ +kind: PersistentVolumeClaim +apiVersion: v1 +metadata: + name: postgres-data +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 3Gi diff --git a/docker/kubernetes/database.yaml b/docker/kubernetes/database.yaml index 6f202de..0cf1899 100644 --- a/docker/kubernetes/database.yaml +++ b/docker/kubernetes/database.yaml @@ -40,14 +40,3 @@ spec: - name: postgres protocol: TCP port: 5432 ---- -kind: PersistentVolumeClaim -apiVersion: v1 -metadata: - name: postgres-data -spec: - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 3Gi From 756c82938d0cf98913ab5fe3f5be9ffcf1cfc6a3 Mon Sep 17 00:00:00 2001 From: Yannick Lamprecht Date: Sun, 1 May 2022 22:59:45 +0200 Subject: [PATCH 4/5] Added namespace to setup --- docker/kubernetes/database-persistence.yaml | 1 + docker/kubernetes/database.yaml | 2 ++ docker/kubernetes/game-jam-bot.yaml | 4 +++- docker/kubernetes/ingress.yaml | 2 ++ docker/kubernetes/kustomization.yaml | 1 + docker/kubernetes/namespace.yaml | 6 ++++++ 6 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 docker/kubernetes/namespace.yaml diff --git a/docker/kubernetes/database-persistence.yaml b/docker/kubernetes/database-persistence.yaml index 6008f45..da7238d 100644 --- a/docker/kubernetes/database-persistence.yaml +++ b/docker/kubernetes/database-persistence.yaml @@ -2,6 +2,7 @@ kind: PersistentVolumeClaim apiVersion: v1 metadata: name: postgres-data + namespace: plugin-jam-bot spec: accessModes: - ReadWriteOnce diff --git a/docker/kubernetes/database.yaml b/docker/kubernetes/database.yaml index 0cf1899..bd0a2e5 100644 --- a/docker/kubernetes/database.yaml +++ b/docker/kubernetes/database.yaml @@ -2,6 +2,7 @@ apiVersion: apps/v1 kind: Deployment metadata: name: database + namespace: plugin-jam-bot spec: template: metadata: @@ -33,6 +34,7 @@ apiVersion: v1 kind: Service metadata: name: database + namespace: plugin-jam-bot spec: selector: app: database diff --git a/docker/kubernetes/game-jam-bot.yaml b/docker/kubernetes/game-jam-bot.yaml index d8d27c1..95a85df 100644 --- a/docker/kubernetes/game-jam-bot.yaml +++ b/docker/kubernetes/game-jam-bot.yaml @@ -2,6 +2,7 @@ apiVersion: apps/v1 kind: Deployment metadata: name: bot + namespace: plugin-jam-bot spec: template: metadata: @@ -18,7 +19,7 @@ spec: initContainers: - name: init-myservice image: busybox:1.28 - command: [ 'sh', '-c', "until nslookup database.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done" ] + command: [ 'sh', '-c', "until nslookup database.plugin-jam-bot.svc.cluster.local; do echo waiting for myservice; sleep 2; done" ] selector: matchLabels: @@ -28,6 +29,7 @@ apiVersion: v1 kind: Service metadata: name: bot-01 + namespace: plugin-jam-bot spec: ports: - name: http diff --git a/docker/kubernetes/ingress.yaml b/docker/kubernetes/ingress.yaml index 3322155..e4ce3e6 100644 --- a/docker/kubernetes/ingress.yaml +++ b/docker/kubernetes/ingress.yaml @@ -2,6 +2,7 @@ apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: game-jam-bot-ingres + namespace: plugin-jam-bot annotations: kubernetes.io/ingress.class: traefik # Stripping the prefix and providing it as `X-Forwarded-Prefix` isn't supported by Javalin and the Swagger module @@ -23,6 +24,7 @@ apiVersion: traefik.containo.us/v1alpha1 kind: Middleware metadata: name: game-jam-strip-prefix + namespace: plugin-jam-bot spec: stripPrefix: prefixes: diff --git a/docker/kubernetes/kustomization.yaml b/docker/kubernetes/kustomization.yaml index 0f57bb1..6d2472c 100644 --- a/docker/kubernetes/kustomization.yaml +++ b/docker/kubernetes/kustomization.yaml @@ -2,6 +2,7 @@ kind: Kustomization apiVersion: kustomize.config.k8s.io/v1beta1 resources: + - namespace.yaml - ingress.yaml - database.yaml - game-jam-bot.yaml diff --git a/docker/kubernetes/namespace.yaml b/docker/kubernetes/namespace.yaml new file mode 100644 index 0000000..75d0f29 --- /dev/null +++ b/docker/kubernetes/namespace.yaml @@ -0,0 +1,6 @@ +kind: Namespace +apiVersion: v1 +metadata: + name: plugin-jam-bot + labels: + app: bot From 0f5ac6333c34082b877fba0f7f3f68f02836bccc Mon Sep 17 00:00:00 2001 From: Yannick Lamprecht Date: Mon, 2 May 2022 22:33:11 +0200 Subject: [PATCH 5/5] Approach for fetching users --- bot/src/main/java/de/chojo/gamejam/Bot.java | 7 +++- .../listener/ReadyStateChangeListener.java | 42 +++++++++++++++++++ docker/kubernetes/kustomization.yaml | 1 - 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 bot/src/main/java/de/chojo/gamejam/listener/ReadyStateChangeListener.java diff --git a/bot/src/main/java/de/chojo/gamejam/Bot.java b/bot/src/main/java/de/chojo/gamejam/Bot.java index 84f8316..fa94bef 100644 --- a/bot/src/main/java/de/chojo/gamejam/Bot.java +++ b/bot/src/main/java/de/chojo/gamejam/Bot.java @@ -17,6 +17,7 @@ import de.chojo.gamejam.data.GuildData; import de.chojo.gamejam.data.JamData; import de.chojo.gamejam.data.TeamData; +import de.chojo.gamejam.listener.ReadyStateChangeListener; import de.chojo.gamejam.util.LogNotify; import de.chojo.jdautil.command.SimpleCommand; import de.chojo.jdautil.command.dispatching.CommandHub; @@ -30,6 +31,7 @@ import de.chojo.sqlutil.updater.SqlType; import de.chojo.sqlutil.updater.SqlUpdater; import de.chojo.sqlutil.wrapper.QueryBuilderConfig; +import net.dv8tion.jda.api.hooks.AnnotatedEventManager; import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder; @@ -106,6 +108,8 @@ public void start() throws IOException, SQLException, LoginException { initBot(); + shardManager.addEventListener(new ReadyStateChangeListener(shardManager)); + buildLocale(); buildCommands(); @@ -142,7 +146,8 @@ private void initBot() throws LoginException { GatewayIntent.GUILD_MEMBERS, GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_MESSAGES) - .setMemberCachePolicy(MemberCachePolicy.DEFAULT) + .setMemberCachePolicy(MemberCachePolicy.ALL) + .setEventManagerProvider(id -> new AnnotatedEventManager()) .build(); RestAction.setDefaultFailure(throwable -> log.error("Unhandled exception occured: ", throwable)); } diff --git a/bot/src/main/java/de/chojo/gamejam/listener/ReadyStateChangeListener.java b/bot/src/main/java/de/chojo/gamejam/listener/ReadyStateChangeListener.java new file mode 100644 index 0000000..b126022 --- /dev/null +++ b/bot/src/main/java/de/chojo/gamejam/listener/ReadyStateChangeListener.java @@ -0,0 +1,42 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * + * Copyright (C) 2022 DevCord Team and Contributor + */ + +package de.chojo.gamejam.listener; + +import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.events.ReadyEvent; +import net.dv8tion.jda.api.events.ShutdownEvent; +import net.dv8tion.jda.api.hooks.SubscribeEvent; +import net.dv8tion.jda.api.sharding.ShardManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ReadyStateChangeListener { + + private final ShardManager shardManager; + private static final Logger log = LoggerFactory.getLogger(ReadyStateChangeListener.class); + + public ReadyStateChangeListener(ShardManager shardManager) { + this.shardManager = shardManager; + } + + @SubscribeEvent + public void finishedLoading(ReadyEvent readyEvent){ + log.warn("Available Guilds: {}",readyEvent.getGuildAvailableCount()); + shardManager.setActivity(Activity.playing("Plugin-Jam-Bot")); + for (Guild guild : shardManager.getGuilds()) { + guild.loadMembers(member -> { + log.warn("Loaded member \"{}\" of guild \"{}\"", member.getEffectiveName(), guild.getName()); + }); + } + } + + @SubscribeEvent + public void shutdown(ShutdownEvent shutdownEvent){ + shardManager.setActivity(null); + } +} diff --git a/docker/kubernetes/kustomization.yaml b/docker/kubernetes/kustomization.yaml index 6d2472c..0f57bb1 100644 --- a/docker/kubernetes/kustomization.yaml +++ b/docker/kubernetes/kustomization.yaml @@ -2,7 +2,6 @@ kind: Kustomization apiVersion: kustomize.config.k8s.io/v1beta1 resources: - - namespace.yaml - ingress.yaml - database.yaml - game-jam-bot.yaml