Skip to content

Commit

Permalink
added context sensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
yannicklamprecht committed Apr 30, 2022
1 parent caac557 commit 8353909
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
10 changes: 4 additions & 6 deletions bot/src/main/java/de/chojo/gamejam/api/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -54,16 +50,18 @@ 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")){
var withoutContext = ctx.path().replace(ctx.contextPath(), "");
if(withoutContext.startsWith("/swagger") || withoutContext.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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions conf/dev/temp.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"poolSize" : 5
},
"api" : {
"contextPath": "game-jam/",
"host" : "0.0.0.0",
"port" : 8888,
"token" : "letmein"
Expand Down
4 changes: 3 additions & 1 deletion docker/kubernetes/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8353909

Please sign in to comment.