source: backend/GlobeGuru-backend/src/main/java/ScraperServer.java@ d4d8f61

Last change on this file since d4d8f61 was d4d8f61, checked in by Kikac324 <kristijanzafirovski26@…>, 7 months ago

init

  • Property mode set to 100644
File size: 3.4 KB
Line 
1import com.fasterxml.jackson.databind.ObjectMapper;
2import com.sun.net.httpserver.HttpExchange;
3import com.sun.net.httpserver.HttpHandler;
4import com.sun.net.httpserver.HttpServer;
5import org.openqa.selenium.internal.Debug;
6
7import java.io.File;
8import java.io.IOException;
9import java.io.OutputStream;
10import java.net.InetSocketAddress;
11import java.nio.file.Files;
12import java.util.List;
13import java.util.Map;
14
15public class ScraperServer {
16
17 public static void main(String[] args) throws IOException {
18 // Start HTTP server
19 HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
20 server.createContext("/", new FrontendHandler());
21 server.createContext("/submit", new FormHandler());
22 server.start();
23
24 System.out.println("Server started on port 8000");
25 }
26
27 static class FrontendHandler implements HttpHandler {
28 public void handle(HttpExchange exchange) throws IOException {
29 String uri = exchange.getRequestURI().toString();
30 File file = new File("frontend" + uri);
31 if (!file.exists() || !file.isFile()) {
32 file = new File("frontend/index.html"); // Default to index.html
33 }
34
35 exchange.sendResponseHeaders(200, file.length());
36 OutputStream outputStream = exchange.getResponseBody();
37 Files.copy(file.toPath(), outputStream);
38 outputStream.close();
39 }
40 }
41
42 static class FormHandler implements HttpHandler {
43 @Override
44 public void handle(HttpExchange exchange) throws IOException {
45 if (exchange.getRequestMethod().equalsIgnoreCase("POST")) {
46 // Read form data from request body
47 String requestBody = new String(exchange.getRequestBody().readAllBytes());
48 System.out.println("Form data: " + requestBody);
49
50 ObjectMapper mapper = new ObjectMapper();
51 Map<String, String> formData = mapper.readValue(requestBody, Map.class);
52 String destination = formData.get("destination");
53 String departureDate = formData.get("departureDate");
54 int numberOfPeople = Integer.parseInt(formData.get("numberOfPeople"));
55
56 // Start Scraper thread with form data
57 Scraper scraper = new Scraper(destination, departureDate, numberOfPeople);
58 scraper.start();
59
60 // Wait for the scraper threads to finish
61 try {
62 scraper.join(); // Ensure the scraper thread completes
63 } catch (InterruptedException e) {
64 e.printStackTrace();
65 }
66
67 List<Option> options = scraper.getOptions();
68 //System.out.println("Scraper finished with options: " + options);
69
70 // Respond with JSON data
71 String optionsJson = mapper.writeValueAsString(options);
72 exchange.getResponseHeaders().set("Content-Type", "application/json");
73 exchange.sendResponseHeaders(200, optionsJson.getBytes().length);
74 OutputStream outputStream = exchange.getResponseBody();
75 outputStream.write(optionsJson.getBytes());
76 outputStream.close();
77 } else {
78 // Method not allowed
79 exchange.sendResponseHeaders(405, -1);
80 exchange.close();
81 }
82 }
83 }
84
85}
Note: See TracBrowser for help on using the repository browser.