1 Commits

Author SHA1 Message Date
Yves
7a619f806c Allow to configure listening interface 2025-03-12 15:26:03 +01:00
5 changed files with 28 additions and 6 deletions

View File

@@ -64,7 +64,8 @@ bool HttpServer::IsRunningOnMachine(ClientContext &context) {
}
const auto local_port = GetLocalPort(context);
auto local_url = StringUtil::Format("http://localhost:%d", local_port);
const auto local_host = GetLocalHost(context);
auto local_url = StringUtil::Format("http://%s:%d", local_host, local_port);
httplib::Client client(local_url);
return client.Get("/info");
@@ -94,19 +95,22 @@ const HttpServer &HttpServer::Start(ClientContext &context, bool *was_started) {
const auto remote_url = GetRemoteUrl(context);
const auto port = GetLocalPort(context);
const auto host = GetLocalHost(context);
auto server = GetInstance(context);
server->DoStart(port, remote_url);
server->DoStart(port, host, remote_url);
return *server;
}
void HttpServer::DoStart(const uint16_t _local_port,
const std::string &_local_host,
const std::string &_remote_url) {
if (Started()) {
throw std::runtime_error("HttpServer already started");
}
local_port = _local_port;
local_url = StringUtil::Format("http://localhost:%d", local_port);
local_host = _local_host;
local_url = StringUtil::Format("http://%s:%d", local_host, local_port);
remote_url = _remote_url;
user_agent =
StringUtil::Format("duckdb-ui/%s-%s(%s)", DuckDB::LibraryVersion(),
@@ -146,10 +150,11 @@ void HttpServer::DoStop() {
ddb_instance.reset();
remote_url = "";
local_port = 0;
local_host = "";
}
std::string HttpServer::LocalUrl() const {
return StringUtil::Format("http://localhost:%d/", local_port);
return StringUtil::Format("http://%s:%d/", local_host, local_port);
}
shared_ptr<DatabaseInstance> HttpServer::LockDatabaseInstance() {
@@ -185,7 +190,7 @@ void HttpServer::Run() {
const httplib::ContentReader &content_reader) {
HandleTokenize(req, res, content_reader);
});
server.listen("localhost", local_port);
server.listen(local_host, local_port);
}
void HttpServer::HandleGetInfo(const httplib::Request &req,

View File

@@ -40,7 +40,8 @@ private:
friend class Watcher;
// Lifecycle
void DoStart(const uint16_t local_port, const std::string &remote_url);
void DoStart(const uint16_t local_port, const std::string &local_host,
const std::string &remote_url);
void DoStop();
void Run();
void UpdateDatabaseInstance(shared_ptr<DatabaseInstance> context_db);
@@ -69,6 +70,7 @@ private:
shared_ptr<DatabaseInstance> LockDatabaseInstance();
uint16_t local_port;
std::string local_host;
std::string local_url;
std::string remote_url;
weak_ptr<DatabaseInstance> ddb_instance;

View File

@@ -5,6 +5,8 @@
#define UI_LOCAL_PORT_SETTING_NAME "ui_local_port"
#define UI_LOCAL_PORT_SETTING_DEFAULT 4213
#define UI_LOCAL_HOST_SETTING_NAME "ui_local_host"
#define UI_LOCAL_HOST_SETTING_DEFAULT "localhost"
#define UI_REMOTE_URL_SETTING_NAME "ui_remote_url"
#define UI_REMOTE_URL_SETTING_DEFAULT "https://ui.duckdb.org"
#define UI_POLLING_INTERVAL_SETTING_NAME "ui_polling_interval"
@@ -27,6 +29,7 @@ T GetSetting(const ClientContext &context, const char *setting_name) {
std::string GetRemoteUrl(const ClientContext &);
uint16_t GetLocalPort(const ClientContext &);
std::string GetLocalHost(const ClientContext &);
uint32_t GetPollingInterval(const ClientContext &);
} // namespace duckdb

View File

@@ -15,6 +15,10 @@ uint16_t GetLocalPort(const ClientContext &context) {
return internal::GetSetting<uint16_t>(context, UI_LOCAL_PORT_SETTING_NAME);
}
std::string GetLocalHost(const ClientContext &context) {
return internal::GetSetting<std::string>(context, UI_LOCAL_HOST_SETTING_NAME);
}
uint32_t GetPollingInterval(const ClientContext &context) {
return internal::GetSetting<uint32_t>(context,
UI_POLLING_INTERVAL_SETTING_NAME);

View File

@@ -93,6 +93,14 @@ static void LoadInternal(DatabaseInstance &instance) {
LogicalType::USMALLINT, Value::USMALLINT(default_port));
}
{
auto default_host = GetEnvOrDefault(UI_LOCAL_HOST_SETTING_NAME,
UI_LOCAL_HOST_SETTING_DEFAULT);
config.AddExtensionOption(UI_LOCAL_HOST_SETTING_NAME,
"Local host on which the UI server listens",
LogicalType::VARCHAR, Value(default_host));
}
{
auto def = GetEnvOrDefault(UI_REMOTE_URL_SETTING_NAME,
UI_REMOTE_URL_SETTING_DEFAULT);