introduce docsify; refactor the daemon

This commit is contained in:
Andreas Penski 2020-05-06 16:17:45 +02:00
parent 7dc62012a6
commit 91e4d79953
12 changed files with 1038 additions and 26 deletions

View file

@ -11,15 +11,21 @@ import com.sun.net.httpserver.HttpHandler;
*
* @author Andreas Penski
*/
public abstract class BaseHandler implements HttpHandler {
abstract class BaseHandler implements HttpHandler {
protected static final String APPLICATION_XML = "application/xml";
protected static final int OK = 200;
protected static void write(final HttpExchange exchange, final byte[] content, final String contentType) throws IOException {
final OutputStream os = exchange.getResponseBody();
write(exchange, contentType, os -> os.write(content));
}
protected static void write(final HttpExchange exchange, final String contentType, Write write) throws IOException {
exchange.getResponseHeaders().add("Content-Type", contentType);
exchange.sendResponseHeaders(200, content.length);
os.write(content);
exchange.sendResponseHeaders(OK, 0);
final OutputStream os = exchange.getResponseBody();
write.write(os);
os.close();
}
@ -31,4 +37,9 @@ public abstract class BaseHandler implements HttpHandler {
os.close();
}
@FunctionalInterface
protected interface Write {
public void write(OutputStream out) throws IOException;
}
}

View file

@ -26,7 +26,7 @@ import de.kosit.validationtool.model.scenarios.Scenarios;
*/
@Slf4j
@RequiredArgsConstructor
public class ConfigHandler extends BaseHandler {
class ConfigHandler extends BaseHandler {
private final Configuration configuration;

View file

@ -7,8 +7,10 @@ import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@ -32,14 +34,19 @@ public class Daemon {
private static final int DEFAULT_PORT = 8080;
private static final int DEFAULT_THREAD_COUNT = Runtime.getRuntime().availableProcessors();
private final String hostName;
private final int port;
private final int threadCount;
@Setter(AccessLevel.PRIVATE)
private boolean guiDisabled = false;
public void disableGui() {
guiDisabled = true;
}
/**
* Methode zum Starten des Servers
*
@ -53,8 +60,7 @@ public class Daemon {
final ConversionService converter = new ConversionService();
server = HttpServer.create(getSocket(), 0);
final DefaultCheck check = new DefaultCheck(config);
server.createContext("/", new CheckHandler(check, config.getContentRepository().getProcessor()));
server.createContext("/", createRootHandler(config));
server.createContext("/server/health", new HealthHandler(config, healthConverter));
server.createContext("/server/config", new ConfigHandler(config, converter));
server.setExecutor(createExecutor());
@ -65,8 +71,21 @@ public class Daemon {
}
}
private HttpHandler createRootHandler(Configuration config) {
HttpHandler rootHandler;
final DefaultCheck check = new DefaultCheck(config);
final CheckHandler checkHandler = new CheckHandler(check, config.getContentRepository().getProcessor());
if (!guiDisabled) {
GuiHandler gui = new GuiHandler();
rootHandler = new RoutingHandler(checkHandler, gui);
} else {
rootHandler = checkHandler;
}
return rootHandler;
}
private ExecutorService createExecutor() {
return Executors.newFixedThreadPool(this.threadCount > 0 ? this.threadCount : DEFAULT_THREAD_COUNT);
return Executors.newFixedThreadPool(this.threadCount > 0 ? this.threadCount : Runtime.getRuntime().availableProcessors());
}
private InetSocketAddress getSocket() {

View file

@ -0,0 +1,52 @@
package de.kosit.validationtool.daemon;
import com.sun.net.httpserver.HttpExchange;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Arrays;
public class GuiHandler extends BaseHandler {
private static final URL INDEX_HTML = GuiHandler.class.getClassLoader().getResource("gui/index.html");
public GuiHandler() {
if (INDEX_HTML == null) {
throw new IllegalStateException("No html found");
}
}
@Override
public void handle(HttpExchange exchange) throws IOException {
assert INDEX_HTML != null;
final String path = exchange.getRequestURI().toASCIIString();
if (path.equals("/")) {
write(exchange, IOUtils.toString(INDEX_HTML, Charset.defaultCharset()).getBytes(), "text/html");
} else{
final URL resource = GuiHandler.class.getClassLoader().getResource("gui" + path);
if (resource != null) {
write(exchange,IOUtils.toString(resource, Charset.defaultCharset()).getBytes(), Mediatype.resolveBySuffix(resource.getPath()).getMimeType());;
}else {
error(exchange,404,"not found");
}
}
}
@RequiredArgsConstructor
@Getter
protected enum Mediatype {
JS("application/javascript"),
MD("text/markdown"),
CSS("text/css");
private final String mimeType;
static Mediatype resolveBySuffix(String path){
return Arrays.stream(values()).filter(e->path.toUpperCase().endsWith("."+e.name())).findFirst().orElse(Mediatype.MD);
}
}
}

View file

@ -4,6 +4,8 @@ import java.io.IOException;
import com.sun.net.httpserver.HttpExchange;
import de.kosit.validationtool.impl.EngineInformation;
import de.kosit.validationtool.model.daemon.ApplicationType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -15,13 +17,12 @@ import de.kosit.validationtool.model.daemon.MemoryType;
/**
* Handler that implements a simple health check. Useful for monitoring the service.
*
* @author Andreas Penski`
* @author Andreas Penski
*/
@Slf4j
@RequiredArgsConstructor
class HealthHandler extends BaseHandler {
private final Configuration scenarios;
private final ConversionService conversionService;
@ -34,9 +35,11 @@ class HealthHandler extends BaseHandler {
}
private static HealthType createHealth() {
private HealthType createHealth() {
final HealthType h = new HealthType();
h.setMemory(createMemory());
h.setApplication(createApplication());
h.setStatus(scenarios.getScenarios().size() > 0 ? "UP" : "DOWN");
return h;
}
@ -48,4 +51,12 @@ class HealthHandler extends BaseHandler {
m.setTotalMemory(runtime.totalMemory());
return m;
}
private static ApplicationType createApplication() {
ApplicationType a = new ApplicationType();
a.setBuild(EngineInformation.getBuild());
a.setName(EngineInformation.getName());
a.setVersion(EngineInformation.getVersion());
return a;
}
}

View file

@ -0,0 +1,27 @@
package de.kosit.validationtool.daemon;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import lombok.RequiredArgsConstructor;
import java.io.IOException;
@RequiredArgsConstructor
public class RoutingHandler extends BaseHandler {
private final CheckHandler checkHandler;
private final GuiHandler guiHandler;
@Override
public void handle(HttpExchange exchange) throws IOException {
final String requestMethod = exchange.getRequestMethod();
if (requestMethod.equals("POST")) {
checkHandler.handle(exchange);
} else if (requestMethod.equals("GET")) {
guiHandler.handle(exchange);
} else {
error(exchange, 405, String.format("Method % not supported", requestMethod));
}
}
}