Skip to content

Commit 9f075f6

Browse files
authored
Merge pull request #8 from lmangani/thread-options
Thread Options
2 parents 99b74d1 + ecf9362 commit 9f075f6

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

docs/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ LOAD httpserver;
3333

3434
### 🔌 Usage
3535
Start the HTTP server providing the `host`, `port` and `auth` parameters.<br>
36-
> If you want no authhentication, just pass an empty string.
36+
> * If you want no authentication, just pass an empty string as parameter.<br>
37+
> * If you want the API run in foreground set `DUCKDB_HTTPSERVER_FOREGROUND=1`
3738
3839
#### Basic Auth
3940
```sql

src/httpserver_extension.cpp

+31-2
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,41 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
383383
});
384384

385385
string host_str = host.GetString();
386-
global_state.server_thread = make_uniq<std::thread>([host_str, port]() {
386+
387+
const char* run_in_same_thread_env = std::getenv("DUCKDB_HTTPSERVER_FOREGROUND");
388+
bool run_in_same_thread = (run_in_same_thread_env != nullptr && std::string(run_in_same_thread_env) == "1");
389+
390+
if (run_in_same_thread) {
391+
#ifdef _WIN32
392+
throw IOException("Foreground mode not yet supported on WIN32 platforms.");
393+
#else
394+
// POSIX signal handler for SIGINT (Linux/macOS)
395+
signal(SIGINT, [](int) {
396+
if (global_state.server) {
397+
global_state.server->stop();
398+
}
399+
global_state.is_running = false; // Update the running state
400+
});
401+
402+
// Run the server in the same thread
387403
if (!global_state.server->listen(host_str.c_str(), port)) {
388404
global_state.is_running = false;
389405
throw IOException("Failed to start HTTP server on " + host_str + ":" + std::to_string(port));
390406
}
391-
});
407+
#endif
408+
409+
// The server has stopped (due to CTRL-C or other reasons)
410+
global_state.is_running = false;
411+
} else {
412+
// Run the server in a dedicated thread (default)
413+
global_state.server_thread = make_uniq<std::thread>([host_str, port]() {
414+
if (!global_state.server->listen(host_str.c_str(), port)) {
415+
global_state.is_running = false;
416+
throw IOException("Failed to start HTTP server on " + host_str + ":" + std::to_string(port));
417+
}
418+
});
419+
}
420+
392421
}
393422

394423
void HttpServerStop() {

0 commit comments

Comments
 (0)