@@ -102,6 +102,12 @@ struct server_task_result {
102102 bool error;
103103};
104104
105+ struct server_static_file {
106+ const unsigned char * data;
107+ unsigned int size;
108+ const char * mime_type;
109+ };
110+
105111struct slot_params {
106112 bool stream = true ;
107113 bool cache_prompt = false ; // remember the prompt to avoid reprocessing all prompt
@@ -2254,6 +2260,16 @@ int main(int argc, char ** argv) {
22542260 LOG_INF (" %s\n " , common_params_get_system_info (params).c_str ());
22552261 LOG_INF (" \n " );
22562262
2263+ // static files
2264+ std::map<std::string, server_static_file> static_files = {
2265+ { " /" , { index_html, index_html_len, " text/html; charset=utf-8" }},
2266+ { " /completion.js" , { completion_js, completion_js_len, " text/javascript; charset=utf-8" }},
2267+ { " /deps_daisyui.min.css" , { deps_daisyui_min_css, deps_daisyui_min_css_len, " text/css; charset=utf-8" }},
2268+ { " /deps_markdown-it.js" , { deps_markdown_it_js, deps_markdown_it_js_len, " text/javascript; charset=utf-8" }},
2269+ { " /deps_tailwindcss.js" , { deps_tailwindcss_js, deps_tailwindcss_js_len, " text/javascript; charset=utf-8" }},
2270+ { " /deps_vue.esm-browser.js" , { deps_vue_esm_browser_js, deps_vue_esm_browser_js_len, " text/javascript; charset=utf-8" }},
2271+ };
2272+
22572273 std::unique_ptr<httplib::Server> svr;
22582274#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
22592275 if (params.ssl_file_key != " " && params.ssl_file_cert != " " ) {
@@ -2334,7 +2350,7 @@ int main(int argc, char ** argv) {
23342350 // Middlewares
23352351 //
23362352
2337- auto middleware_validate_api_key = [¶ms, &res_error](const httplib::Request & req, httplib::Response & res) {
2353+ auto middleware_validate_api_key = [¶ms, &res_error, &static_files ](const httplib::Request & req, httplib::Response & res) {
23382354 static const std::unordered_set<std::string> public_endpoints = {
23392355 " /health" ,
23402356 " /models" ,
@@ -2346,8 +2362,8 @@ int main(int argc, char ** argv) {
23462362 return true ;
23472363 }
23482364
2349- // If path is public, skip validation
2350- if (public_endpoints.find (req.path ) != public_endpoints.end ()) {
2365+ // If path is public or is static file , skip validation
2366+ if (public_endpoints.find (req.path ) != public_endpoints.end () || static_files. find (req. path ) != static_files. end () ) {
23512367 return true ;
23522368 }
23532369
@@ -3091,13 +3107,6 @@ int main(int argc, char ** argv) {
30913107 res.status = 200 ; // HTTP OK
30923108 };
30933109
3094- auto handle_static_file = [](unsigned char * content, size_t len, const char * mime_type) {
3095- return [content, len, mime_type](const httplib::Request &, httplib::Response & res) {
3096- res.set_content (reinterpret_cast <const char *>(content), len, mime_type);
3097- return false ;
3098- };
3099- };
3100-
31013110 //
31023111 // Router
31033112 //
@@ -3112,12 +3121,13 @@ int main(int argc, char ** argv) {
31123121 }
31133122 } else {
31143123 // using embedded static files
3115- svr->Get (" /" , handle_static_file (index_html, index_html_len, " text/html; charset=utf-8" ));
3116- svr->Get (" /completion.js" , handle_static_file (completion_js, completion_js_len, " text/javascript; charset=utf-8" ));
3117- svr->Get (" /deps_daisyui.min.css" , handle_static_file (deps_daisyui_min_css, deps_daisyui_min_css_len, " text/css; charset=utf-8" ));
3118- svr->Get (" /deps_markdown-it.js" , handle_static_file (deps_markdown_it_js, deps_markdown_it_js_len, " text/javascript; charset=utf-8" ));
3119- svr->Get (" /deps_tailwindcss.js" , handle_static_file (deps_tailwindcss_js, deps_tailwindcss_js_len, " text/javascript; charset=utf-8" ));
3120- svr->Get (" /deps_vue.esm-browser.js" , handle_static_file (deps_vue_esm_browser_js, deps_vue_esm_browser_js_len, " text/javascript; charset=utf-8" ));
3124+ for (const auto & it : static_files) {
3125+ const server_static_file & static_file = it.second ;
3126+ svr->Get (it.first .c_str (), [&static_file](const httplib::Request &, httplib::Response & res) {
3127+ res.set_content (reinterpret_cast <const char *>(static_file.data ), static_file.size , static_file.mime_type );
3128+ return false ;
3129+ });
3130+ }
31213131 }
31223132
31233133 // register API routes
0 commit comments