chnaged file to memory
This commit is contained in:
parent
7868580c00
commit
9c59bd0d9d
@ -16,7 +16,7 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
private static final Gson GSON = new Gson();
|
private static final Gson GSON = new Gson();
|
||||||
private static final String DATA_FILE = "stats.json";
|
private static final List<DartsGame> GAMES_CACHE = Collections.synchronizedList(new ArrayList<>());
|
||||||
private static final long MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5MB
|
private static final long MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5MB
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
@ -26,6 +26,7 @@ public class App {
|
|||||||
server.createContext("/", new StaticHandler());
|
server.createContext("/", new StaticHandler());
|
||||||
server.createContext("/api/stats", new StatsHandler());
|
server.createContext("/api/stats", new StatsHandler());
|
||||||
server.createContext("/api/upload", new UploadHandler());
|
server.createContext("/api/upload", new UploadHandler());
|
||||||
|
server.createContext("/api/clear", new ClearHandler());
|
||||||
|
|
||||||
server.setExecutor(null);
|
server.setExecutor(null);
|
||||||
System.out.println("Server started on port " + port);
|
System.out.println("Server started on port " + port);
|
||||||
@ -103,7 +104,8 @@ public class App {
|
|||||||
List<RawGame> rawGames = GSON.fromJson(body, new TypeToken<List<RawGame>>(){}.getType());
|
List<RawGame> rawGames = GSON.fromJson(body, new TypeToken<List<RawGame>>(){}.getType());
|
||||||
List<DartsGame> convertedGames = convertRawToGames(rawGames);
|
List<DartsGame> convertedGames = convertRawToGames(rawGames);
|
||||||
|
|
||||||
Files.writeString(Paths.get(DATA_FILE), GSON.toJson(convertedGames));
|
GAMES_CACHE.clear();
|
||||||
|
GAMES_CACHE.addAll(convertedGames);
|
||||||
|
|
||||||
String response = "Upload successful";
|
String response = "Upload successful";
|
||||||
byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8);
|
byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8);
|
||||||
@ -181,8 +183,7 @@ public class App {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<DartsGame> games = loadGames();
|
StatsResponse response = aggregateStats(new ArrayList<>(GAMES_CACHE));
|
||||||
StatsResponse response = aggregateStats(games);
|
|
||||||
|
|
||||||
String json = GSON.toJson(response);
|
String json = GSON.toJson(response);
|
||||||
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
|
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
|
||||||
@ -194,19 +195,6 @@ public class App {
|
|||||||
os.close();
|
os.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<DartsGame> loadGames() {
|
|
||||||
try {
|
|
||||||
if (!Files.exists(Paths.get(DATA_FILE))) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
String content = Files.readString(Paths.get(DATA_FILE));
|
|
||||||
return GSON.fromJson(content, new TypeToken<List<DartsGame>>(){}.getType());
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private StatsResponse aggregateStats(List<DartsGame> games) {
|
private StatsResponse aggregateStats(List<DartsGame> games) {
|
||||||
Map<String, List<DartsGame>> byPlayer = games.stream()
|
Map<String, List<DartsGame>> byPlayer = games.stream()
|
||||||
.collect(Collectors.groupingBy(DartsGame::getPlayer));
|
.collect(Collectors.groupingBy(DartsGame::getPlayer));
|
||||||
@ -240,4 +228,22 @@ public class App {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class ClearHandler implements HttpHandler {
|
||||||
|
@Override
|
||||||
|
public void handle(HttpExchange exchange) throws IOException {
|
||||||
|
if (!"POST".equals(exchange.getRequestMethod())) {
|
||||||
|
exchange.sendResponseHeaders(405, -1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
GAMES_CACHE.clear();
|
||||||
|
String response = "Cache cleared";
|
||||||
|
byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8);
|
||||||
|
exchange.sendResponseHeaders(200, responseBytes.length);
|
||||||
|
try (OutputStream os = exchange.getResponseBody()) {
|
||||||
|
os.write(responseBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -166,6 +166,7 @@
|
|||||||
<h2 style="margin: 0 0 8px 0; font-size: 1.1rem">Upload Stats JSON</h2>
|
<h2 style="margin: 0 0 8px 0; font-size: 1.1rem">Upload Stats JSON</h2>
|
||||||
<input type="file" id="fileInput" accept=".json">
|
<input type="file" id="fileInput" accept=".json">
|
||||||
<button onclick="uploadFile()">Upload</button>
|
<button onclick="uploadFile()">Upload</button>
|
||||||
|
<button onclick="clearCache()" style="background: #475569; margin-left: 10px;">Clear Cache</button>
|
||||||
</div>
|
</div>
|
||||||
<span id="uploadStatus" style="font-weight: 600"></span>
|
<span id="uploadStatus" style="font-weight: 600"></span>
|
||||||
</div>
|
</div>
|
||||||
@ -230,6 +231,22 @@
|
|||||||
reader.readAsText(file);
|
reader.readAsText(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function clearCache() {
|
||||||
|
if (!confirm('Are you sure you want to clear all data?')) return;
|
||||||
|
const status = document.getElementById('uploadStatus');
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/clear', { method: 'POST' });
|
||||||
|
if (response.ok) {
|
||||||
|
status.innerText = 'Cache cleared!';
|
||||||
|
loadStats();
|
||||||
|
} else {
|
||||||
|
status.innerText = 'Clear failed: ' + await response.text();
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
status.innerText = 'Error: ' + err.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function loadStats() {
|
async function loadStats() {
|
||||||
const response = await fetch('/api/stats');
|
const response = await fetch('/api/stats');
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user