|
8 | 8 | import de.chojo.jdautil.botlist.BotlistService;
|
9 | 9 | import de.chojo.repbot.config.Configuration;
|
10 | 10 | import de.chojo.repbot.web.Api;
|
| 11 | +import de.chojo.repbot.web.error.ApiException; |
11 | 12 | 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; |
18 | 18 | import net.dv8tion.jda.api.entities.User;
|
19 | 19 | import net.dv8tion.jda.api.exceptions.ErrorResponseException;
|
20 | 20 | import net.dv8tion.jda.api.requests.ErrorResponse;
|
@@ -51,19 +51,44 @@ public void init() {
|
51 | 51 | private void initApi() {
|
52 | 52 | var api = configuration.api();
|
53 | 53 |
|
54 |
| - var info = new io.swagger.v3.oas.models.info.Info().version("1.0").title("Reputation Bot API") |
55 |
| - .description("Documentation for the Reputation Bot API") |
56 |
| - .license(new License().name("GNU Affero General Public License v3.0") |
57 |
| - .url("https://github.com/RainbowDashLabs/reputation-bot/blob/master/LICENSE.md")); |
| 54 | + /* |
58 | 55 | var options = new OpenApiOptions(info)
|
59 | 56 | .path("/json-docs")
|
60 | 57 | .reDoc(new ReDocOptions("/redoc")) // endpoint for redoc
|
61 | 58 | .swagger(new SwaggerOptions("/docs").title("Reputation Bot API"));
|
62 |
| - OpenApiVersionUtil.INSTANCE.setLogWarnings(false); |
| 59 | + OpenApiVersionUtil.INSTANCE.setLogWarnings(false);*/ |
63 | 60 |
|
64 |
| - javalin = Javalin.create(config -> config.registerPlugin(new OpenApiPlugin(options))) |
65 |
| - .start(api.host(), api.port()); |
66 |
| - new Api(javalin, data.metrics()).init(); |
| 61 | + javalin = Javalin.create(config -> { |
| 62 | + config.registerPlugin(new OpenApiPlugin(this::configureOpenApi)); |
| 63 | + config.registerPlugin(new SwaggerPlugin(this::configureSwagger)); |
| 64 | + config.router.apiBuilder(() -> new Api(data.metrics()).init()); |
| 65 | + }) |
| 66 | + .start(api.host(), api.port()); |
| 67 | + javalin.exception(ApiException.class, (err, ctx) -> ctx.result(err.getMessage()).status(err.status())); |
| 68 | + } |
| 69 | + |
| 70 | + private void configureSwagger(SwaggerConfiguration swaggerConfiguration) { |
| 71 | + swaggerConfiguration.setDocumentationPath("/docs"); |
| 72 | + swaggerConfiguration.setUiPath("/swagger-ui"); |
| 73 | + } |
| 74 | + |
| 75 | + private void configureOpenApi(OpenApiPluginConfiguration config) { |
| 76 | + config.withDocumentationPath("/docs") |
| 77 | + .withDefinitionConfiguration((version, definition) -> { |
| 78 | + definition.withInfo(info -> { |
| 79 | + info.setTitle("Reputation Bot API"); |
| 80 | + info.setVersion("1.0"); |
| 81 | + info.setDescription("Documentation for the Reputation Bot API"); |
| 82 | + info.setLicense(new OpenApiLicense() |
| 83 | + .name("GNU Affero General Public License v3.0") |
| 84 | + .url("https://github.com/RainbowDashLabs/reputation-bot/blob/master/LICENSE.md") |
| 85 | + ); |
| 86 | + }); |
| 87 | + definition.withServer(openApiServer -> { |
| 88 | + openApiServer.setUrl("https://repbot.rainbowdashlabs.de"); |
| 89 | + openApiServer.setDescription("Main server"); |
| 90 | + }); |
| 91 | + }); |
67 | 92 | }
|
68 | 93 |
|
69 | 94 | private void initBotList() {
|
|
0 commit comments