Skip to content

Commit 11e0dc4

Browse files
Merge remote-tracking branch 'origin/dev' into task/gen-javadoc
2 parents 59947b2 + 9e9075f commit 11e0dc4

16 files changed

+599
-458
lines changed

build.gradle.kts

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group = "de.chojo"
10-
version = "1.15.1"
10+
version = "1.15.2"
1111

1212
repositories {
1313
mavenCentral()
@@ -25,12 +25,18 @@ spotless {
2525

2626
dependencies {
2727
//discord
28-
implementation("de.chojo", "cjda-util", "2.9.8+jda-5.0.0") {
28+
implementation("de.chojo", "cjda-util", "2.10.3+jda-5.1.0") {
2929
exclude(group = "club.minnced", module = "opus-java")
3030
}
3131

32+
val openapi = "6.4.0"
33+
34+
annotationProcessor("io.javalin.community.openapi:openapi-annotation-processor:$openapi")
35+
implementation("io.javalin.community.openapi:javalin-openapi-plugin:$openapi") // for /openapi route with JSON scheme
36+
implementation("io.javalin.community.openapi:javalin-swagger-plugin:$openapi") // for Swagger UI
37+
3238
// database
33-
implementation("org.postgresql", "postgresql", "42.7.4")
39+
implementation("org.postgresql", "postgresql", "42.7.5")
3440
implementation(libs.bundles.sadu)
3541

3642
// Logging
@@ -42,7 +48,7 @@ dependencies {
4248
implementation("org.knowm.xchart", "xchart", "3.8.8")
4349

4450
// unit testing
45-
testImplementation(platform("org.junit:junit-bom:5.11.3"))
51+
testImplementation(platform("org.junit:junit-bom:5.11.4"))
4652
testImplementation("org.junit.jupiter", "junit-jupiter")
4753
testImplementation("org.knowm.xchart", "xchart", "3.8.8")
4854
}

gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

gradlew

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ done
8686
# shellcheck disable=SC2034
8787
APP_BASE_NAME=${0##*/}
8888
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
89-
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
90-
' "$PWD" ) || exit
89+
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
9190

9291
# Use the maximum available, or set MAX_FD != -1 to use that value.
9392
MAX_FD=maximum

settings.gradle.kts

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ dependencyResolutionManagement {
2323
bundle("log4j", listOf("slf4j-api", "log4j-core", "log4j-slf4j2", "log4j-jsontemplate"))
2424

2525
// plugins
26-
plugin("spotless", "com.diffplug.spotless").version("6.25.0")
26+
plugin("spotless", "com.diffplug.spotless").version("7.0.2")
2727
plugin("shadow", "com.gradleup.shadow").version("8.3.5")
28-
2928
}
3029
}
3130
}

src/main/java/de/chojo/repbot/core/Web.java

+39-14
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import de.chojo.jdautil.botlist.BotlistService;
99
import de.chojo.repbot.config.Configuration;
1010
import de.chojo.repbot.web.Api;
11+
import de.chojo.repbot.web.error.ApiException;
1112
import io.javalin.Javalin;
12-
import io.javalin.plugin.openapi.OpenApiOptions;
13-
import io.javalin.plugin.openapi.OpenApiPlugin;
14-
import io.javalin.plugin.openapi.ui.ReDocOptions;
15-
import io.javalin.plugin.openapi.ui.SwaggerOptions;
16-
import io.javalin.plugin.openapi.utils.OpenApiVersionUtil;
17-
import io.swagger.v3.oas.models.info.License;
13+
import io.javalin.openapi.OpenApiLicense;
14+
import io.javalin.openapi.plugin.OpenApiPlugin;
15+
import io.javalin.openapi.plugin.OpenApiPluginConfiguration;
16+
import io.javalin.openapi.plugin.swagger.SwaggerConfiguration;
17+
import io.javalin.openapi.plugin.swagger.SwaggerPlugin;
1818
import net.dv8tion.jda.api.entities.User;
1919
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
2020
import net.dv8tion.jda.api.requests.ErrorResponse;
@@ -80,19 +80,44 @@ public void init() {
8080
private void initApi() {
8181
var api = configuration.api();
8282

83-
var info = new io.swagger.v3.oas.models.info.Info().version("1.0").title("Reputation Bot API")
84-
.description("Documentation for the Reputation Bot API")
85-
.license(new License().name("GNU Affero General Public License v3.0")
86-
.url("https://github.com/RainbowDashLabs/reputation-bot/blob/master/LICENSE.md"));
83+
/*
8784
var options = new OpenApiOptions(info)
8885
.path("/json-docs")
8986
.reDoc(new ReDocOptions("/redoc")) // endpoint for redoc
9087
.swagger(new SwaggerOptions("/docs").title("Reputation Bot API"));
91-
OpenApiVersionUtil.INSTANCE.setLogWarnings(false);
88+
OpenApiVersionUtil.INSTANCE.setLogWarnings(false);*/
9289

93-
javalin = Javalin.create(config -> config.registerPlugin(new OpenApiPlugin(options)))
94-
.start(api.host(), api.port());
95-
new Api(javalin, data.metrics()).init();
90+
javalin = Javalin.create(config -> {
91+
config.registerPlugin(new OpenApiPlugin(this::configureOpenApi));
92+
config.registerPlugin(new SwaggerPlugin(this::configureSwagger));
93+
config.router.apiBuilder(() -> new Api(data.metrics()).init());
94+
})
95+
.start(api.host(), api.port());
96+
javalin.exception(ApiException.class, (err, ctx) -> ctx.result(err.getMessage()).status(err.status()));
97+
}
98+
99+
private void configureSwagger(SwaggerConfiguration swaggerConfiguration) {
100+
swaggerConfiguration.setDocumentationPath("/docs");
101+
swaggerConfiguration.setUiPath("/swagger-ui");
102+
}
103+
104+
private void configureOpenApi(OpenApiPluginConfiguration config) {
105+
config.withDocumentationPath("/docs")
106+
.withDefinitionConfiguration((version, definition) -> {
107+
definition.withInfo(info -> {
108+
info.setTitle("Reputation Bot API");
109+
info.setVersion("1.0");
110+
info.setDescription("Documentation for the Reputation Bot API");
111+
info.setLicense(new OpenApiLicense()
112+
.name("GNU Affero General Public License v3.0")
113+
.url("https://github.com/RainbowDashLabs/reputation-bot/blob/master/LICENSE.md")
114+
);
115+
});
116+
definition.withServer(openApiServer -> {
117+
openApiServer.setUrl("https://repbot.rainbowdashlabs.de");
118+
openApiServer.setDescription("Main server");
119+
});
120+
});
96121
}
97122

98123
/**

src/main/java/de/chojo/repbot/util/Messages.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ private static void handleMark(RestAction<Void> action) {
8888
ErrorResponse.UNKNOWN_MESSAGE,
8989
ErrorResponse.TOO_MANY_REACTIONS,
9090
ErrorResponse.REACTION_BLOCKED,
91-
ErrorResponse.UNKNOWN_CHANNEL));
91+
ErrorResponse.UNKNOWN_CHANNEL,
92+
ErrorResponse.THREAD_LOCKED));
9293
}
9394
}

src/main/java/de/chojo/repbot/web/Api.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import de.chojo.repbot.web.error.ApiException;
1010
import de.chojo.repbot.web.routes.v1.MetricsRoute;
1111
import io.javalin.Javalin;
12+
import io.javalin.apibuilder.ApiBuilder;
13+
import io.javalin.apibuilder.EndpointGroup;
14+
import io.javalin.router.Endpoint;
1215
import org.slf4j.Logger;
1316

1417
import static io.javalin.apibuilder.ApiBuilder.before;
@@ -20,29 +23,22 @@
2023
*/
2124
public class Api {
2225
private static final Logger log = getLogger(Api.class);
23-
private final Javalin javalin;
2426
private final MetricsRoute metricsRoute;
2527

2628
/**
2729
* Constructs an Api instance with the specified Javalin instance and Metrics provider.
2830
*
29-
* @param javalin the Javalin instance to be used
3031
* @param metrics the Metrics provider
3132
*/
32-
public Api(Javalin javalin, Metrics metrics) {
33-
this.javalin = javalin;
33+
public Api(Metrics metrics) {
3434
metricsRoute = new MetricsRoute(metrics);
3535
}
3636

3737
/**
3838
* Initializes the API by setting up exception handling and routes.
3939
*/
4040
public void init() {
41-
javalin.exception(ApiException.class, (err, ctx) -> ctx.result(err.getMessage()).status(err.status()));
42-
javalin.routes(() -> {
43-
before(ctx -> log.debug("Received request on {}.", ctx.path()));
44-
45-
path("v1", () -> path("metrics", metricsRoute::buildRoutes));
46-
});
41+
before(ctx -> log.debug("Received request on {}.", ctx.path()));
42+
path("v1", () -> path("metrics", metricsRoute::buildRoutes));
4743
}
4844
}

src/main/java/de/chojo/repbot/web/error/ApiException.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*/
66
package de.chojo.repbot.web.error;
77

8-
import io.javalin.http.HttpCode;
8+
9+
import io.javalin.http.HttpStatus;
910

1011
/**
1112
* Custom exception class for API errors.
@@ -14,15 +15,15 @@ public class ApiException extends RuntimeException {
1415
/**
1516
* The HTTP status code associated with this exception.
1617
*/
17-
private final HttpCode status;
18+
private final HttpStatus status;
1819

1920
/**
2021
* Constructs a new ApiException with the specified HTTP status code and message.
2122
*
2223
* @param status the HTTP status code
2324
* @param message the detail message
2425
*/
25-
public ApiException(HttpCode status, String message) {
26+
public ApiException(HttpStatus status, String message) {
2627
super(message);
2728
this.status = status;
2829
}
@@ -32,7 +33,7 @@ public ApiException(HttpCode status, String message) {
3233
*
3334
* @return the HTTP status code
3435
*/
35-
public HttpCode status() {
36+
public HttpStatus status() {
3637
return status;
3738
}
3839
}

src/main/java/de/chojo/repbot/web/routes/v1/MetricsHolder.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
import de.chojo.repbot.web.routes.v1.metrics.MetricCache;
1212
import io.javalin.http.Context;
1313
import io.javalin.http.Handler;
14-
import io.javalin.http.HttpCode;
15-
import org.eclipse.jetty.http.HttpStatus;
14+
import io.javalin.http.HttpStatus;
1615
import org.slf4j.Logger;
1716

1817
import static org.slf4j.LoggerFactory.getLogger;
@@ -56,7 +55,7 @@ protected void writeImage(Context ctx, byte[] png) {
5655
ctx.header("X-Content-Type-Options", "nosniff");
5756
ctx.contentType("image/png");
5857

59-
ctx.result(png).status(HttpStatus.OK_200);
58+
ctx.result(png).status(HttpStatus.OK);
6059
}
6160

6261
/**
@@ -74,7 +73,7 @@ protected int offset(Context context, int max) {
7473
assertSize(offset, 0, max);
7574
return offset;
7675
} catch (NumberFormatException e) {
77-
throw new ApiException(HttpCode.BAD_REQUEST, "Offset is not a number, Got: " + param);
76+
throw new ApiException(HttpStatus.BAD_REQUEST, "Offset is not a number, Got: " + param);
7877
}
7978
}
8079

@@ -93,7 +92,7 @@ protected int count(Context context, int max) {
9392
assertSize(offset, 2, max);
9493
return offset;
9594
} catch (NumberFormatException e) {
96-
throw new ApiException(HttpCode.BAD_REQUEST, "Count is not a number, Got: " + param);
95+
throw new ApiException(HttpStatus.BAD_REQUEST, "Count is not a number, Got: " + param);
9796
}
9897
}
9998

@@ -107,10 +106,10 @@ protected int count(Context context, int max) {
107106
*/
108107
private void assertSize(int value, int min, int max) {
109108
if (value < min) {
110-
throw new ApiException(HttpCode.BAD_REQUEST, String.format("Value %s is too small. Min: %s", value, min));
109+
throw new ApiException(HttpStatus.BAD_REQUEST, String.format("Value %s is too small. Min: %s", value, min));
111110
}
112111
if (value > max) {
113-
throw new ApiException(HttpCode.BAD_REQUEST, String.format("Value %s is too large. Max: %s", value, max));
112+
throw new ApiException(HttpStatus.BAD_REQUEST, String.format("Value %s is too large. Max: %s", value, max));
114113
}
115114
}
116115

0 commit comments

Comments
 (0)