Skip to content

Commit

Permalink
Add DUCKDB_HTTPSERVER_BASEPATH (#23)
Browse files Browse the repository at this point in the history
Implements new ENV variable to control the API basepath:

`DUCKDB_HTTPSERVER_BASEPATH=/custom`
  • Loading branch information
lmangani authored Dec 16, 2024
1 parent a95d01a commit 547bbf1
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/httpserver_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -341,8 +349,8 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
global_state.allocator = make_uniq<Allocator>();

// 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) {
Expand Down

0 comments on commit 547bbf1

Please sign in to comment.