Files
Stirling-PDF/src/main/java/stirling/software/SPDF/controller/web/HomeWebController.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
2.5 KiB
Java
Raw Normal View History

2025-05-09 20:01:09 +01:00
package stirling.software.SPDF.controller.api;
2023-06-25 09:16:32 +01:00
2024-01-04 23:04:15 +00:00
import java.io.IOException;
2024-01-09 22:39:21 +00:00
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
2024-01-04 23:04:15 +00:00
import java.util.List;
import java.util.Map;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
2023-06-25 09:16:32 +01:00
import org.springframework.http.MediaType;
2025-05-09 20:01:09 +01:00
import org.springframework.web.bind.annotation.*;
2023-06-25 09:16:32 +01:00
2024-01-04 23:04:15 +00:00
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
2023-06-25 09:16:32 +01:00
import io.swagger.v3.oas.annotations.Hidden;
2023-12-30 19:11:27 +00:00
import lombok.RequiredArgsConstructor;
2024-12-17 10:26:18 +01:00
import lombok.extern.slf4j.Slf4j;
2023-08-26 22:33:23 +01:00
import stirling.software.SPDF.model.ApplicationProperties;
2024-01-03 23:01:33 +00:00
import stirling.software.SPDF.model.Dependency;
2025-05-09 20:01:09 +01:00
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
2025-05-09 20:01:09 +01:00
@Slf4j
2023-06-25 09:16:32 +01:00
public class HomeWebController {
2023-12-30 19:11:27 +00:00
private final ApplicationProperties applicationProperties;
2025-05-09 20:01:09 +01:00
/** Returns the visibility settings for things like surveys. */
@GetMapping("/env")
public Map<String, Object> getEnvironmentFlags() {
String showSurvey = System.getenv("SHOW_SURVEY");
boolean showSurveyValue = showSurvey == null || "true".equalsIgnoreCase(showSurvey);
return Map.of("showSurvey", showSurveyValue);
2023-06-25 09:16:32 +01:00
}
2024-01-04 23:04:15 +00:00
2025-05-09 20:01:09 +01:00
/** Returns the third-party licenses as a JSON list. */
2024-01-03 23:01:33 +00:00
@GetMapping("/licenses")
2025-05-09 20:01:09 +01:00
public List<Dependency> getLicenses() {
2024-01-03 23:01:33 +00:00
Resource resource = new ClassPathResource("static/3rdPartyLicenses.json");
2025-05-09 20:01:09 +01:00
try (InputStream is = resource.getInputStream()) {
2024-01-09 22:39:21 +00:00
String json = new String(is.readAllBytes(), StandardCharsets.UTF_8);
2024-01-03 23:01:33 +00:00
ObjectMapper mapper = new ObjectMapper();
2025-05-09 20:01:09 +01:00
Map<String, List<Dependency>> data = mapper.readValue(json, new TypeReference<>() {});
return data.get("dependencies");
2024-01-03 23:01:33 +00:00
} catch (IOException e) {
2025-05-09 20:01:09 +01:00
log.error("Failed to read licenses JSON", e);
throw new RuntimeException("Could not load license data", e);
2024-01-03 23:01:33 +00:00
}
2025-01-30 18:55:33 +00:00
}
2025-05-09 20:01:09 +01:00
/** Dynamic generation of robots.txt based on configuration. */
2023-06-25 09:16:32 +01:00
@GetMapping(value = "/robots.txt", produces = MediaType.TEXT_PLAIN_VALUE)
@Hidden
public String getRobotsTxt() {
2023-08-26 22:33:23 +01:00
Boolean allowGoogle = applicationProperties.getSystem().getGooglevisibility();
if (Boolean.TRUE.equals(allowGoogle)) {
2023-06-25 09:16:32 +01:00
return "User-agent: Googlebot\nAllow: /\n\nUser-agent: *\nAllow: /";
} else {
return "User-agent: Googlebot\nDisallow: /\n\nUser-agent: *\nDisallow: /";
}
}
}