Refactoring
This commit is contained in:
parent
17afb42715
commit
f99efcdad7
1 changed files with 54 additions and 62 deletions
|
@ -6,19 +6,11 @@ import io.undertow.Undertow;
|
||||||
import io.undertow.server.RoutingHandler;
|
import io.undertow.server.RoutingHandler;
|
||||||
import io.undertow.server.handlers.RedirectHandler;
|
import io.undertow.server.handlers.RedirectHandler;
|
||||||
import io.undertow.util.StatusCodes;
|
import io.undertow.util.StatusCodes;
|
||||||
import io.vavr.collection.HashMap;
|
|
||||||
import io.vavr.collection.HashSet;
|
|
||||||
import io.vavr.collection.Map;
|
|
||||||
import io.vavr.collection.Set;
|
|
||||||
import io.vavr.collection.Stream;
|
|
||||||
import io.vavr.control.Option;
|
|
||||||
import io.vavr.control.Try;
|
import io.vavr.control.Try;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class LabyrinthServer {
|
public class LabyrinthServer {
|
||||||
|
@ -31,7 +23,7 @@ public class LabyrinthServer {
|
||||||
return serverOption;
|
return serverOption;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LabyrinthServer(@NonNull final ServerConfig config) {
|
private LabyrinthServer(@NonNull final ServerConfig config) {
|
||||||
log.info("Starting Server at http://{}:{}/", config.getAddress().getHostAddress(), config.getPort());
|
log.info("Starting Server at http://{}:{}/", config.getAddress().getHostAddress(), config.getPort());
|
||||||
final RoutingHandler routingHandler = new RoutingHandler().get("/", new RedirectHandler("/create/text"))
|
final RoutingHandler routingHandler = new RoutingHandler().get("/", new RedirectHandler("/create/text"))
|
||||||
.get("/create", new RedirectHandler("/create/text"))
|
.get("/create", new RedirectHandler("/create/text"))
|
||||||
|
@ -49,7 +41,7 @@ public class LabyrinthServer {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
private void start() {
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "listener-stopper"));
|
Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "listener-stopper"));
|
||||||
this.undertow.start();
|
this.undertow.start();
|
||||||
Try.of(() -> (InetSocketAddress) this.undertow.getListenerInfo().get(0).getAddress())
|
Try.of(() -> (InetSocketAddress) this.undertow.getListenerInfo().get(0).getAddress())
|
||||||
|
@ -57,7 +49,7 @@ public class LabyrinthServer {
|
||||||
.forEach(address -> log.info("Listening on http://{}:{}", address.getHostString(), address.getPort()));
|
.forEach(address -> log.info("Listening on http://{}:{}", address.getHostString(), address.getPort()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
private void stop() {
|
||||||
log.info("Stopping server ...");
|
log.info("Stopping server ...");
|
||||||
this.undertow.stop();
|
this.undertow.stop();
|
||||||
log.info("Server stopped.");
|
log.info("Server stopped.");
|
||||||
|
@ -131,41 +123,41 @@ public class LabyrinthServer {
|
||||||
// });
|
// });
|
||||||
// }
|
// }
|
||||||
|
|
||||||
@NonNull
|
// @NonNull
|
||||||
private Map<RequestParameter, String> parseQueryString(@Nullable final String query) {
|
// private Map<RequestParameter, String> parseQueryString(@Nullable final String query) {
|
||||||
if (query == null) {
|
// if (query == null) {
|
||||||
return HashMap.empty();
|
// return HashMap.empty();
|
||||||
}
|
// }
|
||||||
HashMap<RequestParameter, String> result = HashMap.empty();
|
// HashMap<RequestParameter, String> result = HashMap.empty();
|
||||||
final String[] parts = query.split("&");
|
// final String[] parts = query.split("&");
|
||||||
for (final String part : parts) {
|
// for (final String part : parts) {
|
||||||
final int split = part.indexOf('=');
|
// final int split = part.indexOf('=');
|
||||||
if (split == -1) {
|
// if (split == -1) {
|
||||||
final Try<RequestParameter> tryKey = Try.of(() -> this.normalizeParameterName(part));
|
// final Try<RequestParameter> tryKey = Try.of(() -> this.normalizeParameterName(part));
|
||||||
if (tryKey.isSuccess()) {
|
// if (tryKey.isSuccess()) {
|
||||||
result = result.put(tryKey.get(), null);
|
// result = result.put(tryKey.get(), null);
|
||||||
}
|
// }
|
||||||
} else {
|
// } else {
|
||||||
final String key = part.substring(0, split);
|
// final String key = part.substring(0, split);
|
||||||
final String value = part.substring(split + 1);
|
// final String value = part.substring(split + 1);
|
||||||
final Try<RequestParameter> tryKey = Try.of(() -> this.normalizeParameterName(key));
|
// final Try<RequestParameter> tryKey = Try.of(() -> this.normalizeParameterName(key));
|
||||||
if (tryKey.isSuccess()) {
|
// if (tryKey.isSuccess()) {
|
||||||
result = result.put(tryKey.get(), value);
|
// result = result.put(tryKey.get(), value);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return result;
|
// return result;
|
||||||
}
|
// }
|
||||||
|
|
||||||
private RequestParameter normalizeParameterName(final String paramName) {
|
// private RequestParameter normalizeParameterName(final String paramName) {
|
||||||
return RequestParameter.parseName(paramName).get();
|
// return RequestParameter.parseName(paramName).get();
|
||||||
}
|
// }
|
||||||
|
|
||||||
private <T> T getOrDefault(@NonNull final Option<String> input,
|
// private <T> T getOrDefault(@NonNull final Option<String> input,
|
||||||
@NonNull final Function<String, T> mapper,
|
// @NonNull final Function<String, T> mapper,
|
||||||
@Nullable final T defaultValue) {
|
// @Nullable final T defaultValue) {
|
||||||
return input.toTry().map(mapper).getOrElse(defaultValue);
|
// return input.toTry().map(mapper).getOrElse(defaultValue);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// private void handleRender(final HttpExchange exchange) {
|
// private void handleRender(final HttpExchange exchange) {
|
||||||
// this.executorService.submit(() -> {
|
// this.executorService.submit(() -> {
|
||||||
|
@ -211,22 +203,22 @@ public class LabyrinthServer {
|
||||||
// });
|
// });
|
||||||
// }
|
// }
|
||||||
|
|
||||||
private enum RequestParameter {
|
// private enum RequestParameter {
|
||||||
WIDTH("w", "width"),
|
// WIDTH("w", "width"),
|
||||||
HEIGHT("h", "height"),
|
// HEIGHT("h", "height"),
|
||||||
ID("i", "id"),
|
// ID("i", "id"),
|
||||||
OUTPUT("o", "output");
|
// OUTPUT("o", "output");
|
||||||
private final Set<String> names;
|
// private final Set<String> names;
|
||||||
|
//
|
||||||
RequestParameter(@NonNull final String... names) {
|
// RequestParameter(@NonNull final String... names) {
|
||||||
this.names = HashSet.of(names);
|
// this.names = HashSet.of(names);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
static Option<RequestParameter> parseName(@Nullable final String name) {
|
// static Option<RequestParameter> parseName(@Nullable final String name) {
|
||||||
if (name == null) {
|
// if (name == null) {
|
||||||
return Option.none();
|
// return Option.none();
|
||||||
}
|
// }
|
||||||
return Stream.of(values()).find(param -> param.names.exists(name::equalsIgnoreCase));
|
// return Stream.of(values()).find(param -> param.names.exists(name::equalsIgnoreCase));
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue