package com.lanyuanxiaoyao.service.forest;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

/**
 * @author lanyuanxiaoyao
 * @date 2024-01-24
 */
public class TestServer {
    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/success", new SuccessHandler());
        server.createContext("/error", new ErrorHandler());
        server.start();
    }

    public static class SuccessHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            exchange.sendResponseHeaders(200, 0);
            OutputStream body = exchange.getResponseBody();
            body.write("Success".getBytes());
            body.close();
        }
    }

    public static class ErrorHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            exchange.sendResponseHeaders(500, 0);
            OutputStream body = exchange.getResponseBody();
            body.write("Error".getBytes());
            body.close();
        }
    }
}
