added inject

This commit is contained in:
Francesco Mangiacrapa 2024-10-17 15:10:28 +02:00
parent b00d80ab9a
commit 4ac33c1f5c
4 changed files with 105 additions and 32 deletions

View File

@ -167,6 +167,12 @@
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
</dependency>
<!-- Plugins related tests -->
<dependency>

View File

@ -0,0 +1,41 @@
package org.gcube.application.geoportal.service.health;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.Liveness;
import org.eclipse.microprofile.health.Readiness;
import lombok.extern.slf4j.Slf4j;
@ApplicationScoped
@Readiness
@Liveness
@Slf4j
public class GeoportalHealthCheck implements HealthCheck {
private static final String SERVICE_NAME = "geooportal-service";
// @GET
// @Path("")
// @Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
// public Response check() {
// log.info("Check called");
// HealthCheckResponse hcr = call();
// return Response.ok().entity(hcr).build();
// }
@Override
public HealthCheckResponse call() {
log.info(GeoportalHealthCheck.class.getSimpleName() + " call");
return HealthCheckResponse.named(SERVICE_NAME).state(true).build();
}
}

View File

@ -1,10 +1,7 @@
package org.gcube.application.geoportal.service.rest;
package org.gcube.application.geoportal.service.health;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.eclipse.microprofile.health.HealthCheck;
@ -16,49 +13,32 @@ import org.gcube.application.cms.implementations.ImplementationProvider;
import org.gcube.application.geoportal.common.model.configuration.MongoConnection;
import org.gcube.application.geoportal.service.model.internal.db.Mongo;
import org.gcube.common.scope.api.ScopeProvider;
import org.glassfish.jersey.process.internal.RequestScoped;
import com.mongodb.client.MongoIterable;
import lombok.extern.slf4j.Slf4j;
@RequestScoped
@ApplicationScoped
@Readiness
@Path("/health")
@Liveness
@Slf4j
public class GeoportalHealthCheck implements HealthCheck {
public class MongoHealthCheck implements HealthCheck {
private static final String SERVICE_NAME = "geooportal-service";
private String context;
private static final String SERVICE_NAME = "mongo";
@GET
@Path("")
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
public Response check() {
log.info("Check called");
HealthCheckResponse hcr = call();
return Response.ok().entity(hcr).build();
}
@Liveness
@Override
public HealthCheckResponse call() {
return HealthCheckResponse.named(SERVICE_NAME).state(true).build();
return checkMongo(context);
}
@GET
@Path("/mongo")
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
public Response checkDatabase(@QueryParam("context") String context) {
log.info("checkDatabase called in the context {}", context);
if (context == null)
return Response.status(400).entity("'context' paramater (e.g. '/gcube/devsec/devVRE' is required").build();
return Response.ok().entity(checkMongo(context)).build();
public MongoHealthCheck context(String context) {
this.context = context;
return this;
}
private HealthCheckResponse checkMongo(String context) {
HealthCheckResponseBuilder buildHCRProvider = HealthCheckResponse.named("mongo");
HealthCheckResponseBuilder buildHCRProvider = HealthCheckResponse.named(SERVICE_NAME);
Mongo mongo = null;
try {
ScopeProvider.instance.set(context);

View File

@ -0,0 +1,46 @@
package org.gcube.application.geoportal.service.rest;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.gcube.application.geoportal.service.health.GeoportalHealthCheck;
import org.gcube.application.geoportal.service.health.MongoHealthCheck;
import lombok.extern.slf4j.Slf4j;
@Path("/health")
@Slf4j
public class GeoportalHealth {
@Inject
GeoportalHealthCheck geoportalHC;
@Inject
MongoHealthCheck mongoHC;
@GET
@Path("")
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
public HealthCheckResponse serviceCheck() {
log.info("serviceCheck called");
return geoportalHC.call();
}
@GET
@Path("/mongo")
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
public HealthCheckResponse databaseCheck(@QueryParam("context") String context) {
log.info("databaseCheck called in the context {}", context);
if (context == null)
return HealthCheckResponse.named("databaseCheck")
.withData("context", "is required parameter (e.g. context=/gcube/devsec/devVRE)").down().build();
return mongoHC.context(context).call();
}
}