import java.net.*; import java.nio.*; import java.nio.channels.*; import java.io.*; import java.nio.charset.*; import java.util.*; import java.nio.file.*; import java.util.stream.*; import java.util.concurrent.*; public class Webserver { public static void main(String[] args) { try { start(8000); } catch (IOException e) { System.err.println("Ajaj"); } } public static void start(int port) throws IOException { ExecutorService e = Executors.newFixedThreadPool(20); try { ServerSocketChannel s = ServerSocketChannel.open(); s.bind(new InetSocketAddress(port)); while (true) { SocketChannel sc = s.accept(); e.execute(() -> handleGet(sc)); } } finally { e.shutdown(); } } public static void handleGet(SocketChannel sc) { String resp = wrap(Html.index); ByteBuffer index = ByteBuffer.wrap(resp.getBytes()); try { sc.write(index); sc.close(); } catch (IOException e) { } } public static String wrap(Html h) { String content = "" + Html.html(HtmlTag.HTML, Html.html(HtmlTag.BODY, h)); return String.format("HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\nContent-Encoding: identity\r\nContent-Length: %d\r\n\r\n%s", content.getBytes().length, content); } }