view src/luan/modules/http/LuanDomainHandler.java @ 1578:c922446f53aa

immutable threading
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 08 Feb 2021 14:16:19 -0700
parents 8fbcc4747091
children 46cf5137cb6b
line wrap: on
line source

package luan.modules.http;

import goodjava.webserver.Request;
import goodjava.webserver.Response;
import goodjava.webserver.Handler;
import goodjava.webserver.handlers.DomainHandler;
import luan.Luan;
import luan.LuanTable;
import luan.LuanFunction;
import luan.LuanException;
import luan.modules.logging.LuanLogger;


public class LuanDomainHandler implements Handler, DomainHandler.Factory {

	private final Luan luanInit;
	private final DomainHandler domainHandler = new DomainHandler(this);

	public LuanDomainHandler(Luan luanInit) {
		LuanLogger.initThreadLogging();
		this.luanInit = new Luan(luanInit);
	}

	@Override public Handler newHandler(String domain) {
		Luan luan = newLuan(domain);
		return new LuanHandler(luan,domain);
	}

	protected Luan newLuan(final String domain) {
		Luan luan = new Luan(luanInit);
		LuanFunction reset_luan = new LuanFunction() {
			@Override public Object call(Luan luan,Object[] args) {
				domainHandler.removeHandler(domain);
				return LuanFunction.NOTHING;
			}
		};
		try {
			LuanTable Http = (LuanTable)luan.require("luan:http/Http.luan");
			Http.put( luan, "domain", domain );
			Http.put( luan, "reset_luan", reset_luan );
		} catch(LuanException e) {
			throw new RuntimeException(e);
		}
		return luan;
	}

	@Override public Response handle(Request request) {
		return domainHandler.handle(request);
	}
}