Load plugin.conf earlier and some integrated webserver improvements

This commit is contained in:
Blue (Lukas Rieger) 2020-08-25 00:57:24 +02:00
parent b6c4971420
commit 8adf2eaccf
4 changed files with 38 additions and 18 deletions

View File

@ -63,7 +63,13 @@ public LiveAPIRequestHandler(ServerInterface server, PluginConfig config, HttpRe
public HttpResponse handle(HttpRequest request) {
if (!config.isLiveUpdatesEnabled()) return this.notFoundHandler.handle(request);
HttpRequestHandler handler = liveAPIRequests.get(request.getPath());
String path = request.getPath();
//normalize path
if (path.startsWith("/")) path = path.substring(1);
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
HttpRequestHandler handler = liveAPIRequests.get(path);
if (handler != null) return handler.handle(request);
return this.notFoundHandler.handle(request);

View File

@ -93,10 +93,20 @@ public synchronized void load() throws IOException, ParseResourceException {
unload(); //ensure nothing is left running (from a failed load or something)
blueMap = new BlueMapService(serverInterface);
//load configs
CoreConfig coreConfig = blueMap.getCoreConfig();
RenderConfig renderConfig = blueMap.getRenderConfig();
WebServerConfig webServerConfig = blueMap.getWebServerConfig();
//load plugin config
pluginConfig = new PluginConfig(blueMap.getConfigManager().loadOrCreate(
new File(serverInterface.getConfigFolder(), "plugin.conf"),
Plugin.class.getResource("/plugin.conf"),
Plugin.class.getResource("/plugin-defaults.conf"),
true,
true
));
//try load resources
try {
@ -162,15 +172,6 @@ public synchronized void load() throws IOException, ParseResourceException {
blueMap.createOrUpdateWebApp(false);
blueMap.updateWebAppSettings();
//load plugin config
pluginConfig = new PluginConfig(blueMap.getConfigManager().loadOrCreate(
new File(serverInterface.getConfigFolder(), "plugin.conf"),
Plugin.class.getResource("/plugin.conf"),
Plugin.class.getResource("/plugin-defaults.conf"),
true,
true
));
//start skin updater
if (pluginConfig.isLiveUpdatesEnabled()) {
this.skinUpdater = new PlayerSkinUpdater(new File(renderConfig.getWebRoot(), "assets" + File.separator + "playerheads"));

View File

@ -84,6 +84,10 @@ public HttpResponse handle(HttpRequest request) {
private HttpResponse generateResponse(HttpRequest request) {
String path = request.getPath();
//normalize path
if (path.startsWith("/")) path = path.substring(1);
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
Path filePath = webRoot;
try {
filePath = webRoot.resolve(path);
@ -102,6 +106,13 @@ private HttpResponse generateResponse(HttpRequest request) {
File file = filePath.toFile();
// redirect to have correct relative paths
if (file.isDirectory() && !request.getPath().endsWith("/")) {
HttpResponse response = new HttpResponse(HttpStatusCode.SEE_OTHER);
response.addHeader("Location", "/" + path + "/" + (request.getGETParamString().isEmpty() ? "" : "?" + request.getGETParamString()));
return response;
}
if (!file.exists() || file.isDirectory()){
file = new File(filePath.toString() + ".gz");
isDeflated = true;
@ -164,7 +175,7 @@ private HttpResponse generateResponse(HttpRequest request) {
//add content type header
String filetype = file.getName().toString();
if (filetype.endsWith(".gz")) filetype = filetype.substring(3);
if (filetype.endsWith(".gz")) filetype = filetype.substring(0, filetype.length() - 3);
int pointIndex = filetype.lastIndexOf('.');
if (pointIndex >= 0) filetype = filetype.substring(pointIndex + 1);

View File

@ -58,6 +58,7 @@ public class HttpRequest {
private String path = null;
private Map<String, String> getParams = null;
private String getParamString = null;
public HttpRequest(String method, String adress, String version, Map<String, Set<String>> header) {
this.method = method;
@ -119,16 +120,21 @@ public Map<String, String> getGETParams() {
if (getParams == null) parseAdress();
return Collections.unmodifiableMap(getParams);
}
public String getGETParamString() {
if (getParamString == null) parseAdress();
return getParamString;
}
private void parseAdress() {
String adress = this.adress;
if (adress.isEmpty()) adress = "/";
String[] adressParts = adress.split("\\?", 2);
String path = adressParts[0];
String getParamString = adressParts.length > 1 ? adressParts[1] : "";
this.getParamString = adressParts.length > 1 ? adressParts[1] : "";
Map<String, String> getParams = new HashMap<>();
for (String getParam : getParamString.split("&")){
for (String getParam : this.getParamString.split("&")){
if (getParam.isEmpty()) continue;
String[] kv = getParam.split("=", 2);
String key = kv[0];
@ -136,10 +142,6 @@ private void parseAdress() {
getParams.put(key, value);
}
//normalize path
if (path.startsWith("/")) path = path.substring(1);
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
this.path = path;
this.getParams = getParams;
}