view src/luan/modules/http/LuanDomainHandler.java @ 1685:46cf5137cb6b

misc fixes
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 08 Jun 2022 20:13:54 -0600
parents c922446f53aa
children 357daf580951
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.LuanRuntimeException;
import luan.modules.logging.LuanLogger;


public class LuanDomainHandler implements Handler, DomainHandler.Factory {
	private final DomainHandler domainHandler = new DomainHandler(this);
	private final LuanFunction init;

	public LuanDomainHandler(LuanFunction init) throws LuanException {
		if(init==null) throw new NullPointerException("init required");
		LuanLogger.initThreadLogging();
		this.init = init;
	}

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

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

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