From c55d2836d7f4133b16e98738b74b3e5e359c6a9f Mon Sep 17 00:00:00 2001 From: "konstantina.galouni" Date: Fri, 1 Dec 2023 10:19:20 +0200 Subject: [PATCH] [irish-monitor-service | WIP | CHANGED]: Monitor Service to work as standalone and as library (classifier library) at the same time. 1. pom.xml: Added plugin "maven-jar-plugin" and an with library which creates a jar and excluded files from library setup. 2. UoaMonitorServiceApplication.java: Moved to package application and removed @EnableConfigurationProperties and @Import of other configurations (moved to UoaMonitorServiceConfiguration.java). 3. UoaMonitorServiceConfiguration.java: Added @EnableConfigurationProperties and @Import of configurations and @ComponentScan. 4. ServletInitializer.java: Moved to package application. 5. MonitorLibraryCheckDeployController.java: Added this copy of MonitorServiceCheckDeployController.java to be used only in library setup with @RequestMapping("/monitor-library"). --- pom.xml | 35 +++++++- .../UoaMonitorServiceConfiguration.java | 16 ++++ .../{ => application}/ServletInitializer.java | 2 +- .../UoaMonitorServiceApplication.java | 12 +-- .../MonitorLibraryCheckDeployController.java | 80 +++++++++++++++++++ 5 files changed, 132 insertions(+), 13 deletions(-) rename src/main/java/eu/dnetlib/uoamonitorservice/{ => application}/ServletInitializer.java (90%) rename src/main/java/eu/dnetlib/uoamonitorservice/{ => application}/UoaMonitorServiceApplication.java (51%) create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java diff --git a/pom.xml b/pom.xml index 1cdbd42..d66b489 100644 --- a/pom.xml +++ b/pom.xml @@ -59,14 +59,26 @@ spring-boot-maven-plugin ${spring-boot-version} - eu.dnetlib.uoamonitorservice.UoaMonitorServiceApplication + eu.dnetlib.uoamonitorservice.application.UoaMonitorServiceApplication true + + + + + default-war + package repackage + + + + + + @@ -78,6 +90,27 @@ false + + org.apache.maven.plugins + maven-jar-plugin + + + library + package + + jar + + + library + + **/eu/dnetlib/uoamonitorservice/application/** + **/eu/dnetlib/uoamonitorservice/configuration/SwaggerConfig.class + **/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.class + + + + + uoa-monitor-service diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/UoaMonitorServiceConfiguration.java b/src/main/java/eu/dnetlib/uoamonitorservice/UoaMonitorServiceConfiguration.java index 163784d..97b57df 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/UoaMonitorServiceConfiguration.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/UoaMonitorServiceConfiguration.java @@ -1,13 +1,29 @@ package eu.dnetlib.uoamonitorservice; +import eu.dnetlib.uoaadmintoolslibrary.UoaAdminToolsLibraryConfiguration; +import eu.dnetlib.uoaauthorizationlibrary.configuration.AuthorizationConfiguration; +import eu.dnetlib.uoamonitorservice.configuration.GlobalVars; +import eu.dnetlib.uoamonitorservice.configuration.properties.APIProperties; +import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig; +import eu.dnetlib.uoanotificationservice.configuration.NotificationConfiguration; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration +@EnableConfigurationProperties({ MongoConfig.class, GlobalVars.class, APIProperties.class}) +@ComponentScan(basePackages = { "eu.dnetlib.uoamonitorservice" }) +@Import({ + AuthorizationConfiguration.class, + UoaAdminToolsLibraryConfiguration.class, + NotificationConfiguration.class +}) public class UoaMonitorServiceConfiguration extends WebMvcConfigurerAdapter { private final Logger log = LogManager.getLogger(this.getClass()); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/ServletInitializer.java b/src/main/java/eu/dnetlib/uoamonitorservice/application/ServletInitializer.java similarity index 90% rename from src/main/java/eu/dnetlib/uoamonitorservice/ServletInitializer.java rename to src/main/java/eu/dnetlib/uoamonitorservice/application/ServletInitializer.java index 2f0b319..6df2b1f 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/ServletInitializer.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/application/ServletInitializer.java @@ -1,4 +1,4 @@ -package eu.dnetlib.uoamonitorservice; +package eu.dnetlib.uoamonitorservice.application; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/UoaMonitorServiceApplication.java b/src/main/java/eu/dnetlib/uoamonitorservice/application/UoaMonitorServiceApplication.java similarity index 51% rename from src/main/java/eu/dnetlib/uoamonitorservice/UoaMonitorServiceApplication.java rename to src/main/java/eu/dnetlib/uoamonitorservice/application/UoaMonitorServiceApplication.java index 14cc038..136450e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/UoaMonitorServiceApplication.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/application/UoaMonitorServiceApplication.java @@ -1,15 +1,7 @@ -package eu.dnetlib.uoamonitorservice; +package eu.dnetlib.uoamonitorservice.application; -import eu.dnetlib.uoaadmintoolslibrary.UoaAdminToolsLibraryConfiguration; -import eu.dnetlib.uoaauthorizationlibrary.configuration.AuthorizationConfiguration; -import eu.dnetlib.uoamonitorservice.configuration.GlobalVars; -import eu.dnetlib.uoamonitorservice.configuration.properties.APIProperties; -import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig; -import eu.dnetlib.uoanotificationservice.configuration.NotificationConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySources; //uoahelptexts @@ -21,8 +13,6 @@ import org.springframework.context.annotation.PropertySources; @PropertySource("classpath:monitorservice.properties"), @PropertySource(value = "classpath:dnet-override.properties", ignoreResourceNotFound = true) }) -@EnableConfigurationProperties({ MongoConfig.class, GlobalVars.class, APIProperties.class}) -@Import({AuthorizationConfiguration.class, UoaAdminToolsLibraryConfiguration.class, NotificationConfiguration.class}) public class UoaMonitorServiceApplication { public static void main(String[] args) { SpringApplication.run(UoaMonitorServiceApplication.class, args); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java new file mode 100644 index 0000000..85b5380 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorLibraryCheckDeployController.java @@ -0,0 +1,80 @@ +package eu.dnetlib.uoamonitorservice.controllers; + +import com.mongodb.BasicDBObject; +import com.mongodb.CommandResult; +import com.mongodb.DBObject; +import eu.dnetlib.uoamonitorservice.application.UoaMonitorServiceApplication; +import eu.dnetlib.uoamonitorservice.configuration.GlobalVars; +import eu.dnetlib.uoamonitorservice.configuration.mongo.MongoConnection; +import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.context.annotation.Conditional; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; +import java.util.Map; + +@RestController +@CrossOrigin(origins = "*") +@RequestMapping("/monitor-library") +//@ConditionalOnBean(UoaMonitorServiceApplication.class) +public class MonitorLibraryCheckDeployController { + private final Logger log = LogManager.getLogger(this.getClass()); + + @Autowired + private MongoConnection mongoConnection; + + @Autowired + private MongoConfig mongoConfig; + + @Autowired + private GlobalVars globalVars; + + @RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET) + public String hello() { + log.debug("Hello from uoa-monitor-service library!"); + return "Hello from uoa-monitor-service library!"; + } + +// @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") + @RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET) + public Map checkEverything() { + Map response = new HashMap<>(); + + MongoTemplate mt = mongoConnection.getMongoTemplate(); + DBObject ping = new BasicDBObject("ping", "1"); + try { + CommandResult answer = mt.getDb().command(ping); + response.put("Mongo try: error", answer.getErrorMessage()); + } catch (Exception e) { + response.put("Mongo catch: error", e.getMessage()); + } + + response.put("monitorservice.mongodb.database", mongoConfig.getDatabase()); + response.put("monitorservice.mongodb.host", mongoConfig.getHost()); + response.put("monitorservice.mongodb.port", mongoConfig.getPort()+""); + response.put("monitorservice.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]"); + response.put("monitorservice.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]"); +// response.put("Define also", "monitorservice.mongodb.username, monitorservice.mongodb.password"); + + if(globalVars.date != null) { + response.put("Date of deploy", globalVars.date.toString()); + } + if(globalVars.getBuildDate() != null) { + response.put("Date of build", globalVars.getBuildDate()); + } + if(globalVars.getVersion() != null) { + response.put("Version", globalVars.getVersion()); + } + + return response; + } +}