diff --git a/docker-compose-prod.yml b/docker-compose-prod.yml index 5d2ef53b..4f1c7073 100644 --- a/docker-compose-prod.yml +++ b/docker-compose-prod.yml @@ -42,6 +42,7 @@ services: - POSTGRES_USER=${POSTGRES_USER} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - POSTGRES_DB=${POSTGRES_DB} + - SCREENSHOT_SERVICE_URL=http://screenshot:8080/ volumes: - pgdata:/var/lib/postgresql/data ports: diff --git a/docker-compose-staging.yml b/docker-compose-staging.yml index f190e68d..19c0ea76 100644 --- a/docker-compose-staging.yml +++ b/docker-compose-staging.yml @@ -28,6 +28,7 @@ services: - POSTGRES_USER=${POSTGRES_USER} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - POSTGRES_DB=${POSTGRES_DB} + - SCREENSHOT_SERVICE_URL=http://screenshot:8080/ volumes: - pgdata:/var/lib/postgresql/data ports: diff --git a/docker-compose.yml b/docker-compose.yml index f7bc49dc..38840e27 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,6 +13,7 @@ services: - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/findfirst - SPRING_PROFILES_ACTIVE=dev - SPRING_MAIL_HOST=mail + - SCREENSHOT_SERVICE_URL=http://screenshot:8080/ volumes: - screenshots-data:/app/screenshots screenshot: diff --git a/screenshot/app/src/main/java/dev/findfirst/screenshot/controller/ScreenshotController.java b/screenshot/app/src/main/java/dev/findfirst/screenshot/controller/ScreenshotController.java index 4ba22220..757e293d 100644 --- a/screenshot/app/src/main/java/dev/findfirst/screenshot/controller/ScreenshotController.java +++ b/screenshot/app/src/main/java/dev/findfirst/screenshot/controller/ScreenshotController.java @@ -39,7 +39,7 @@ public String takeScreenshot(@RequestParam String url) { String cleanUrl = url.replace("/", "_"); Path filePath = Path.of(screenshotSaveLoc, cleanUrl + System.currentTimeMillis() + ".png"); page.screenshot(new Page.ScreenshotOptions().setPath(filePath)); - return "Screenshot saved to: " + filePath; + return filePath.toString(); } } catch (PlaywrightException e) { // Handle Playwright specific exceptions diff --git a/server/src/main/java/dev/findfirst/core/service/ScreenshotManager.java b/server/src/main/java/dev/findfirst/core/service/ScreenshotManager.java new file mode 100644 index 00000000..68b47440 --- /dev/null +++ b/server/src/main/java/dev/findfirst/core/service/ScreenshotManager.java @@ -0,0 +1,21 @@ +package dev.findfirst.core.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +@Service +public class ScreenshotManager { + + @Value("${screenshot.service.url:http://localhost:8080}") + private String screenshotServiceUrl; + + @Autowired private RestTemplate rest; + + public String getScreenshot(String reqUrl) { + String url = screenshotServiceUrl + "/screenshot?url=" + reqUrl; + return rest.getForObject(url, String.class); + } + +} diff --git a/server/src/test/java/dev/findfirst/core/service/ScreenshotManagerTest.java b/server/src/test/java/dev/findfirst/core/service/ScreenshotManagerTest.java new file mode 100644 index 00000000..fca6291f --- /dev/null +++ b/server/src/test/java/dev/findfirst/core/service/ScreenshotManagerTest.java @@ -0,0 +1,41 @@ +package dev.findfirst.core.service; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.client.RestTemplate; + +@ExtendWith(MockitoExtension.class) +public class ScreenshotManagerTest { + + @Mock + private RestTemplate restTemplate; + + @InjectMocks + private ScreenshotManager screenshotManager = new ScreenshotManager(); + + + @BeforeEach + public void setUp() { + ReflectionTestUtils.setField(screenshotManager, "screenshotServiceUrl", "http://localhost:8080"); + } + + @Test + public void returnMockedPath() { + var expectedUrl = "https:__findfirst.dev"; + Mockito + .when(restTemplate.getForObject( + "http://localhost:8080/screenshot?url=https://findfirst.dev", String.class)) + .thenReturn("https:__findfirst.dev"); + var pathUrl = screenshotManager.getScreenshot("https://findfirst.dev"); + + Assertions.assertEquals(pathUrl, expectedUrl); + } + +}