package org.eck; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.eck.middlewares.LocomotiveMiddleware; import org.eck.middlewares.LocomotiveRestMiddleware; import org.eck.path.RouteParser; public class Locomotive { private static final String GET = "GET"; private static final String PUT = "PUT"; private static final String POST = "POST"; private static final String DELETE = "DELETE"; private static final String PATCH = "PATCH"; private int port; private List<String> routeWagons = new ArrayList<String>(); private Map<String, Map<String, Wagon>> wagons = new HashMap<>(); private Channel ch; private NioEventLoopGroup bossGroup; private NioEventLoopGroup workerGroup; private List<LocomotiveMiddleware> middlewares; public Locomotive(int port) { super(); this.port = port; // Initialize and add the default middlewares middlewares = new ArrayList<LocomotiveMiddleware>(); } public void get(String url, Wagon wagon) { addWagon(GET, url, wagon); } public void put(String url, Wagon wagon) { addWagon(PUT, url, wagon); } public void post(String url, Wagon wagon) { addWagon(POST, url, wagon); } public void delete(String url, Wagon wagon) { addWagon(DELETE, url, wagon); } public void patch(String url, Wagon wagon) { addWagon(PATCH, url, wagon); } public String getUriPattern(String uri) { for (String route : routeWagons) { if (RouteParser.macthes(route, uri)) { return route; } } return null; } public Wagon getWagon(String method, String url) { if (wagons.get(method) != null) { return wagons.get(method).get(url); } return null; } private void addWagon(String method, String url, Wagon wagon) { if (wagons.get(method) == null) { wagons.put(method, new HashMap<String, Wagon>()); } if (url.contains(":")) { routeWagons.add(url); } wagons.get(method).put(url, wagon); } public void shutdown() { try { ch.close().sync(); bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } catch (InterruptedException e) { throw new RuntimeException(e); } } public void boot() { middlewares.add(new LocomotiveRestMiddleware(this)); // Configure the server. bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new LocomotiveInitializer(this)); ch = b.bind(port).sync().channel(); System.out.println("Listening one http://127.0.0.1:" + port + '/'); } catch (Exception e) { throw new RuntimeException(e); } } public List<LocomotiveMiddleware> middlewares() { return middlewares; } public void addMiddleware(LocomotiveMiddleware middleware) { this.middlewares.add(middleware); } public static void main(String[] args) { Locomotive locomotive = new Locomotive(8080); locomotive.get("/user", (req, resp) -> { resp.append("Hello sire"); }); locomotive.boot(); } }