Merge branch 'rye761-logactivity' into bleeding

This commit is contained in:
Blue (Lukas Rieger) 2020-10-04 12:25:35 +02:00
commit 961e65b3b3
No known key found for this signature in database
GPG Key ID: 904C4995F9E1F800
4 changed files with 36 additions and 7 deletions

View File

@ -130,7 +130,8 @@ public void load() throws IOException, ParseResourceException {
webServerConfig.getWebserverPort(),
webServerConfig.getWebserverMaxConnections(),
webServerConfig.getWebserverBindAdress(),
requestHandler
requestHandler,
false
);
webServer.start();
}

View File

@ -34,6 +34,9 @@
import java.util.concurrent.TimeUnit;
import de.bluecolored.bluemap.core.logger.Logger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class HttpConnection implements Runnable {
@ -43,11 +46,14 @@ public class HttpConnection implements Runnable {
private Socket connection;
private InputStream in;
private OutputStream out;
private final boolean verbose;
public HttpConnection(ServerSocket server, Socket connection, HttpRequestHandler handler, int timeout, TimeUnit timeoutUnit) throws IOException {
public HttpConnection(ServerSocket server, Socket connection, HttpRequestHandler handler, int timeout, TimeUnit timeoutUnit, boolean verbose) throws IOException {
this.server = server;
this.connection = connection;
this.handler = handler;
this.verbose = verbose;
if (isClosed()){
throw new IOException("Socket already closed!");
@ -66,6 +72,9 @@ public void run() {
HttpRequest request = acceptRequest();
HttpResponse response = handler.handle(request);
sendResponse(response);
if (verbose) {
log(request, response);
}
} catch (InvalidRequestException e){
try {
sendResponse(new HttpResponse(HttpStatusCode.BAD_REQUEST));
@ -89,6 +98,21 @@ public void run() {
Logger.global.logError("Error while closing HttpConnection!", e);
}
}
private void log(HttpRequest request, HttpResponse response) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
Logger.global.logInfo(
connection.getInetAddress().toString()
+ " [ "
+ dateFormat.format(date)
+ " ] \""
+ request.getMethod()
+ " " + request.getPath()
+ " " + request.getVersion()
+ "\" "
+ response.getStatusCode().toString());
}
private void sendResponse(HttpResponse response) throws IOException {
response.write(out);

View File

@ -41,6 +41,7 @@ public class WebServer extends Thread {
private final int port;
private final int maxConnections;
private final InetAddress bindAdress;
private final boolean verbose;
private HttpRequestHandler handler;
@ -48,10 +49,11 @@ public class WebServer extends Thread {
private ServerSocket server;
public WebServer(int port, int maxConnections, InetAddress bindAdress, HttpRequestHandler handler) {
public WebServer(int port, int maxConnections, InetAddress bindAdress, HttpRequestHandler handler, boolean verbose) {
this.port = port;
this.maxConnections = maxConnections;
this.bindAdress = bindAdress;
this.verbose = verbose;
this.handler = handler;
@ -88,7 +90,7 @@ public void run(){
Socket connection = server.accept();
try {
connectionThreads.execute(new HttpConnection(server, connection, handler, 10, TimeUnit.SECONDS));
connectionThreads.execute(new HttpConnection(server, connection, handler, 10, TimeUnit.SECONDS, verbose));
} catch (RejectedExecutionException e){
connection.close();
Logger.global.logWarning("Dropped an incoming HttpConnection! (Too many connections?)");

View File

@ -229,7 +229,7 @@ public void renderMaps(BlueMapService blueMap, boolean forceRender, boolean forc
Logger.global.logInfo("Render finished!");
}
public void startWebserver(BlueMapService blueMap) throws IOException {
public void startWebserver(BlueMapService blueMap, boolean verbose) throws IOException {
Logger.global.logInfo("Starting webserver ...");
WebServerConfig config = blueMap.getWebServerConfig();
@ -239,7 +239,8 @@ public void startWebserver(BlueMapService blueMap) throws IOException {
config.getWebserverPort(),
config.getWebserverMaxConnections(),
config.getWebserverBindAdress(),
requestHandler
requestHandler,
verbose
);
webServer.start();
}
@ -285,7 +286,7 @@ public static void main(String[] args) {
if (cmd.hasOption("w")) {
noActions = false;
cli.startWebserver(blueMap);
cli.startWebserver(blueMap, cmd.hasOption("b"));
Thread.sleep(1000); //wait a second to let the webserver start, looks nicer in the log if anything comes after that
}
@ -376,6 +377,7 @@ private static Options createOptions() {
);
options.addOption("w", "webserver", false, "Starts the web-server, configured in the 'webserver.conf' file");
options.addOption("b", "verbose", false, "Causes the web-server to log requests to the console");
options.addOption("g", "generate-webapp", false, "Generates the files for the web-app to the folder, configured in the 'render.conf' file (this is done automatically when rendering if the 'index.html' file in the webroot can't be found)");
options.addOption("s", "generate-websettings", false, "Generates the settings for the web-app, using the settings from the 'render.conf' file (this is done automatically when rendering)");