remove tcp boilerplate, integrate with http
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package org.lumijiez;
|
||||
|
||||
import org.lumijiez.core.http.HttpServer;
|
||||
import org.lumijiez.logging.Logger;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -1,20 +1,29 @@
|
||||
package org.lumijiez.core.http;
|
||||
|
||||
import org.lumijiez.core.routing.Router;
|
||||
import org.lumijiez.core.tcp.TcpServer;
|
||||
import org.lumijiez.logging.Logger;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class HttpServer extends TcpServer {
|
||||
public class HttpServer {
|
||||
private boolean running;
|
||||
private final int port;
|
||||
private ServerSocket serverSocket;
|
||||
private final ExecutorService threadPool;
|
||||
private final Router router;
|
||||
|
||||
public HttpServer(int port) {
|
||||
super(port);
|
||||
this.running = false;
|
||||
this.port = port;
|
||||
this.router = new Router();
|
||||
this.threadPool = Executors.newCachedThreadPool();
|
||||
}
|
||||
|
||||
public void GET(String path, HttpHandler handler) {
|
||||
@@ -25,7 +34,33 @@ public class HttpServer extends TcpServer {
|
||||
router.addRoute("POST", path, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
try {
|
||||
serverSocket = new ServerSocket(port);
|
||||
running = true;
|
||||
|
||||
Logger.info("HTTP", "Server started on port " + port);
|
||||
|
||||
while (running) {
|
||||
try {
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
|
||||
Logger.info("HTTP", "Client connected " + clientSocket.getInetAddress());
|
||||
|
||||
threadPool.submit(() -> handleClient(clientSocket));
|
||||
} catch (IOException e) {
|
||||
if (running) {
|
||||
Logger.error("HTTP", "Error accepting client connection: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Logger.error("HTTP", "Error starting server: " + e.getMessage());
|
||||
} finally {
|
||||
stop();
|
||||
}
|
||||
}
|
||||
|
||||
protected void handleClient(Socket clientSocket) {
|
||||
try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
|
||||
@@ -35,7 +70,24 @@ public class HttpServer extends TcpServer {
|
||||
|
||||
router.handleRequest(request, response);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error handling client: " + e.getMessage());
|
||||
Logger.error("HTTP", "Error handling client: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
running = false;
|
||||
if (serverSocket != null) {
|
||||
try {
|
||||
serverSocket.close();
|
||||
Logger.info("HTTP", "Server stopped");
|
||||
} catch (IOException e) {
|
||||
Logger.error("HTTP", "Error stopping server: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
threadPool.shutdownNow();
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package org.lumijiez.core.tcp;
|
||||
import org.lumijiez.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public abstract class TcpServer {
|
||||
private final int port;
|
||||
private boolean running;
|
||||
private ServerSocket serverSocket;
|
||||
private final ExecutorService threadPool;
|
||||
|
||||
public TcpServer(int port) {
|
||||
this.port = port;
|
||||
this.running = false;
|
||||
this.threadPool = Executors.newCachedThreadPool();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
serverSocket = new ServerSocket(port);
|
||||
running = true;
|
||||
System.out.println("Server started on port " + port);
|
||||
|
||||
while (running) {
|
||||
try {
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
|
||||
System.out.println("New client connected: " + clientSocket.getInetAddress());
|
||||
|
||||
threadPool.submit(() -> handleClient(clientSocket));
|
||||
} catch (IOException e) {
|
||||
if (running) {
|
||||
System.out.println("Error accepting client connection: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error starting server: " + e.getMessage());
|
||||
} finally {
|
||||
stop();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void handleClient(Socket clientSocket);
|
||||
|
||||
public void stop() {
|
||||
running = false;
|
||||
if (serverSocket != null) {
|
||||
try {
|
||||
serverSocket.close();
|
||||
System.out.println("Server stopped.");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error stopping server: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
threadPool.shutdownNow();
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class Logger {
|
||||
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
|
||||
public enum LogLevel {
|
||||
DEBUG, INFO, WARN, ERROR
|
||||
|
||||
Reference in New Issue
Block a user