From 950f79f957f7340c1bdc3b5fa516c2047b67da48 Mon Sep 17 00:00:00 2001 From: Lorenzo Mangani Date: Mon, 16 Dec 2024 18:10:16 +0100 Subject: [PATCH] Add DUCKDB_HTTPSERVER_BASEPATH Implements new ENV variable to control the API basepath: `DUCKDB_HTTPSERVER_BASEPATH=/custom` --- src/httpserver_extension.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/httpserver_extension.cpp b/src/httpserver_extension.cpp index 4b5eac0..62ae297 100644 --- a/src/httpserver_extension.cpp +++ b/src/httpserver_extension.cpp @@ -325,8 +325,16 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t global_state.is_running = true; global_state.auth_token = auth.GetString(); + // Custom basepath, defaults to root / + const char* base_path_env = std::getenv("DUCKDB_HTTPSERVER_BASEPATH"); + std::string base_path = "/"; + + if (base_path_env && base_path_env[0] == '/' && strlen(base_path_env) > 1) { + base_path = std::string(base_path_env); + } + // CORS Preflight - global_state.server->Options("/", + global_state.server->Options(base_path, [](const duckdb_httplib_openssl::Request& /*req*/, duckdb_httplib_openssl::Response& res) { res.set_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS"); res.set_header("Content-Type", "text/html; charset=utf-8"); @@ -341,8 +349,8 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t global_state.allocator = make_uniq(); // Handle GET and POST requests - global_state.server->Get("/", HandleHttpRequest); - global_state.server->Post("/", HandleHttpRequest); + global_state.server->Get(base_path, HandleHttpRequest); + global_state.server->Post(base_path, HandleHttpRequest); // Health check endpoint global_state.server->Get("/ping", [](const duckdb_httplib_openssl::Request& req, duckdb_httplib_openssl::Response& res) {