fixes
This commit is contained in:
parent
abcd9ff04d
commit
63a4549a8d
@ -1,12 +1,10 @@
|
||||
package cz.kamma.czb;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URL;
|
||||
@ -16,6 +14,12 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
public class BasketballServer {
|
||||
|
||||
private static JsonNode cachedData;
|
||||
@ -30,7 +34,7 @@ public class BasketballServer {
|
||||
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
|
||||
server.createContext("/api/matches", new ApiHandler());
|
||||
server.createContext("/", new WebHandler("src/main/resources/index.html"));
|
||||
server.createContext("/", new WebHandler());
|
||||
server.setExecutor(Executors.newFixedThreadPool(4));
|
||||
server.start();
|
||||
|
||||
@ -90,21 +94,62 @@ public class BasketballServer {
|
||||
|
||||
// --- Web UI handler ---
|
||||
static class WebHandler implements HttpHandler {
|
||||
private final String htmlFile;
|
||||
|
||||
public WebHandler(String htmlFile) { this.htmlFile = htmlFile; }
|
||||
public WebHandler() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
File file = new File(htmlFile);
|
||||
if (!file.exists()) { exchange.sendResponseHeaders(404, -1); return; }
|
||||
|
||||
byte[] bytes = java.nio.file.Files.readAllBytes(file.toPath());
|
||||
exchange.getResponseHeaders().set("Content-Type", "text/html; charset=UTF-8");
|
||||
exchange.sendResponseHeaders(200, bytes.length);
|
||||
OutputStream os = exchange.getResponseBody();
|
||||
os.write(bytes);
|
||||
os.close();
|
||||
try {
|
||||
String path = exchange.getRequestURI().getPath();
|
||||
if ("/".equals(path) || path.isEmpty() || path.equals("/index.html")) {
|
||||
String html = readResource("/index.html");
|
||||
if (html == null) {
|
||||
sendError(exchange, 404, "index.html not found in resources");
|
||||
return;
|
||||
}
|
||||
exchange.getResponseHeaders().set("Content-Type", "text/html; charset=utf-8");
|
||||
byte[] bytes = html.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, bytes.length);
|
||||
try (OutputStream os = exchange.getResponseBody()) {
|
||||
os.write(bytes);
|
||||
os.flush();
|
||||
}
|
||||
} else {
|
||||
sendError(exchange, 404, "Not found");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
sendError(exchange, 500, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ======= Chyba =======
|
||||
private static void sendError(HttpExchange exchange, int code, String message) throws IOException {
|
||||
exchange.getResponseHeaders().set("Content-Type", "text/plain; charset=utf-8");
|
||||
byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(code, bytes.length);
|
||||
try (OutputStream os = exchange.getResponseBody()) {
|
||||
os.write(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
private static String readResource(String resourcePath) {
|
||||
try (InputStream is = BasketballServer.class.getResourceAsStream(resourcePath)) {
|
||||
if (is == null)
|
||||
return null;
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
sb.append(line).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user