add caching (untested)

This commit is contained in:
ayunami2000 2022-04-17 19:21:44 -04:00
parent 01d2cf44ea
commit b6b5a7f227
11 changed files with 132 additions and 25 deletions

View file

@ -1,5 +1,6 @@
package dev.plex; package dev.plex;
import dev.plex.cache.FileCache;
import dev.plex.config.ModuleConfig; import dev.plex.config.ModuleConfig;
import dev.plex.module.PlexModule; import dev.plex.module.PlexModule;
import dev.plex.request.impl.AdminsEndpoint; import dev.plex.request.impl.AdminsEndpoint;
@ -35,6 +36,8 @@ public class HTTPDModule extends PlexModule
public static ModuleConfig moduleConfig; public static ModuleConfig moduleConfig;
public static final FileCache fileCache = new FileCache();
@Override @Override
public void load() public void load()
{ {

View file

@ -0,0 +1,19 @@
package dev.plex.cache;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
public class CacheItem
{
public String path;
public byte[] file;
public long timestamp;
public CacheItem(File f) throws IOException
{
this.path = f.getPath();
this.file = Files.readAllBytes(f.toPath());
this.timestamp = System.currentTimeMillis();
}
}

View file

@ -0,0 +1,34 @@
package dev.plex.cache;
import com.google.common.collect.EvictingQueue;
import java.io.File;
import java.io.IOException;
import java.util.Queue;
public class FileCache
{
private final Queue<CacheItem> cache = EvictingQueue.create(15);
public byte[] getFile(File file) throws IOException
{
return getFile(file.getPath());
}
public byte[] getFile(String path) throws IOException
{
CacheItem theItem = cache.stream().filter(cacheItem -> cacheItem.path.equals(path)).findFirst().orElse(null);
if (theItem == null)
{
theItem = new CacheItem(new File(path));
cache.add(theItem);
}
if (System.currentTimeMillis() - theItem.timestamp > 3 * 60 * 1000) // 3 minutes
{
cache.remove(theItem);
theItem = new CacheItem(new File(path));
cache.add(theItem);
}
return theItem.file;
}
}

View file

@ -46,7 +46,7 @@ public class AbstractServlet extends HttpServlet
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{ {
String ipAddress = req.getRemoteAddr(); String ipAddress = req.getRemoteAddr();
if (ipAddress == null) if (ipAddress.equals("127.0.0.1"))
{ {
ipAddress = req.getHeader("X-FORWARDED-FOR"); ipAddress = req.getHeader("X-FORWARDED-FOR");
} }
@ -72,8 +72,8 @@ public class AbstractServlet extends HttpServlet
resp.setStatus(HttpServletResponse.SC_OK); resp.setStatus(HttpServletResponse.SC_OK);
try try
{ {
Object object = mapping.method.invoke(this, req); Object object = mapping.method.invoke(this, req, resp);
resp.getWriter().println(object.toString()); if (object != null) resp.getWriter().println(object.toString());
} }
catch (IOException | IllegalAccessException | InvocationTargetException e) catch (IOException | IllegalAccessException | InvocationTargetException e)
{ {

View file

@ -14,6 +14,8 @@ import dev.plex.util.adapter.LocalDateTimeSerializer;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import jakarta.servlet.http.HttpServletResponse;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -22,7 +24,7 @@ public class AdminsEndpoint extends AbstractServlet
private static final String TITLE = "Admins - Plex HTTPD"; private static final String TITLE = "Admins - Plex HTTPD";
@GetMapping(endpoint = "/api/admins/") @GetMapping(endpoint = "/api/admins/")
public String getAdmins(HttpServletRequest request) public String getAdmins(HttpServletRequest request, HttpServletResponse response)
{ {
String ipAddress = request.getRemoteAddr(); String ipAddress = request.getRemoteAddr();
if (ipAddress == null) if (ipAddress == null)

View file

@ -10,6 +10,7 @@ import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping; import dev.plex.request.GetMapping;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -18,7 +19,7 @@ public class IndefBansEndpoint extends AbstractServlet
private static final String TITLE = "Indefinite Bans - Plex HTTPD"; private static final String TITLE = "Indefinite Bans - Plex HTTPD";
@GetMapping(endpoint = "/api/indefbans/") @GetMapping(endpoint = "/api/indefbans/")
public String getBans(HttpServletRequest request) public String getBans(HttpServletRequest request, HttpServletResponse response)
{ {
String ipAddress = request.getRemoteAddr(); String ipAddress = request.getRemoteAddr();
if (ipAddress == null) if (ipAddress == null)

View file

@ -3,18 +3,19 @@ package dev.plex.request.impl;
import dev.plex.request.AbstractServlet; import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping; import dev.plex.request.GetMapping;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
public class IndexEndpoint extends AbstractServlet public class IndexEndpoint extends AbstractServlet
{ {
@GetMapping(endpoint = "//") @GetMapping(endpoint = "//")
public String getIndex(HttpServletRequest request) public String getIndex(HttpServletRequest request, HttpServletResponse response)
{ {
return indexHTML(); return indexHTML();
} }
@GetMapping(endpoint = "/api/") @GetMapping(endpoint = "/api/")
public String getAPI(HttpServletRequest request) public String getAPI(HttpServletRequest request, HttpServletResponse response)
{ {
return indexHTML(); return indexHTML();
} }

View file

@ -6,6 +6,8 @@ import dev.plex.request.GetMapping;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -14,7 +16,7 @@ public class ListEndpoint extends AbstractServlet
List<String> players = new ArrayList<>(); List<String> players = new ArrayList<>();
@GetMapping(endpoint = "/api/list/") @GetMapping(endpoint = "/api/list/")
public String getOnlinePlayers(HttpServletRequest request) public String getOnlinePlayers(HttpServletRequest request, HttpServletResponse response)
{ {
for (Player player : Bukkit.getOnlinePlayers()) for (Player player : Bukkit.getOnlinePlayers())
{ {

View file

@ -13,13 +13,15 @@ import dev.plex.util.adapter.LocalDateTimeSerializer;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.UUID; import java.util.UUID;
import jakarta.servlet.http.HttpServletResponse;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
public class PunishmentsEndpoint extends AbstractServlet public class PunishmentsEndpoint extends AbstractServlet
{ {
@GetMapping(endpoint = "/api/punishments/") @GetMapping(endpoint = "/api/punishments/")
public String getPunishments(HttpServletRequest request) public String getPunishments(HttpServletRequest request, HttpServletResponse response)
{ {
String ipAddress = request.getRemoteAddr(); String ipAddress = request.getRemoteAddr();
if (ipAddress == null) if (ipAddress == null)

View file

@ -1,50 +1,92 @@
package dev.plex.request.impl; package dev.plex.request.impl;
import dev.plex.HTTPDModule;
import dev.plex.request.AbstractServlet; import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping; import dev.plex.request.GetMapping;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays; import java.util.Arrays;
import jakarta.servlet.http.HttpServletResponse;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
public class SchematicDownloadEndpoint extends AbstractServlet public class SchematicDownloadEndpoint extends AbstractServlet
{ {
@GetMapping(endpoint = "/api/schematics/download/") @GetMapping(endpoint = "/api/schematics/download/")
public String downloadSchematics(HttpServletRequest request) public String downloadSchematics(HttpServletRequest request, HttpServletResponse response)
{
if (request.getPathInfo() == null || request.getPathInfo().equals("/"))
{ {
return schematicHTML(); return schematicHTML();
} }
else
private String schematicHTML() {
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
} catch (IOException e) {
return null;
}
schematicServe(request.getPathInfo().replace("/", ""), outputStream);
return null;
}
}
private File getWorldeditFolder()
{ {
String file = readFile(this.getClass().getResourceAsStream("/httpd/schematic_list.html"));
File worldeditFolder;
if (Bukkit.getPluginManager().isPluginEnabled("WorldEdit")) if (Bukkit.getPluginManager().isPluginEnabled("WorldEdit"))
{ {
worldeditFolder = new File(Bukkit.getPluginManager().getPlugin("WorldEdit").getDataFolder() + "/schematics/"); return new File(Bukkit.getPluginManager().getPlugin("WorldEdit").getDataFolder() + "/schematics/");
} }
else if (Bukkit.getPluginManager().isPluginEnabled("FastAsyncWorldEdit")) else if (Bukkit.getPluginManager().isPluginEnabled("FastAsyncWorldEdit"))
{ {
worldeditFolder = new File(Bukkit.getPluginManager().getPlugin("FastAsyncWorldEdit").getDataFolder() + "/schematics/"); return new File(Bukkit.getPluginManager().getPlugin("FastAsyncWorldEdit").getDataFolder() + "/schematics/");
} }
else else
{ {
return null; return null;
} }
}
private void schematicServe(String requestedSchematic, OutputStream outputStream)
{
File worldeditFolder = getWorldeditFolder();
if (worldeditFolder == null) return;
File[] schems = worldeditFolder.listFiles();
if (schems != null)
{
File schemFile = Arrays.stream(schems).filter(file -> file.getName().equals(requestedSchematic)).findFirst().orElse(null);
if (schemFile != null)
{
try
{
outputStream.write(HTTPDModule.fileCache.getFile(schemFile));
} catch (IOException ignored) {}
}
}
}
private String schematicHTML()
{
String file = readFile(this.getClass().getResourceAsStream("/httpd/schematic_list.html"));
File worldeditFolder = getWorldeditFolder();
if (worldeditFolder == null) return null;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
File[] alphabetical = worldeditFolder.listFiles(); File[] alphabetical = worldeditFolder.listFiles();
if (alphabetical != null) if (alphabetical != null)
{ {
Arrays.sort(alphabetical); Arrays.sort(alphabetical);
}
for (File worldeditFile : alphabetical) for (File worldeditFile : alphabetical)
{ {
sb.append("<tr>" + sb.append("<tr>" +
"<th scope=\"row\">" + worldeditFile.getName() + "</th>" + "<th scope=\"row\"><a href=\"" + worldeditFile.getName() + "\" download>" + worldeditFile.getName() + "</a></th>" +
"<td>" + worldeditFile.length() + "B" + "</td>" + "<td>" + worldeditFile.length() + "B" + "</td>" +
"</tr>"); "</tr>");
} }
file = file.replace("${schematics}", sb.toString()); file = file.replace("${schematics}", sb.toString());
}
return file; return file;
} }
} }

View file

@ -3,11 +3,12 @@ package dev.plex.request.impl;
import dev.plex.request.AbstractServlet; import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping; import dev.plex.request.GetMapping;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class SchematicIndexEndpoint extends AbstractServlet public class SchematicIndexEndpoint extends AbstractServlet
{ {
@GetMapping(endpoint = "/api/schematics/") @GetMapping(endpoint = "/api/schematics/")
public String schematicIndex(HttpServletRequest request) public String schematicIndex(HttpServletRequest request, HttpServletResponse response)
{ {
return readFile(this.getClass().getResourceAsStream("/httpd/schematic_list.html")); return readFile(this.getClass().getResourceAsStream("/httpd/schematic_list.html"));
} }