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
|
|
|
|
2025-04-25 15:35:12 +02:00
|
|
|
import lombok.RequiredArgsConstructor;
|
2024-12-17 10:26:18 +01:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2025-02-23 13:36:21 +00:00
|
|
|
|
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")
|
2025-04-25 15:35:12 +02:00
|
|
|
@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
|
|
|
|
2024-12-24 09:52:53 +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: /";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|