diff --git a/tools/server/server.cpp b/tools/server/server.cpp index de8ded71fd6ad..183f0eeb8b8da 100644 --- a/tools/server/server.cpp +++ b/tools/server/server.cpp @@ -3546,7 +3546,7 @@ static void log_server_request(const httplib::Request & req, const httplib::Resp std::function 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 @@ -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); @@ -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 svr; + return true; +} + +static std::unique_ptr setup_server(common_params & params) { + std::unique_ptr 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 svr = setup_server(params); + if (!svr) { + return 1; + } + std::atomic state{SERVER_STATE_LOADING_MODEL}; svr->set_default_headers({{"Server", "llama.cpp"}});