diff --git a/src/main/java/org/lumijiez/Main.java b/src/main/java/org/lumijiez/Main.java index 857bcb9..1fb9de6 100644 --- a/src/main/java/org/lumijiez/Main.java +++ b/src/main/java/org/lumijiez/Main.java @@ -1,16 +1,26 @@ package org.lumijiez; +import org.lumijiez.core.config.ServerConfig; import org.lumijiez.core.http.HttpServer; +import org.lumijiez.core.http.HttpStatus; +import org.lumijiez.logging.Logger; public class Main { public static void main(String[] args) { - HttpServer server = new HttpServer(8080); + ServerConfig config = new ServerConfig.Builder() + .port(8080) + .keepAliveTimeout(30000) + .build(); - server.GET("/hello", (req, res) -> res.sendResponse(200, "Hello, World!")); + HttpServer server = new HttpServer(config); - server.GET("/goodbye", (req, res) -> res.sendResponse(200, "Goodbye, World!")); + server.addMiddleware((req, res, chain) -> { + Logger.info("MIDDLEWARE", "Request: " + req.getMethod() + " " + req.getPath()); + chain.next(req, res); + }); - server.POST("/data", (req, res) -> res.sendResponse(200, "Data received")); + server.GET("/hello", (req, res) -> + res.sendResponse(HttpStatus.OK, "Hello, World!")); server.start(); } diff --git a/src/main/java/org/lumijiez/core/config/ServerConfig.java b/src/main/java/org/lumijiez/core/config/ServerConfig.java index 2a44d02..912fd72 100644 --- a/src/main/java/org/lumijiez/core/config/ServerConfig.java +++ b/src/main/java/org/lumijiez/core/config/ServerConfig.java @@ -15,6 +15,26 @@ public class ServerConfig { this.threadPoolSize = builder.threadPoolSize; } + public int getPort() { + return port; + } + + public int getKeepAliveTimeout() { + return keepAliveTimeout; + } + + public int getMaxRequestsPerConnection() { + return maxRequestsPerConnection; + } + + public int getBufferSize() { + return bufferSize; + } + + public int getThreadPoolSize() { + return threadPoolSize; + } + public static class Builder { private int port = 8080; private int keepAliveTimeout = 30000; @@ -30,5 +50,25 @@ public class ServerConfig { public ServerConfig build() { return new ServerConfig(this); } + + public Builder keepAliveTimeout(int i) { + this.keepAliveTimeout = i; + return this; + } + + public Builder maxRequestsPerConnection(int i) { + this.maxRequestsPerConnection = i; + return this; + } + + public Builder bufferSize(int i) { + this.bufferSize = i; + return this; + } + + public Builder threadPoolSize(int i) { + this.threadPoolSize = i; + return this; + } } } diff --git a/src/main/java/org/lumijiez/core/http/HttpResponse.java b/src/main/java/org/lumijiez/core/http/HttpResponse.java index 7ccf5a0..024897c 100644 --- a/src/main/java/org/lumijiez/core/http/HttpResponse.java +++ b/src/main/java/org/lumijiez/core/http/HttpResponse.java @@ -13,10 +13,10 @@ public class HttpResponse { this.out = out; } - public void sendResponse(int statusCode, String message) throws IOException { - Logger.info("HTTP", "Outgoing: " + statusCode + " " + message); + public void sendResponse(HttpStatus status, String message) throws IOException { + Logger.info("HTTP", "Outgoing: " + status.getCode() + " " + message); - out.write("HTTP/1.1 " + statusCode + " " + getStatusText(statusCode)); + out.write("HTTP/1.1 " + status.getCode() + " " + status.getMessage()); out.write("\r\n"); out.write("Content-Type: text/plain"); @@ -34,13 +34,4 @@ public class HttpResponse { out.flush(); } - - private String getStatusText(int code) { - return switch (code) { - case 200 -> "OK"; - case 404 -> "Not Found"; - case 500 -> "Internal Server Error"; - default -> "Unknown"; - }; - } } diff --git a/src/main/java/org/lumijiez/core/http/HttpServer.java b/src/main/java/org/lumijiez/core/http/HttpServer.java index f6028a1..f59004f 100644 --- a/src/main/java/org/lumijiez/core/http/HttpServer.java +++ b/src/main/java/org/lumijiez/core/http/HttpServer.java @@ -1,5 +1,7 @@ package org.lumijiez.core.http; +import org.lumijiez.core.config.ServerConfig; +import org.lumijiez.core.middleware.Middleware; import org.lumijiez.core.routing.Router; import org.lumijiez.logging.Logger; @@ -16,18 +18,24 @@ public class HttpServer { private ServerSocket serverSocket; private final ExecutorService threadPool; private final Router router; + private static int KEEP_ALIVE_TIMEOUT = 30000; + private static int MAX_REQUESTS_PER_CONNECTION = 1000; + private static int BUFFER_SIZE = 8192; - private static final int KEEP_ALIVE_TIMEOUT = 30000; - private static final int MAX_REQUESTS_PER_CONNECTION = 1000; - private static final int BUFFER_SIZE = 8192; - - public HttpServer(int port) { + public HttpServer(ServerConfig config) { this.running = false; - this.port = port; + this.port = config.getPort(); + KEEP_ALIVE_TIMEOUT = config.getKeepAliveTimeout(); + MAX_REQUESTS_PER_CONNECTION = config.getMaxRequestsPerConnection(); + BUFFER_SIZE = config.getBufferSize(); this.router = new Router(); this.threadPool = Executors.newCachedThreadPool(); } + public void addMiddleware(Middleware middleware) { + this.router.addMiddleware(middleware); + } + public void GET(String path, HttpHandler handler) { router.addRoute(HttpMethod.GET, path, handler); } diff --git a/src/main/java/org/lumijiez/core/routing/Router.java b/src/main/java/org/lumijiez/core/routing/Router.java index da26e9d..5de8f19 100644 --- a/src/main/java/org/lumijiez/core/routing/Router.java +++ b/src/main/java/org/lumijiez/core/routing/Router.java @@ -1,9 +1,6 @@ package org.lumijiez.core.routing; -import org.lumijiez.core.http.HttpHandler; -import org.lumijiez.core.http.HttpMethod; -import org.lumijiez.core.http.HttpRequest; -import org.lumijiez.core.http.HttpResponse; +import org.lumijiez.core.http.*; import org.lumijiez.core.middleware.Chain; import org.lumijiez.core.middleware.Middleware;