full middleware support
This commit is contained in:
@@ -1,16 +1,26 @@
|
|||||||
package org.lumijiez;
|
package org.lumijiez;
|
||||||
|
|
||||||
|
import org.lumijiez.core.config.ServerConfig;
|
||||||
import org.lumijiez.core.http.HttpServer;
|
import org.lumijiez.core.http.HttpServer;
|
||||||
|
import org.lumijiez.core.http.HttpStatus;
|
||||||
|
import org.lumijiez.logging.Logger;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
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();
|
server.start();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,26 @@ public class ServerConfig {
|
|||||||
this.threadPoolSize = builder.threadPoolSize;
|
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 {
|
public static class Builder {
|
||||||
private int port = 8080;
|
private int port = 8080;
|
||||||
private int keepAliveTimeout = 30000;
|
private int keepAliveTimeout = 30000;
|
||||||
@@ -30,5 +50,25 @@ public class ServerConfig {
|
|||||||
public ServerConfig build() {
|
public ServerConfig build() {
|
||||||
return new ServerConfig(this);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ public class HttpResponse {
|
|||||||
this.out = out;
|
this.out = out;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendResponse(int statusCode, String message) throws IOException {
|
public void sendResponse(HttpStatus status, String message) throws IOException {
|
||||||
Logger.info("HTTP", "Outgoing: " + statusCode + " " + message);
|
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("\r\n");
|
||||||
|
|
||||||
out.write("Content-Type: text/plain");
|
out.write("Content-Type: text/plain");
|
||||||
@@ -34,13 +34,4 @@ public class HttpResponse {
|
|||||||
|
|
||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getStatusText(int code) {
|
|
||||||
return switch (code) {
|
|
||||||
case 200 -> "OK";
|
|
||||||
case 404 -> "Not Found";
|
|
||||||
case 500 -> "Internal Server Error";
|
|
||||||
default -> "Unknown";
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package org.lumijiez.core.http;
|
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.core.routing.Router;
|
||||||
import org.lumijiez.logging.Logger;
|
import org.lumijiez.logging.Logger;
|
||||||
|
|
||||||
@@ -16,18 +18,24 @@ public class HttpServer {
|
|||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
private final ExecutorService threadPool;
|
private final ExecutorService threadPool;
|
||||||
private final Router router;
|
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;
|
public HttpServer(ServerConfig config) {
|
||||||
private static final int MAX_REQUESTS_PER_CONNECTION = 1000;
|
|
||||||
private static final int BUFFER_SIZE = 8192;
|
|
||||||
|
|
||||||
public HttpServer(int port) {
|
|
||||||
this.running = false;
|
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.router = new Router();
|
||||||
this.threadPool = Executors.newCachedThreadPool();
|
this.threadPool = Executors.newCachedThreadPool();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addMiddleware(Middleware middleware) {
|
||||||
|
this.router.addMiddleware(middleware);
|
||||||
|
}
|
||||||
|
|
||||||
public void GET(String path, HttpHandler handler) {
|
public void GET(String path, HttpHandler handler) {
|
||||||
router.addRoute(HttpMethod.GET, path, handler);
|
router.addRoute(HttpMethod.GET, path, handler);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package org.lumijiez.core.routing;
|
package org.lumijiez.core.routing;
|
||||||
|
|
||||||
import org.lumijiez.core.http.HttpHandler;
|
import org.lumijiez.core.http.*;
|
||||||
import org.lumijiez.core.http.HttpMethod;
|
|
||||||
import org.lumijiez.core.http.HttpRequest;
|
|
||||||
import org.lumijiez.core.http.HttpResponse;
|
|
||||||
import org.lumijiez.core.middleware.Chain;
|
import org.lumijiez.core.middleware.Chain;
|
||||||
import org.lumijiez.core.middleware.Middleware;
|
import org.lumijiez.core.middleware.Middleware;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user