@@ -3546,7 +3546,7 @@ static void log_server_request(const httplib::Request & req, const httplib::Resp
3546
3546
std::function<void (int )> shutdown_handler;
3547
3547
std::atomic_flag is_terminating = ATOMIC_FLAG_INIT;
3548
3548
3549
- inline void signal_handler (int signal) {
3549
+ static inline void signal_handler (int signal) {
3550
3550
if (is_terminating.test_and_set ()) {
3551
3551
// in case it hangs, we can force terminate the server by hitting Ctrl+C twice
3552
3552
// 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) {
3557
3557
shutdown_handler (signal );
3558
3558
}
3559
3559
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) {
3573
3561
llama_backend_init ();
3574
3562
llama_numa_init (params.numa );
3575
3563
@@ -3578,25 +3566,51 @@ int main(int argc, char ** argv) {
3578
3566
LOG_INF (" %s\n " , common_params_get_system_info (params).c_str ());
3579
3567
LOG_INF (" \n " );
3580
3568
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
+
3582
3575
#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 ()));
3588
3579
} 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 ());
3591
3582
}
3592
3583
#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 ;
3596
3587
}
3597
- svr .reset (new httplib::Server ());
3588
+ server .reset (new httplib::Server ());
3598
3589
#endif
3599
3590
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
+
3600
3614
std::atomic<server_state> state{SERVER_STATE_LOADING_MODEL};
3601
3615
3602
3616
svr->set_default_headers ({{" Server" , " llama.cpp" }});
0 commit comments