Skip to content

Commit

Permalink
Add health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Oct 22, 2023
1 parent c51f89c commit f3b6307
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ license {

repositories {
mavenCentral()
maven { url 'https://nexus.lucko.me/repository/maven-snapshots/' }
}

dependencies {
compileOnly 'org.slf4j:slf4j-api:1.7.36'
compileOnly 'net.luckperms:api:5.4'
compileOnly 'net.luckperms:api:5.5-20231022.200451-2'
implementation 'io.javalin:javalin:4.6.4'
implementation 'io.javalin:javalin-openapi:4.6.4'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import me.lucko.luckperms.extension.rest.util.SwaggerUi;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.messaging.MessagingService;
import net.luckperms.api.platform.Health;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -115,6 +116,11 @@ private void setupErrorHandlers(Javalin app) {
private void setupRoutes(Javalin app, LuckPerms luckPerms) {
app.get("/", ctx -> ctx.redirect("/docs/swagger-ui"));

app.get("health", ctx -> {
Health health = luckPerms.runHealthCheck();
ctx.status(health.isHealthy() ? HttpCode.OK : HttpCode.SERVICE_UNAVAILABLE).json(health);
});

MessagingService messagingService = luckPerms.getMessagingService().orElse(StubMessagingService.INSTANCE);

UserController userController = new UserController(luckPerms.getUserManager(), luckPerms.getTrackManager(), messagingService, this.objectMapper);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.luckperms.extension.rest.bind;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import net.luckperms.api.platform.Health;

import java.io.IOException;
import java.util.Map;

public class HealthSerializer extends JsonSerializer<Health> {

@Override
public void serialize(Health value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writePOJO(Model.from(value));
}

record Model(boolean healthy, Map<String, Object> details) {
static Model from(Health health) {
return new Model(
health.isHealthy(),
health.getDetails()
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import me.lucko.luckperms.extension.rest.bind.ContextSetSerializer;
import me.lucko.luckperms.extension.rest.bind.DemotionResultSerializer;
import me.lucko.luckperms.extension.rest.bind.GroupSerializer;
import me.lucko.luckperms.extension.rest.bind.HealthSerializer;
import me.lucko.luckperms.extension.rest.bind.MetadataSerializer;
import me.lucko.luckperms.extension.rest.bind.NodeDeserializer;
import me.lucko.luckperms.extension.rest.bind.NodeSerializer;
Expand All @@ -46,6 +47,7 @@
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User;
import net.luckperms.api.node.Node;
import net.luckperms.api.platform.Health;
import net.luckperms.api.query.QueryOptions;
import net.luckperms.api.track.DemotionResult;
import net.luckperms.api.track.PromotionResult;
Expand All @@ -64,6 +66,7 @@ public CustomObjectMapper() {
module.addSerializer(ContextSet.class, new ContextSetSerializer());
module.addSerializer(DemotionResult.class, new DemotionResultSerializer());
module.addSerializer(Group.class, new GroupSerializer());
module.addSerializer(Health.class, new HealthSerializer());
module.addSerializer(CachedMetaData.class, new MetadataSerializer());
module.addDeserializer(Node.class, new NodeDeserializer());
module.addSerializer(Node.class, new NodeSerializer());
Expand Down
49 changes: 49 additions & 0 deletions src/main/resources/luckperms-openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ tags:
description: API methods for LuckPerms groups.
- name: Actions
description: API methods for LuckPerms actions.
- name: Misc
description: Miscellaneous API methods.
paths:
/user:
get:
Expand Down Expand Up @@ -1128,6 +1130,39 @@ paths:
description: Submit a new action to the action logger.
tags:
- Actions
/health:
get:
summary: Get the current health status of the app
operationId: get-health
responses:
'200':
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/Health'
examples:
example-1:
value:
health: true
details:
storageConnected: true
storagePing: 15
'503':
description: Service Unavailable
content:
application/json:
schema:
$ref: '#/components/schemas/Health'
examples:
example-1:
value:
health: false
details:
reason: storage is disconnected
description: Returns the current health status of the app
tags:
- Misc
components:
schemas:
Node:
Expand Down Expand Up @@ -1574,6 +1609,20 @@ components:
- replace_existing_if_duration_longer
- none
default: 'none'
Health:
title: Health
type: object
description: The health status of the app
properties:
health:
type: boolean
description: if the app is healthy
details:
type: object
description: extra information about the healthcheck result
required:
- health
- details
securitySchemes:
apikey:
type: http
Expand Down

0 comments on commit f3b6307

Please sign in to comment.