Skip to content

Commit 8353909

Browse files
added context sensitivity
1 parent caac557 commit 8353909

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

bot/src/main/java/de/chojo/gamejam/api/Api.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@
1313
import de.chojo.gamejam.data.JamData;
1414
import de.chojo.gamejam.data.TeamData;
1515
import io.javalin.Javalin;
16-
import io.javalin.apibuilder.ApiBuilder;
1716
import io.javalin.http.Context;
18-
import io.javalin.http.HttpCode;
1917
import io.javalin.plugin.openapi.OpenApiOptions;
2018
import io.javalin.plugin.openapi.OpenApiPlugin;
2119
import io.javalin.plugin.openapi.ui.ReDocOptions;
2220
import io.javalin.plugin.openapi.ui.SwaggerOptions;
23-
import io.swagger.models.Info;
24-
import net.dv8tion.jda.api.requests.ErrorResponse;
2521
import net.dv8tion.jda.api.sharding.ShardManager;
2622
import org.slf4j.Logger;
2723

@@ -54,16 +50,18 @@ public Api(Configuration configuration, ShardManager shardManager, TeamData team
5450

5551
private void build() {
5652
app = Javalin.create(config -> {
53+
config.contextPath = configuration.api().contextPath();
5754
config.registerPlugin(getConfiguredOpenApiPlugin());
5855
config.accessManager((handler, ctx, routeRoles) -> {
59-
if(ctx.path().startsWith("/swagger") || ctx.path().startsWith("/redoc")){
56+
var withoutContext = ctx.path().replace(ctx.contextPath(), "");
57+
if(withoutContext.startsWith("/swagger") || withoutContext.startsWith("/redoc")){
6058
handler.handle(ctx);
6159
return;
6260
}
6361

6462
var token = ctx.req.getHeader("authorization");
6563
if (token == null) {
66-
ctx.status(HttpServletResponse.SC_UNAUTHORIZED).result("Please provde a valid token in the authorization header.");
64+
ctx.status(HttpServletResponse.SC_UNAUTHORIZED).result("Please provide a valid token in the authorization header.");
6765
} else if (!token.equals(configuration.api().token())) {
6866
ctx.status(HttpServletResponse.SC_UNAUTHORIZED).result("Unauthorized");
6967
} else {

bot/src/main/java/de/chojo/gamejam/configuration/elements/Api.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88

99
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
1010
public class Api {
11+
12+
private String contextPath = "/";
1113
private String host = "localhost";
1214
private int port = 8888;
1315
private String token = "letmein";
1416

17+
public String contextPath() {
18+
return contextPath;
19+
}
20+
1521
public String host() {
1622
return host;
1723
}

conf/dev/temp.config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"poolSize" : 5
1414
},
1515
"api" : {
16+
"contextPath": "game-jam/",
1617
"host" : "0.0.0.0",
1718
"port" : 8888,
1819
"token" : "letmein"

docker/kubernetes/ingress.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ metadata:
44
name: game-jam-bot-ingres
55
annotations:
66
kubernetes.io/ingress.class: traefik
7-
traefik.ingress.kubernetes.io/router.middlewares: default-game-jam-strip-prefix@kubernetescrd
7+
# Stripping the prefix and providing it as `X-Forwarded-Prefix` isn't supported by Javalin and the Swagger module
8+
# Thus we deactivate the middleware for now
9+
# traefik.ingress.kubernetes.io/router.middlewares: default-game-jam-strip-prefix@kubernetescrd
810
spec:
911
rules:
1012
- http:

0 commit comments

Comments
 (0)