/
OverallStatusRoute.java
executable file
·124 lines (110 loc) · 5.73 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
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.gateway.endpoints.routes.status;
import static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.completeWithFuture;
import static akka.http.javadsl.server.Directives.get;
import static akka.http.javadsl.server.Directives.path;
import static akka.http.javadsl.server.Directives.pathEndOrSingleSlash;
import static akka.http.javadsl.server.Directives.rawPathPrefix;
import static akka.http.javadsl.server.Directives.route;
import static org.eclipse.ditto.services.gateway.endpoints.directives.CustomPathMatchers.mergeDoubleSlashes;
import static org.eclipse.ditto.services.gateway.endpoints.directives.DevOpsBasicAuthenticationDirective.REALM_DEVOPS;
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;
import org.eclipse.ditto.services.gateway.endpoints.config.DevOpsConfig;
import org.eclipse.ditto.services.gateway.endpoints.directives.DevOpsBasicAuthenticationDirective;
import org.eclipse.ditto.services.gateway.health.StatusAndHealthProvider;
import org.eclipse.ditto.services.utils.health.cluster.ClusterStatus;
import akka.http.javadsl.model.ContentTypes;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.server.Route;
/**
* Builder for creating Akka HTTP routes for {@code /status}.
*/
public final class OverallStatusRoute {
/**
* 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 DevOpsConfig devOpsConfig;
/**
* 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 devOpsConfig the configuration settings of the Gateway service's DevOps endpoint.
*/
public OverallStatusRoute(final Supplier<ClusterStatus> clusterStateSupplier,
final StatusAndHealthProvider statusHealthProvider, final DevOpsConfig devOpsConfig) {
this.clusterStateSupplier = clusterStateSupplier;
this.statusHealthProvider = statusHealthProvider;
this.devOpsConfig = devOpsConfig;
}
/**
* Builds the {@code /status} route.
*
* @return the {@code /status} route.
*/
public Route buildOverallStatusRoute() {
return rawPathPrefix(mergeDoubleSlashes().concat(PATH_OVERALL), () -> { // /overall/*
final DevOpsBasicAuthenticationDirective devOpsBasicAuthenticationDirective =
DevOpsBasicAuthenticationDirective.getInstance(devOpsConfig);
return devOpsBasicAuthenticationDirective.authenticateDevOpsBasic(REALM_DEVOPS, get(() -> // GET
// /overall/status
// /overall/status/health
// /overall/status/cluster
rawPathPrefix(mergeDoubleSlashes().concat(PATH_STATUS), () ->
route(
// /status
pathEndOrSingleSlash(
() -> completeWithFuture(createOverallStatusResponse())),
// /status/health
path(PATH_HEALTH,
() -> completeWithFuture(createOverallHealthResponse())),
path(PATH_CLUSTER, () -> complete( // /status/cluster
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());
}
});
}
}