default:
System.err.println("--net must be one of main, test or regtest");
return;
}
HttpServer server = createServer(portFlag, keystoreFlag, options);
// Where will we store our projects and received pledges?
if (options.has(dirFlag))
AppDirectory.overrideAppDir(Paths.get(options.valueOf(dirFlag)));
Path appDir = AppDirectory.initAppDir("lighthouse-server"); // Create dir if necessary.
WalletAppKit kit = new WalletAppKit(params, appDir.toFile(), "lighthouse-server") {
{
walletFactory = PledgingWallet::new;
}
};
if (kit.isChainFileLocked()) {
log.error("App is already running. Please terminate the other instance or use a different directory (--dir=...)");
return;
}
int minPeersSupportingGetUTXO = 2; // Increase when the feature eventually rolls out to the network.
if (options.has("local-node") || params == RegTestParams.get()) {
kit.connectToLocalHost();
minPeersSupportingGetUTXO = 1; // Only local matters.
}
// Eventually take this out when getutxo is merged, released and people have upgraded.
kit.setBlockingStartup(true)
.setAutoSave(true)
.setAutoStop(true)
.setUserAgent("Lighthouse Server", "1.0")
.startAsync()
.awaitRunning();
log.info("bitcoinj initialised");
// Don't start up fully until we're properly set up. Eventually this can go away.
// TODO: Make this also check for NODE_GETUTXOS flag.
log.info("Waiting to find a peer that supports getutxo");
kit.peerGroup().waitForPeersOfVersion(minPeersSupportingGetUTXO, GetUTXOsMessage.MIN_PROTOCOL_VERSION).get();
log.info("Found ... starting web server on port {}", portFlag.value(options));
// This app is mostly single threaded. It handles all requests and state changes on a single thread.
// Speed should ideally not be an issue, as the backend blocks only rarely. If it's a problem then
// we'll have to split the backend thread from the http server thread.
AffinityExecutor.ServiceAffinityExecutor executor = new AffinityExecutor.ServiceAffinityExecutor("server");
server.setExecutor(executor);
LighthouseBackend backend = new LighthouseBackend(SERVER, kit.peerGroup(), kit.chain(), (PledgingWallet) kit.wallet(), executor);
backend.setMinPeersForUTXOQuery(minPeersSupportingGetUTXO);
server.createContext(LHUtils.HTTP_PATH_PREFIX, new ProjectHandler(backend));
server.createContext("/", exchange -> {
log.warn("404 Not Found: {}", exchange.getRequestURI());
exchange.sendResponseHeaders(404, -1);
});
server.start();
}