1 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
2 | import com.sun.net.httpserver.HttpExchange;
|
---|
3 | import com.sun.net.httpserver.HttpHandler;
|
---|
4 | import com.sun.net.httpserver.HttpServer;
|
---|
5 | import org.openqa.selenium.internal.Debug;
|
---|
6 |
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.OutputStream;
|
---|
10 | import java.net.InetSocketAddress;
|
---|
11 | import java.nio.file.Files;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 |
|
---|
15 | public 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 | }
|
---|