Skip to content

Break down main function in llama-server #13425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions tools/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3546,7 +3546,7 @@ static void log_server_request(const httplib::Request & req, const httplib::Resp
std::function<void(int)> shutdown_handler;
std::atomic_flag is_terminating = ATOMIC_FLAG_INIT;

inline void signal_handler(int signal) {
static inline void signal_handler(int signal) {
if (is_terminating.test_and_set()) {
// in case it hangs, we can force terminate the server by hitting Ctrl+C twice
// this is for better developer experience, we can remove when the server is stable enough
Expand All @@ -3557,19 +3557,7 @@ inline void signal_handler(int signal) {
shutdown_handler(signal);
}

int main(int argc, char ** argv) {
// own arguments required by this example
common_params params;

if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_SERVER)) {
return 1;
}

common_init();

// struct that contains llama context and inference
server_context ctx_server;

static bool initialize_server_context(common_params & params) {
llama_backend_init();
llama_numa_init(params.numa);

Expand All @@ -3578,25 +3566,51 @@ int main(int argc, char ** argv) {
LOG_INF("%s\n", common_params_get_system_info(params).c_str());
LOG_INF("\n");

std::unique_ptr<httplib::Server> svr;
return true;
}

static std::unique_ptr<httplib::Server> setup_server(common_params & params) {
std::unique_ptr<httplib::Server> server;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
if (params.ssl_file_key != "" && params.ssl_file_cert != "") {
LOG_INF("Running with SSL: key = %s, cert = %s\n", params.ssl_file_key.c_str(), params.ssl_file_cert.c_str());
svr.reset(
new httplib::SSLServer(params.ssl_file_cert.c_str(), params.ssl_file_key.c_str())
);
if (!params.ssl_file_key.empty() && !params.ssl_file_cert.empty()) {
LOG_INF("Running with SSL: key = %s, cert = %s", params.ssl_file_key.c_str(), params.ssl_file_cert.c_str());
server.reset(new httplib::SSLServer(params.ssl_file_cert.c_str(), params.ssl_file_key.c_str()));
} else {
LOG_INF("Running without SSL\n");
svr.reset(new httplib::Server());
LOG_INF("Running without SSL");
server.reset(new httplib::Server());
}
#else
if (params.ssl_file_key != "" && params.ssl_file_cert != "") {
LOG_ERR("Server is built without SSL support\n");
return 1;
if (!params.ssl_file_key.empty() || !params.ssl_file_cert.empty()) {
LOG_ERR("Server is built without SSL support");
return nullptr;
}
svr.reset(new httplib::Server());
server.reset(new httplib::Server());
#endif

return server;
}

int main(int argc, char ** argv) {
// Parse and initialize common parameters
common_params params;
if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_SERVER)) {
return 1;
}
common_init();

// Initialize server context
server_context ctx_server;
if (!initialize_server_context(params)) {
return 1;
}

// Setup server and configure properties
std::unique_ptr<httplib::Server> svr = setup_server(params);
if (!svr) {
return 1;
}

std::atomic<server_state> state{SERVER_STATE_LOADING_MODEL};

svr->set_default_headers({{"Server", "llama.cpp"}});
Expand Down
Loading