@@ -3546,7 +3546,7 @@ static void log_server_request(const httplib::Request & req, const httplib::Resp
35463546std::function<void (int )> shutdown_handler;
35473547std::atomic_flag is_terminating = ATOMIC_FLAG_INIT;
35483548
3549- inline void signal_handler (int signal) {
3549+ static inline void signal_handler (int signal) {
35503550 if (is_terminating.test_and_set ()) {
35513551 // in case it hangs, we can force terminate the server by hitting Ctrl+C twice
35523552 // 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) {
35573557 shutdown_handler (signal);
35583558}
35593559
3560- int main (int argc, char ** argv) {
3561- // own arguments required by this example
3562- common_params params;
3563-
3564- if (!common_params_parse (argc, argv, params, LLAMA_EXAMPLE_SERVER)) {
3565- return 1 ;
3566- }
3567-
3568- common_init ();
3569-
3570- // struct that contains llama context and inference
3571- server_context ctx_server;
3572-
3560+ static bool initialize_server_context (common_params & params) {
35733561 llama_backend_init ();
35743562 llama_numa_init (params.numa );
35753563
@@ -3578,25 +3566,51 @@ int main(int argc, char ** argv) {
35783566 LOG_INF (" %s\n " , common_params_get_system_info (params).c_str ());
35793567 LOG_INF (" \n " );
35803568
3581- std::unique_ptr<httplib::Server> svr;
3569+ return true ;
3570+ }
3571+
3572+ static std::unique_ptr<httplib::Server> setup_server (common_params & params) {
3573+ std::unique_ptr<httplib::Server> server;
3574+
35823575#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3583- if (params.ssl_file_key != " " && params.ssl_file_cert != " " ) {
3584- LOG_INF (" Running with SSL: key = %s, cert = %s\n " , params.ssl_file_key .c_str (), params.ssl_file_cert .c_str ());
3585- svr.reset (
3586- new httplib::SSLServer (params.ssl_file_cert .c_str (), params.ssl_file_key .c_str ())
3587- );
3576+ if (!params.ssl_file_key .empty () && !params.ssl_file_cert .empty ()) {
3577+ LOG_INF (" Running with SSL: key = %s, cert = %s" , params.ssl_file_key .c_str (), params.ssl_file_cert .c_str ());
3578+ server.reset (new httplib::SSLServer (params.ssl_file_cert .c_str (), params.ssl_file_key .c_str ()));
35883579 } else {
3589- LOG_INF (" Running without SSL\n " );
3590- svr .reset (new httplib::Server ());
3580+ LOG_INF (" Running without SSL" );
3581+ server .reset (new httplib::Server ());
35913582 }
35923583#else
3593- if (params.ssl_file_key != " " && params.ssl_file_cert != " " ) {
3594- LOG_ERR (" Server is built without SSL support\n " );
3595- return 1 ;
3584+ if (! params.ssl_file_key . empty () || ! params.ssl_file_cert . empty () ) {
3585+ LOG_ERR (" Server is built without SSL support" );
3586+ return nullptr ;
35963587 }
3597- svr .reset (new httplib::Server ());
3588+ server .reset (new httplib::Server ());
35983589#endif
35993590
3591+ return server;
3592+ }
3593+
3594+ int main (int argc, char ** argv) {
3595+ // Parse and initialize common parameters
3596+ common_params params;
3597+ if (!common_params_parse (argc, argv, params, LLAMA_EXAMPLE_SERVER)) {
3598+ return 1 ;
3599+ }
3600+ common_init ();
3601+
3602+ // Initialize server context
3603+ server_context ctx_server;
3604+ if (!initialize_server_context (params)) {
3605+ return 1 ;
3606+ }
3607+
3608+ // Setup server and configure properties
3609+ std::unique_ptr<httplib::Server> svr = setup_server (params);
3610+ if (!svr) {
3611+ return 1 ;
3612+ }
3613+
36003614 std::atomic<server_state> state{SERVER_STATE_LOADING_MODEL};
36013615
36023616 svr->set_default_headers ({{" Server" , " llama.cpp" }});
0 commit comments