From 139cdfc0de5e8b5d9989473867788097b19ddb36 Mon Sep 17 00:00:00 2001 From: Behnam M <58621210+ibehnam@users.noreply.github.com> Date: Wed, 10 Jan 2024 00:24:32 -0500 Subject: [PATCH] Update server.cpp --- examples/server/server.cpp | 43 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/examples/server/server.cpp b/examples/server/server.cpp index 29c571cd8..654630eb3 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -2801,6 +2801,28 @@ int main(int argc, char **argv) httplib::Server svr; + svr.set_default_headers({{"Server", "llama.cpp"}, + {"Access-Control-Allow-Origin", "*"}, + {"Access-Control-Allow-Headers", "content-type"}}); + + svr.Get("/health", [&](const httplib::Request&, httplib::Response& res) { + switch(server_state) { + case READY: + res.set_content(R"({"status": "ok"})", "application/json"); + res.status = 200; // HTTP OK + break; + case LOADING_MODEL: + res.set_content(R"({"status": "loading model"})", "application/json"); + res.status = 503; // HTTP Service Unavailable + break; + case ERROR: + res.set_content(R"({"status": "error", "error": "Model failed to load"})", "application/json"); + res.status = 500; // HTTP Internal Server Error + break; + } + }); + + ServerState server_state = LOADING_MODEL; // load the model if (!llama.load_model(params)) @@ -2839,9 +2861,6 @@ int main(int argc, char **argv) return false; }; - svr.set_default_headers({{"Server", "llama.cpp"}, - {"Access-Control-Allow-Origin", "*"}, - {"Access-Control-Allow-Headers", "content-type"}}); // this is only called if no index.html is found in the public --path svr.Get("/", [](const httplib::Request &, httplib::Response &res) @@ -2951,24 +2970,6 @@ int main(int argc, char **argv) }); - svr.Get("/health", [&](const httplib::Request&, httplib::Response& res) { - switch(server_state) { - case READY: - res.set_content(R"({"status": "ok"})", "application/json"); - res.status = 200; // HTTP OK - break; - case LOADING_MODEL: - res.set_content(R"({"status": "loading model"})", "application/json"); - res.status = 503; // HTTP Service Unavailable - break; - case ERROR: - res.set_content(R"({"status": "error", "error": "Model failed to load"})", "application/json"); - res.status = 500; // HTTP Internal Server Error - break; - } - }); - - svr.Get("/v1/models", [¶ms](const httplib::Request&, httplib::Response& res) { std::time_t t = std::time(0);