-
Notifications
You must be signed in to change notification settings - Fork 214
/
OverallStatusRoute.java
executable file
·113 lines (100 loc) · 4.97 KB
/
OverallStatusRoute.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.gateway.service.endpoints.routes.status;
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;
import org.eclipse.ditto.gateway.service.endpoints.directives.auth.DevOpsOAuth2AuthenticationDirective;
import org.eclipse.ditto.gateway.service.endpoints.directives.auth.DevopsAuthenticationDirective;
import org.eclipse.ditto.gateway.service.health.StatusAndHealthProvider;
import org.eclipse.ditto.internal.utils.health.cluster.ClusterStatus;
import org.apache.pekko.http.javadsl.model.ContentTypes;
import org.apache.pekko.http.javadsl.model.HttpResponse;
import org.apache.pekko.http.javadsl.model.StatusCodes;
import org.apache.pekko.http.javadsl.server.PathMatchers;
import org.apache.pekko.http.javadsl.server.Route;
import org.apache.pekko.http.javadsl.server.directives.RouteDirectives;
/**
* Builder for creating Akka HTTP routes for {@code /status}.
*/
public final class OverallStatusRoute extends RouteDirectives {
/**
* Public endpoint of overall status.
*/
public static final String PATH_OVERALL = "overall";
static final String PATH_STATUS = "status";
static final String PATH_HEALTH = "health";
static final String PATH_CLUSTER = "cluster";
private final Supplier<ClusterStatus> clusterStateSupplier;
private final StatusAndHealthProvider statusHealthProvider;
private final DevopsAuthenticationDirective devOpsAuthenticationDirective;
/**
* Constructs the {@code /status} route builder.
*
* @param clusterStateSupplier the supplier to get the cluster state.
* @param statusHealthProvider the provider for retrieving health status of the cluster.
* @param devOpsAuthenticationDirective the authentication handler for the Devops directive.
*/
public OverallStatusRoute(final Supplier<ClusterStatus> clusterStateSupplier,
final StatusAndHealthProvider statusHealthProvider,
final DevopsAuthenticationDirective devOpsAuthenticationDirective) {
this.clusterStateSupplier = clusterStateSupplier;
this.statusHealthProvider = statusHealthProvider;
this.devOpsAuthenticationDirective = devOpsAuthenticationDirective;
}
/**
* Builds the {@code /status} route.
*
* @return the {@code /status} route.
*/
public Route buildOverallStatusRoute() {
return rawPathPrefix(PathMatchers.slash().concat(PATH_OVERALL), () -> {// /overall/*
return devOpsAuthenticationDirective.authenticateDevOps(DevOpsOAuth2AuthenticationDirective.REALM_STATUS, get(() -> // GET
// /overall/status
// /overall/status/health
// /overall/status/cluster
rawPathPrefix(PathMatchers.slash().concat(PATH_STATUS), () -> concat(
// /status
pathEndOrSingleSlash(() -> completeWithFuture(createOverallStatusResponse())),
// /status/health
path(PATH_HEALTH, () -> completeWithFuture(createOverallHealthResponse())),
// /status/cluster
path(PATH_CLUSTER, () -> complete(
HttpResponse.create().withStatus(StatusCodes.OK)
.withEntity(ContentTypes.APPLICATION_JSON,
clusterStateSupplier.get().toJson().toString()))
)
))
));
});
}
private CompletionStage<HttpResponse> createOverallStatusResponse() {
return statusHealthProvider.retrieveStatus()
.thenApply(status -> HttpResponse.create()
.withStatus(StatusCodes.OK)
.withEntity(ContentTypes.APPLICATION_JSON, status.toString()));
}
private CompletionStage<HttpResponse> createOverallHealthResponse() {
return statusHealthProvider.retrieveHealth()
.thenApply(overallHealth -> {
if (overallHealth.isHealthy()) {
return HttpResponse.create()
.withStatus(StatusCodes.OK)
.withEntity(ContentTypes.APPLICATION_JSON, overallHealth.toJsonString());
} else {
return HttpResponse.create()
.withStatus(StatusCodes.SERVICE_UNAVAILABLE)
.withEntity(ContentTypes.APPLICATION_JSON, overallHealth.toJsonString());
}
});
}
}