Initial Commit
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
package dev.coph.nextusweb.server;
|
||||
|
||||
import dev.coph.nextusweb.server.router.Request;
|
||||
import dev.coph.nextusweb.server.router.Response;
|
||||
import dev.coph.nextusweb.server.router.Router;
|
||||
import dev.coph.nextusweb.server.router.exception.BadRequestException;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.handler.codec.http.*;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
|
||||
|
||||
private static final Executor VT_EXECUTOR =
|
||||
Executors.newVirtualThreadPerTaskExecutor();
|
||||
|
||||
private final Router router;
|
||||
|
||||
public HttpRequestHandler(Router router) {
|
||||
this.router = router;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) {
|
||||
req.retain();
|
||||
VT_EXECUTOR.execute(() -> {
|
||||
try {
|
||||
handle(ctx, req);
|
||||
} finally {
|
||||
req.release();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void handle(ChannelHandlerContext ctx, FullHttpRequest raw) {
|
||||
String path = new QueryStringDecoder(raw.uri()).path();
|
||||
Router.Resolution resolution = router.resolve(raw.method(), path);
|
||||
|
||||
Response res = new Response();
|
||||
|
||||
switch (resolution) {
|
||||
case Router.Resolution.Match m -> {
|
||||
Request request = new Request(raw, m.pathParams());
|
||||
try {
|
||||
for (var mw : router.middlewares()) mw.accept(request, res);
|
||||
m.handler().handle(request, res);
|
||||
} catch (BadRequestException e) {
|
||||
res.status(400).json("{\"error\":\"" + e.getMessage() + "\"}");
|
||||
} catch (Exception e) {
|
||||
res.status(500).text("Internal Server Error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
case Router.Resolution.MethodNotAllowed mna -> {
|
||||
String allow = mna.allowedMethods().stream()
|
||||
.map(HttpMethod::name)
|
||||
.sorted()
|
||||
.collect(Collectors.joining(", "));
|
||||
res.status(405)
|
||||
.header(HttpHeaderNames.ALLOW.toString(), allow)
|
||||
.json("{\"error\":\"Method Not Allowed\",\"allowed\":\"" + allow + "\"}");
|
||||
}
|
||||
case Router.Resolution.NotFound nf -> res.status(404).json("{\"error\":\"Not Found\"}");
|
||||
}
|
||||
|
||||
send(ctx, res);
|
||||
}
|
||||
|
||||
private void send(ChannelHandlerContext ctx, Response res) {
|
||||
var nettyRes = new DefaultFullHttpResponse(
|
||||
HttpVersion.HTTP_1_1,
|
||||
HttpResponseStatus.valueOf(res.status()),
|
||||
Unpooled.wrappedBuffer(res.body())
|
||||
);
|
||||
nettyRes.headers().add(res.headers());
|
||||
nettyRes.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, res.body().length);
|
||||
ctx.writeAndFlush(nettyRes).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package dev.coph.nextusweb.server;
|
||||
|
||||
import dev.coph.nextusweb.server.router.Router;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.epoll.Epoll;
|
||||
import io.netty.channel.epoll.EpollIoHandler;
|
||||
import io.netty.channel.epoll.EpollServerSocketChannel;
|
||||
import io.netty.channel.kqueue.KQueue;
|
||||
import io.netty.channel.kqueue.KQueueIoHandler;
|
||||
import io.netty.channel.kqueue.KQueueServerSocketChannel;
|
||||
import io.netty.channel.nio.NioIoHandler;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||
import io.netty.handler.codec.http.HttpServerCodec;
|
||||
|
||||
public final class HttpServer {
|
||||
|
||||
public static void start(int port, Router router) throws InterruptedException {
|
||||
EventLoopGroup boss, worker;
|
||||
Class<? extends ServerChannel> channelClass;
|
||||
|
||||
if (Epoll.isAvailable()) {
|
||||
boss = new MultiThreadIoEventLoopGroup(1, EpollIoHandler.newFactory());
|
||||
worker = new MultiThreadIoEventLoopGroup(EpollIoHandler.newFactory());
|
||||
channelClass = EpollServerSocketChannel.class;
|
||||
} else if (KQueue.isAvailable()) {
|
||||
boss = new MultiThreadIoEventLoopGroup(1, KQueueIoHandler.newFactory());
|
||||
worker = new MultiThreadIoEventLoopGroup(KQueueIoHandler.newFactory());
|
||||
channelClass = KQueueServerSocketChannel.class;
|
||||
} else {
|
||||
boss = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
|
||||
worker = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
|
||||
channelClass = NioServerSocketChannel.class;
|
||||
}
|
||||
|
||||
try {
|
||||
new ServerBootstrap()
|
||||
.group(boss, worker)
|
||||
.channel(channelClass)
|
||||
.option(ChannelOption.SO_BACKLOG, 8192)
|
||||
.childOption(ChannelOption.TCP_NODELAY, true)
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) {
|
||||
ch.pipeline()
|
||||
.addLast(new HttpServerCodec())
|
||||
.addLast(new HttpObjectAggregator(1024 * 1024))
|
||||
.addLast(new HttpRequestHandler(router));
|
||||
}
|
||||
})
|
||||
.bind(port).sync().channel().closeFuture().sync();
|
||||
} finally {
|
||||
boss.shutdownGracefully();
|
||||
worker.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package dev.coph.nextusweb.server.annotation;
|
||||
|
||||
import dev.coph.nextusweb.server.router.Request;
|
||||
import dev.coph.nextusweb.server.router.Response;
|
||||
import dev.coph.nextusweb.server.router.Router;
|
||||
import io.netty.handler.codec.http.HttpMethod;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public final class AnnotationScanner {
|
||||
|
||||
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
|
||||
private static final MethodType HANDLER_TYPE =
|
||||
MethodType.methodType(void.class, Request.class, Response.class);
|
||||
|
||||
private AnnotationScanner() {
|
||||
}
|
||||
|
||||
public static void register(Router router, Object controller) {
|
||||
Class<?> clazz = controller.getClass();
|
||||
Controller ctrlAnno = clazz.getAnnotation(Controller.class);
|
||||
if (ctrlAnno == null) {
|
||||
throw new IllegalArgumentException(
|
||||
clazz.getName() + " ist nicht mit @Controller annotiert");
|
||||
}
|
||||
String prefix = normalizePrefix(ctrlAnno.value());
|
||||
|
||||
for (Method method : clazz.getDeclaredMethods()) {
|
||||
RouteInfo info = extractRoute(method);
|
||||
if (info == null) continue;
|
||||
|
||||
validateSignature(method);
|
||||
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
MethodHandle mh = LOOKUP.unreflect(method).bindTo(controller);
|
||||
|
||||
Router.Handler handler = (req, res) -> {
|
||||
try {
|
||||
mh.invokeExact(req, res);
|
||||
} catch (Throwable t) {
|
||||
if (t instanceof Exception e) throw e;
|
||||
throw new RuntimeException(t);
|
||||
}
|
||||
};
|
||||
|
||||
String fullPath = prefix + normalizePath(info.path());
|
||||
router.register(HttpMethod.valueOf(info.method()), fullPath, handler);
|
||||
System.out.printf("Registered: %-6s %-30s -> %s.%s%n",
|
||||
info.method(), fullPath, clazz.getSimpleName(), method.getName());
|
||||
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException("Konnte Methode nicht registrieren: " + method, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String normalizePrefix(String p) {
|
||||
if (p == null || p.isEmpty()) return "";
|
||||
return p.startsWith("/") ? p : "/" + p;
|
||||
}
|
||||
|
||||
private static RouteInfo extractRoute(Method m) {
|
||||
Route r = m.getAnnotation(Route.class);
|
||||
if (r != null) return new RouteInfo(r.method(), r.path());
|
||||
|
||||
GET get = m.getAnnotation(GET.class);
|
||||
if (get != null) return new RouteInfo("GET", get.value());
|
||||
|
||||
POST post = m.getAnnotation(POST.class);
|
||||
if (post != null) return new RouteInfo("POST", post.value());
|
||||
|
||||
PUT put = m.getAnnotation(PUT.class);
|
||||
if (put != null) return new RouteInfo("PUT", put.value());
|
||||
|
||||
DELETE del = m.getAnnotation(DELETE.class);
|
||||
if (del != null) return new RouteInfo("DELETE", del.value());
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void validateSignature(Method m) {
|
||||
Class<?>[] params = m.getParameterTypes();
|
||||
if (params.length != 2 || params[0] != Request.class || params[1] != Response.class) {
|
||||
throw new IllegalArgumentException(
|
||||
"Handler-Methode " + m + " muss Signatur (Request, Response) haben");
|
||||
}
|
||||
if (m.getReturnType() != void.class) {
|
||||
throw new IllegalArgumentException(
|
||||
"Handler-Methode " + m + " muss void zurückgeben");
|
||||
}
|
||||
}
|
||||
|
||||
private static String normalizePath(String p) {
|
||||
if (p == null || p.isEmpty()) return "";
|
||||
return p.startsWith("/") ? p : "/" + p;
|
||||
}
|
||||
|
||||
private record RouteInfo(String method, String path) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.coph.nextusweb.server.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface Controller {
|
||||
String value() default "";
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.coph.nextusweb.server.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface DELETE {
|
||||
String value();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.coph.nextusweb.server.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface GET {
|
||||
String value();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.coph.nextusweb.server.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface POST {
|
||||
String value();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.coph.nextusweb.server.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface PUT {
|
||||
String value();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package dev.coph.nextusweb.server.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface Route {
|
||||
String method();
|
||||
|
||||
String path();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package dev.coph.nextusweb.server.json;
|
||||
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
|
||||
|
||||
public final class Json {
|
||||
public static final ObjectMapper MAPPER = JsonMapper.builder()
|
||||
// .addModule(new JavaTimeModule())
|
||||
.build();
|
||||
|
||||
private Json() {}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package dev.coph.nextusweb.server.router;
|
||||
|
||||
import dev.coph.nextusweb.server.json.Json;
|
||||
import dev.coph.nextusweb.server.router.exception.BadRequestException;
|
||||
import io.netty.handler.codec.http.*;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import tools.jackson.core.JacksonException;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public final class Request {
|
||||
|
||||
private final FullHttpRequest raw;
|
||||
private final Map<String, String> pathParams;
|
||||
private Map<String, List<String>> queryParams;
|
||||
private JsonNode jsonCache;
|
||||
|
||||
public Request(FullHttpRequest raw, Map<String, String> pathParams) {
|
||||
this.raw = raw;
|
||||
this.pathParams = pathParams;
|
||||
}
|
||||
|
||||
public String pathParam(String name) {
|
||||
return pathParams.get(name);
|
||||
}
|
||||
|
||||
public String queryParam(String name) {
|
||||
if (queryParams == null) {
|
||||
queryParams = new QueryStringDecoder(raw.uri()).parameters();
|
||||
}
|
||||
var values = queryParams.get(name);
|
||||
return values == null || values.isEmpty() ? null : values.getFirst();
|
||||
}
|
||||
|
||||
public List<String> queryParams(String name) {
|
||||
if (queryParams == null) {
|
||||
queryParams = new QueryStringDecoder(raw.uri()).parameters();
|
||||
}
|
||||
return queryParams.getOrDefault(name, List.of());
|
||||
}
|
||||
|
||||
public String header(String name) {
|
||||
return raw.headers().get(name);
|
||||
}
|
||||
|
||||
public String body() {
|
||||
return raw.content().toString(CharsetUtil.UTF_8);
|
||||
}
|
||||
|
||||
public JsonNode json() {
|
||||
if (jsonCache == null) {
|
||||
try {
|
||||
byte[] bytes = new byte[raw.content().readableBytes()];
|
||||
raw.content().getBytes(raw.content().readerIndex(), bytes);
|
||||
if (bytes.length == 0) {
|
||||
jsonCache = Json.MAPPER.nullNode();
|
||||
} else {
|
||||
jsonCache = Json.MAPPER.readTree(bytes);
|
||||
}
|
||||
} catch (JacksonException e) {
|
||||
throw new BadRequestException("Invalid JSON: " + e.getOriginalMessage());
|
||||
}
|
||||
}
|
||||
return jsonCache;
|
||||
}
|
||||
|
||||
public <T> T jsonAs(Class<T> type) {
|
||||
try {
|
||||
byte[] bytes = new byte[raw.content().readableBytes()];
|
||||
raw.content().getBytes(raw.content().readerIndex(), bytes);
|
||||
return Json.MAPPER.readValue(bytes, type);
|
||||
} catch (JacksonException e) {
|
||||
throw new BadRequestException(
|
||||
"Could not deserialize body as " + type.getSimpleName() + ": " + e.getOriginalMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public HttpMethod method() {
|
||||
return raw.method();
|
||||
}
|
||||
|
||||
public String path() {
|
||||
return new QueryStringDecoder(raw.uri()).path();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package dev.coph.nextusweb.server.router;
|
||||
|
||||
import dev.coph.nextusweb.server.json.Json;
|
||||
import io.netty.handler.codec.http.*;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import tools.jackson.core.JacksonException;
|
||||
|
||||
public final class Response {
|
||||
|
||||
private int status = 200;
|
||||
private final HttpHeaders headers = new DefaultHttpHeaders();
|
||||
private byte[] body = new byte[0];
|
||||
|
||||
public Response status(int s) { this.status = s; return this; }
|
||||
|
||||
public Response header(String name, String value) {
|
||||
headers.set(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Response text(String s) {
|
||||
this.body = s.getBytes(CharsetUtil.UTF_8);
|
||||
headers.set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=utf-8");
|
||||
return this;
|
||||
}
|
||||
|
||||
public Response json(String json) {
|
||||
this.body = json.getBytes(CharsetUtil.UTF_8);
|
||||
headers.set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=utf-8");
|
||||
return this;
|
||||
}
|
||||
|
||||
public Response json(Object value) {
|
||||
try {
|
||||
this.body = Json.MAPPER.writeValueAsBytes(value);
|
||||
headers.set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=utf-8");
|
||||
} catch (JacksonException e) {
|
||||
throw new RuntimeException("JSON serialization failed", e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public int status() { return status; }
|
||||
public HttpHeaders headers() { return headers; }
|
||||
public byte[] body() { return body; }
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package dev.coph.nextusweb.server.router;
|
||||
|
||||
import io.netty.handler.codec.http.HttpMethod;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public final class Router {
|
||||
|
||||
private final Node root = new Node();
|
||||
private final List<BiConsumer<Request, Response>> middlewares = new ArrayList<>();
|
||||
|
||||
public Router use(BiConsumer<Request, Response> middleware) {
|
||||
middlewares.add(middleware);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Router get(String path, Handler h) {
|
||||
return register(HttpMethod.GET, path, h);
|
||||
}
|
||||
|
||||
public Router register(HttpMethod method, String path, Handler h) {
|
||||
Node node = root;
|
||||
for (String segment : split(path)) {
|
||||
if (segment.startsWith("{") && segment.endsWith("}")) {
|
||||
if (node.paramChild == null) {
|
||||
node.paramChild = new Node();
|
||||
node.paramName = segment.substring(1, segment.length() - 1);
|
||||
}
|
||||
node = node.paramChild;
|
||||
} else if (segment.equals("*")) {
|
||||
if (node.wildcardChild == null) node.wildcardChild = new Node();
|
||||
node = node.wildcardChild;
|
||||
} else {
|
||||
node = node.children.computeIfAbsent(segment, k -> new Node());
|
||||
}
|
||||
}
|
||||
node.handlers.put(method, h);
|
||||
return this;
|
||||
}
|
||||
|
||||
private static List<String> split(String path) {
|
||||
List<String> out = new ArrayList<>();
|
||||
int start = path.startsWith("/") ? 1 : 0;
|
||||
for (int i = start; i < path.length(); i++) {
|
||||
if (path.charAt(i) == '/') {
|
||||
if (i > start) out.add(path.substring(start, i));
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
if (start < path.length()) out.add(path.substring(start));
|
||||
return out;
|
||||
}
|
||||
|
||||
public Router post(String path, Handler h) {
|
||||
return register(HttpMethod.POST, path, h);
|
||||
}
|
||||
|
||||
public Router put(String path, Handler h) {
|
||||
return register(HttpMethod.PUT, path, h);
|
||||
}
|
||||
|
||||
public Router delete(String path, Handler h) {
|
||||
return register(HttpMethod.DELETE, path, h);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve den Pfad zu einem Handler. Liefert:
|
||||
* - Match wenn Pfad + Methode passen
|
||||
* - MethodNotAllowed wenn der Pfad existiert, aber nicht für diese Methode
|
||||
* - NotFound wenn der Pfad gar nicht registriert ist
|
||||
*/
|
||||
public Resolution resolve(HttpMethod method, String path) {
|
||||
Map<String, String> params = new HashMap<>(4);
|
||||
Node node = root;
|
||||
for (String segment : split(path)) {
|
||||
Node next = node.children.get(segment);
|
||||
if (next != null) {
|
||||
node = next;
|
||||
} else if (node.paramChild != null) {
|
||||
params.put(node.paramName, segment);
|
||||
node = node.paramChild;
|
||||
} else if (node.wildcardChild != null) {
|
||||
node = node.wildcardChild;
|
||||
} else {
|
||||
return new Resolution.NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
Handler h = node.handlers.get(method);
|
||||
if (h != null) {
|
||||
return new Resolution.Match(h, params);
|
||||
}
|
||||
|
||||
// Pfad existiert, aber nicht für diese Methode
|
||||
if (!node.handlers.isEmpty()) {
|
||||
return new Resolution.MethodNotAllowed(
|
||||
Collections.unmodifiableSet(node.handlers.keySet()));
|
||||
}
|
||||
|
||||
return new Resolution.NotFound();
|
||||
}
|
||||
|
||||
public List<BiConsumer<Request, Response>> middlewares() {
|
||||
return middlewares;
|
||||
}
|
||||
|
||||
public sealed interface Resolution {
|
||||
record Match(Handler handler, Map<String, String> pathParams) implements Resolution {
|
||||
}
|
||||
|
||||
record MethodNotAllowed(Set<HttpMethod> allowedMethods) implements Resolution {
|
||||
}
|
||||
|
||||
record NotFound() implements Resolution {
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Handler {
|
||||
void handle(Request req, Response res) throws Exception;
|
||||
}
|
||||
|
||||
private static final class Node {
|
||||
final Map<String, Node> children = new ConcurrentHashMap<>();
|
||||
final Map<HttpMethod, Handler> handlers = new ConcurrentHashMap<>();
|
||||
Node paramChild; // für {id}
|
||||
String paramName;
|
||||
Node wildcardChild; // für *
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package dev.coph.nextusweb.server.router.exception;
|
||||
|
||||
public final class BadRequestException extends RuntimeException {
|
||||
public BadRequestException(String message) { super(message); }
|
||||
}
|
||||
Reference in New Issue
Block a user