package ipsteric;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import ipsteric.httpserver.Router;
import ipsteric.httpserver.Router.Request;
import ipsteric.httpserver.Router.Response;
import ipsteric.httpserver.Router.Response.Status;

@WebListener
public class App implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		Router router = new Router()
			.get("/ip/:ip", new Router.Handler() {
				@Override
				public void handle(Request request, Response response) {
					Map<String, Object> content = new LinkedHashMap<>();
					content.put("ip", request.pathparam("ip"));
					content.put("country", "IT");
					
					response.status(Status.ok)
						.body(content)
						.asJson();
				}
			});

		router.install(sce.getServletContext(), "/api/v1/*");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		
	}

}
