Skip to content

Commit a2e1f5d

Browse files
- Customizable page size
- Customizable thread numbers - Fixed page navigation
1 parent 6f80a74 commit a2e1f5d

4 files changed

Lines changed: 35 additions & 15 deletions

File tree

cmds/server.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "command_base.h"
33
#include "../func/server/server.h"
44
#include <iostream>
5-
5+
#include <thread>
66
class ServerCommand : public Command {
77
public:
88
const char* name() const override {
@@ -35,10 +35,24 @@ class ServerCommand : public Command {
3535
} else if (ctx.flags.count("p")) {
3636
port = std::stoi(ctx.flags.at("p"));
3737
}
38+
size_t page_size = 100;
39+
if (ctx.flags.count("pagesize")) {
40+
page_size = std::stoul(ctx.flags.at("pagesize"));
41+
} else if (ctx.flags.count("ps")) {
42+
page_size = std::stoul(ctx.flags.at("ps"));
43+
}
44+
45+
int threads = 1;
46+
if (ctx.flags.count("threads")) {
47+
threads = std::stoi(ctx.flags.at("threads"));
48+
} else if (ctx.flags.count("t")) {
49+
threads = std::stoi(ctx.flags.at("t"));
50+
} else if (std::thread::hardware_concurrency() > 0) {
51+
threads = static_cast<int>(std::thread::hardware_concurrency());
52+
}
53+
Logger::Log(LOG_INFO, "Starting server for folder: " + folder + " on port " + std::to_string(port) + " with page size " + std::to_string(page_size) + " and thread pool size " + std::to_string(threads));
3854

39-
Logger::Log(LOG_INFO, "Starting server for folder: " + folder + " on port " + std::to_string(port));
40-
41-
return start_server(folder, port);
55+
return start_server(folder, port, page_size, threads);
4256
}
4357
};
4458

func/server/server.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
#include <csignal>
1616
#include <atomic>
1717
#include <thread>
18+
1819
namespace fs = std::filesystem;
1920

2021
namespace {
2122
// Global server pointer for signal handling
2223
std::atomic<httplib::Server*> g_server_ptr{nullptr};
23-
unsigned int n = std::thread::hardware_concurrency();
2424

2525
// Configuration
2626
struct Config {
2727
bool show_hidden = false;
2828
size_t page_size = 100;
29-
int thread_pool_size = n;
29+
int thread_pool_size = 1;
3030
bool follow_symlinks = false;
3131
} g_config;
3232

@@ -202,7 +202,7 @@ namespace {
202202
return html.str();
203203
}
204204

205-
// Parse query parameters
205+
// Parse query parameters
206206
std::unordered_map<std::string, std::string> parse_query(const std::string& query) {
207207
std::unordered_map<std::string, std::string> params;
208208
if (query.empty()) return params;
@@ -449,7 +449,13 @@ namespace {
449449
}
450450
}
451451

452-
int start_server(const std::string& root_path, int port) {
452+
// Updated signature to accept page_size and thread_pool_size
453+
int start_server(const std::string& root_path, int port, size_t page_size, int thread_pool_size) {
454+
// Update global config
455+
g_config.page_size = page_size;
456+
// Ensure at least 1 thread
457+
g_config.thread_pool_size = (thread_pool_size > 0) ? thread_pool_size : 1;
458+
453459
httplib::Server svr;
454460

455461
// Setup signal handlers for graceful shutdown
@@ -572,11 +578,11 @@ int start_server(const std::string& root_path, int port) {
572578
return;
573579
}
574580

575-
auto query_params = parse_query(req.get_header_value("Query-String"));
576-
// Also parse from URL if httplib doesn't provide it
577-
size_t query_pos = req.path.find('?');
578-
if (query_pos != std::string::npos) {
579-
query_params = parse_query(req.path.substr(query_pos + 1));
581+
// FIX: httplib parses query strings into req.params automatically.
582+
// We use req.params instead of manually parsing req.path.
583+
std::unordered_map<std::string, std::string> query_params;
584+
for (const auto& param : req.params) {
585+
query_params[param.first] = param.second;
580586
}
581587

582588
serve_directory(p, url_path, query_params, res);

func/server/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
* @param port The port to listen on.
99
* @return int 0 on success, non-zero on failure.
1010
*/
11-
int start_server(const std::string& root_path, int port);
11+
int start_server(const std::string& root_path, int port, size_t page_size, int thread_pool_size);

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#ifndef VERSION_H
22
#define VERSION_H
3-
#define VERSION "0.7.0-alpha-1"
3+
#define VERSION "0.7.0-alpha-2"
44
#endif

0 commit comments

Comments
 (0)