2023-06-20 21:16:07 +02:00
package eu.openaire.pdf_aggregation_statistics ;
import eu.openaire.pdf_aggregation_statistics.util.UriBuilder ;
2023-06-22 00:54:47 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2023-06-20 21:16:07 +02:00
import org.springframework.boot.CommandLineRunner ;
import org.springframework.boot.SpringApplication ;
import org.springframework.boot.autoconfigure.SpringBootApplication ;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext ;
2023-06-22 00:54:47 +02:00
import org.springframework.context.ConfigurableApplicationContext ;
2023-06-20 21:16:07 +02:00
import org.springframework.context.annotation.Bean ;
import org.springframework.core.env.Environment ;
import org.springframework.scheduling.annotation.EnableScheduling ;
import org.springframework.web.cors.CorsConfiguration ;
import org.springframework.web.cors.CorsConfigurationSource ;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource ;
2023-06-22 00:54:47 +02:00
import javax.annotation.PreDestroy ;
2023-06-20 21:16:07 +02:00
import java.util.Arrays ;
import java.util.Collections ;
@SpringBootApplication
@EnableScheduling
public class PdfAggregationStatisticsApplication {
2023-06-22 00:54:47 +02:00
private static final Logger logger = LoggerFactory . getLogger ( PdfAggregationStatisticsApplication . class ) ;
private static ConfigurableApplicationContext context ;
2023-06-20 21:16:07 +02:00
public static void main ( String [ ] args ) {
2023-06-22 00:54:47 +02:00
context = SpringApplication . run ( PdfAggregationStatisticsApplication . class , args ) ;
2023-06-20 21:16:07 +02:00
}
@Bean
public CorsConfigurationSource corsConfigurationSource ( ) {
CorsConfiguration configuration = new CorsConfiguration ( ) ;
configuration . setAllowedOrigins ( Collections . singletonList ( " * " ) ) ;
configuration . setAllowedMethods ( Collections . singletonList ( " GET " ) ) ;
configuration . setAllowedHeaders ( Arrays . asList ( " authorization " , " content-type " , " x-auth-token " ) ) ;
configuration . setExposedHeaders ( Collections . singletonList ( " x-auth-token " ) ) ;
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ( ) ;
source . registerCorsConfiguration ( " /** " , configuration ) ;
return source ;
}
2023-06-22 00:54:47 +02:00
public static void gentleAppShutdown ( )
{
int exitCode = 0 ;
try {
exitCode = SpringApplication . exit ( context , ( ) - > 0 ) ; // The "PreDestroy" method will be called. (the "context" will be closed automatically (I checked it))
} catch ( IllegalArgumentException iae ) {
logger . error ( iae . getMessage ( ) ) ; // This will say "Context must not be null", in case the "gentleAppShutdown()" was called too early in the app's lifetime. But it's ok.
}
System . exit ( exitCode ) ;
}
@PreDestroy
public void preDestroy ( ) {
// TODO - Add any future shut-down logic here (e.g. shutting down threads).
logger . info ( " Exiting.. " ) ;
}
2023-06-21 11:26:18 +02:00
2023-06-20 21:16:07 +02:00
@Bean
public CommandLineRunner setServerBaseUrl ( Environment environment , ServletWebServerApplicationContext webServerAppCtxt )
{
return args - > new UriBuilder ( environment , webServerAppCtxt ) ;
}
}