- Feat: Enable rendering binary data to any supported output format. - Fix: Correctly serve as attachment when outputtyp is pdffile or binary.
59 lines
2 KiB
Java
59 lines
2 KiB
Java
package ch.fritteli.labyrinth.server;
|
|
|
|
import ch.fritteli.labyrinth.server.handler.CreateHandler;
|
|
import ch.fritteli.labyrinth.server.handler.RenderHandler;
|
|
import io.undertow.Undertow;
|
|
import io.undertow.server.RoutingHandler;
|
|
import io.vavr.control.Try;
|
|
import java.net.InetSocketAddress;
|
|
import lombok.NonNull;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
|
public class LabyrinthServer {
|
|
|
|
@NonNull
|
|
private final Undertow undertow;
|
|
|
|
private LabyrinthServer(@NonNull final ServerConfig config) {
|
|
final String hostAddress = config.getAddress().getHostAddress();
|
|
final int port = config.getPort();
|
|
log.info("Starting Server at http://{}:{}/", hostAddress, port);
|
|
final RoutingHandler routingHandler = new RoutingHandler()
|
|
.get(CreateHandler.PATH_TEMPLATE, new CreateHandler())
|
|
.post(RenderHandler.PATH_TEMPLATE, new RenderHandler());
|
|
|
|
this.undertow = Undertow.builder()
|
|
.addHttpListener(port, hostAddress)
|
|
.setHandler(routingHandler)
|
|
.build();
|
|
}
|
|
|
|
@NonNull
|
|
public static Try<LabyrinthServer> createAndStartServer() {
|
|
return Try.of(ServerConfig::init)
|
|
.flatMapTry(LabyrinthServer::createAndStartServer);
|
|
}
|
|
|
|
@NonNull
|
|
public static Try<LabyrinthServer> createAndStartServer(@NonNull final ServerConfig config) {
|
|
return Try.of(() -> new LabyrinthServer(config))
|
|
.peek(LabyrinthServer::start);
|
|
}
|
|
|
|
private void start() {
|
|
Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "listener-stopper"));
|
|
this.undertow.start();
|
|
final InetSocketAddress address = (InetSocketAddress) this.undertow.getListenerInfo().get(0).getAddress();
|
|
final String hostAddress = address.getAddress().getHostAddress();
|
|
final int port = address.getPort();
|
|
|
|
log.info("Listening on http://{}:{}", hostAddress, port);
|
|
}
|
|
|
|
private void stop() {
|
|
log.info("Stopping server ...");
|
|
this.undertow.stop();
|
|
log.info("Server stopped.");
|
|
}
|
|
}
|