diff --git a/Dockerfile-lite b/Dockerfile-lite index 1d12a545..8cd84c8d 100644 --- a/Dockerfile-lite +++ b/Dockerfile-lite @@ -12,9 +12,7 @@ COPY build/libs/*.jar app.jar EXPOSE 8080 # Set environment variables -ENV APP_HOME_NAME="Stirling PDF" -#ENV APP_HOME_DESCRIPTION="Personal PDF Website!" -#ENV APP_NAVBAR_NAME="Stirling PDF" +ENV GROUPS_TO_REMOVE=LibreOffice # Run the application RUN chmod +x /scripts/init.sh diff --git a/Dockerfile-ultralite b/Dockerfile-ultralite new file mode 100644 index 00000000..2c87caf7 --- /dev/null +++ b/Dockerfile-ultralite @@ -0,0 +1,14 @@ +# Build jbig2enc in a separate stage +FROM openjdk:17-jdk-slim + +# Copy the application JAR file +COPY build/libs/*.jar app.jar + +# Expose the application port +EXPOSE 8080 + +# Set environment variables +ENV GROUPS_TO_REMOVE=LibreOffice,CLI + +# Run the application +CMD ["java", "-jar", "/app.jar"] diff --git a/DockerfileBase b/DockerfileBase index fc5399af..876b5a10 100644 --- a/DockerfileBase +++ b/DockerfileBase @@ -26,11 +26,11 @@ RUN git clone https://github.com/agl/jbig2enc && \ FROM openjdk:17-jdk-slim AS base RUN apt-get update && \ apt-get install -y --no-install-recommends \ - libreoffice-core \ + libreoffice-core-nogui \ libreoffice-common \ - libreoffice-writer \ - libreoffice-calc \ - libreoffice-impress \ + libreoffice-writer-nogui \ + libreoffice-calc-nogui \ + libreoffice-impress-nogui \ python3-uno \ python3-pip \ unoconv \ diff --git a/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java b/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java index b2a372e5..a81a9130 100644 --- a/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java +++ b/src/main/java/stirling/software/SPDF/config/EndpointConfiguration.java @@ -7,11 +7,12 @@ import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; - +import org.slf4j.Logger; @Service public class EndpointConfiguration { - + private static final Logger logger = LoggerFactory.getLogger(EndpointConfiguration.class); private Map endpointStatuses = new ConcurrentHashMap<>(); private Map> endpointGroups = new ConcurrentHashMap<>(); @@ -25,6 +26,7 @@ public class EndpointConfiguration { } public void disableEndpoint(String endpoint) { + logger.info("Disabling {}", endpoint); endpointStatuses.put(endpoint, false); } @@ -162,6 +164,7 @@ public class EndpointConfiguration { addEndpointToGroup("Javascript", "pdf-organizer"); addEndpointToGroup("Javascript", "sign"); addEndpointToGroup("Javascript", "compare"); + } private void processEnvironmentConfigs() { diff --git a/src/main/java/stirling/software/SPDF/config/EndpointInterceptor.java b/src/main/java/stirling/software/SPDF/config/EndpointInterceptor.java index 4e560148..5befd511 100644 --- a/src/main/java/stirling/software/SPDF/config/EndpointInterceptor.java +++ b/src/main/java/stirling/software/SPDF/config/EndpointInterceptor.java @@ -17,12 +17,10 @@ public class EndpointInterceptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String requestURI = request.getRequestURI(); - System.out.println("trying " + requestURI); if (!endpointConfiguration.isEndpointEnabled(requestURI)) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "This endpoint is disabled"); return false; } return true; } -} - +} \ No newline at end of file diff --git a/src/main/java/stirling/software/SPDF/config/MetricsConfig.java b/src/main/java/stirling/software/SPDF/config/MetricsConfig.java index c80acba4..928a3868 100644 --- a/src/main/java/stirling/software/SPDF/config/MetricsConfig.java +++ b/src/main/java/stirling/software/SPDF/config/MetricsConfig.java @@ -18,7 +18,7 @@ public class MetricsConfig { return new MeterFilter() { @Override public MeterFilterReply accept(Meter.Id id) { - if (id.getName().equals("http.requests") || id.getName().equals("health")) { + if (id.getName().equals("http.requests")) { return MeterFilterReply.NEUTRAL; } return MeterFilterReply.DENY; diff --git a/src/main/java/stirling/software/SPDF/config/MetricsFilter.java b/src/main/java/stirling/software/SPDF/config/MetricsFilter.java index 57b9272c..2407b649 100644 --- a/src/main/java/stirling/software/SPDF/config/MetricsFilter.java +++ b/src/main/java/stirling/software/SPDF/config/MetricsFilter.java @@ -33,7 +33,7 @@ public class MetricsFilter extends OncePerRequestFilter { String uri = request.getRequestURI(); // Ignore static resources - if (!(uri.startsWith("/css") || uri.startsWith("/js") || uri.startsWith("/images") || uri.endsWith(".ico") || uri.endsWith(".svg")|| uri.endsWith(".js"))) { + if (!(uri.startsWith("/js") || uri.startsWith("/images") || uri.endsWith(".ico") || uri.endsWith(".css") || uri.endsWith(".svg")|| uri.endsWith(".js") || uri.contains("swagger") || uri.startsWith("/api"))) { Counter counter = Counter.builder("http.requests") .tag("uri", uri) .tag("method", request.getMethod()) diff --git a/src/main/java/stirling/software/SPDF/controller/web/MetricsController.java b/src/main/java/stirling/software/SPDF/controller/web/MetricsController.java new file mode 100644 index 00000000..97d6db93 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/controller/web/MetricsController.java @@ -0,0 +1,80 @@ +package stirling.software.SPDF.controller.web; +import io.micrometer.core.instrument.Counter; +import io.micrometer.core.instrument.Meter; +import io.micrometer.core.instrument.MeterRegistry; +import io.swagger.v3.oas.annotations.Operation; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/v1") +public class MetricsController { + + private final MeterRegistry meterRegistry; + + public MetricsController(MeterRegistry meterRegistry) { + this.meterRegistry = meterRegistry; + } + + @GetMapping("/status") + @Operation(summary = "Application status and version", + description = "This endpoint returns the status of the application and its version number.") + public Map getStatus() { + Map status = new HashMap<>(); + status.put("status", "UP"); + status.put("version", getClass().getPackage().getImplementationVersion()); + return status; + } + + @GetMapping("/loads") + @Operation(summary = "GET request count", + description = "This endpoint returns the total count of GET requests or the count of GET requests for a specific endpoint.") + public Double getPageLoads(@RequestParam Optional endpoint) { + try { + double count = 0.0; + + for (Meter meter : meterRegistry.getMeters()) { + if (meter.getId().getName().equals("http.requests")) { + String method = meter.getId().getTag("method"); + if (method != null && method.equals("GET")) { + if (meter instanceof Counter) { + count += ((Counter) meter).count(); + } + } + } + } + + return count; + } catch (Exception e) { + return -1.0; + } + } + + @GetMapping("/requests") + @Operation(summary = "POST request count", + description = "This endpoint returns the total count of POST requests or the count of POST requests for a specific endpoint.") + public Double getTotalRequests(@RequestParam Optional endpoint) { + try { + Counter counter; + if (endpoint.isPresent()) { + counter = meterRegistry.get("http.requests") + .tags("method", "POST", "uri", endpoint.get()).counter(); + } else { + counter = meterRegistry.get("http.requests") + .tags("method", "POST").counter(); + } + return counter.count(); + } catch (Exception e) { + return -1.0; + } + + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 2e577acb..6130f481 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -22,5 +22,4 @@ server.servlet.context-path=${APP_ROOT_PATH:/} spring.devtools.restart.enabled=true spring.devtools.livereload.enabled=true -spring.thymeleaf.encoding=UTF-8 - +spring.thymeleaf.encoding=UTF-8 \ No newline at end of file diff --git a/src/main/resources/templates/fragments/navbar.html b/src/main/resources/templates/fragments/navbar.html index 6c2b7b70..cdebcfca 100644 --- a/src/main/resources/templates/fragments/navbar.html +++ b/src/main/resources/templates/fragments/navbar.html @@ -151,11 +151,11 @@ function compareVersions(version1, version2) { @@ -167,16 +167,16 @@ function compareVersions(version1, version2) {