forked from LondheShubham153/java-quotes-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
71 lines (57 loc) · 2.42 KB
/
Copy pathMain.java
File metadata and controls
71 lines (57 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
public class Main {
private static List<String> quotes;
public static void main(String[] args) throws IOException {
// Load quotes from an external file
quotes = loadQuotesFromFile("quotes.txt");
if (quotes.isEmpty()) {
System.err.println("No quotes found in the file. Please ensure 'quotes.txt' has content.");
return;
}
// Create an HTTP server listening on port 8000
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
// Define a context for the root path ("/")
server.createContext("/", exchange -> {
// Get a random quote
String quote = getRandomQuote();
// Create a JSON response
String jsonResponse = String.format("{\"quote\": \"%s\"}", quote);
// Convert the JSON response to bytes
byte[] responseBytes = jsonResponse.getBytes(StandardCharsets.UTF_8);
// Set the response headers
exchange.getResponseHeaders().set("Content-Type", "application/json");
exchange.sendResponseHeaders(200, responseBytes.length);
// Write the JSON response to the output stream
try (OutputStream os = exchange.getResponseBody()) {
os.write(responseBytes);
}
});
// Start the server
server.start();
System.out.println("Server is running on port 8000...");
}
// Helper method to get a random quote
private static String getRandomQuote() {
Random random = new Random();
return quotes.get(random.nextInt(quotes.size()));
}
// Helper method to load quotes from an external file
private static List<String> loadQuotesFromFile(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
return reader.lines().collect(Collectors.toList());
} catch (IOException e) {
System.err.println("Error reading quotes file: " + e.getMessage());
return List.of();
}
}
}