Spaces:
Sleeping
Sleeping
server : add request path option(#1741)
Browse files
examples/server/server.cpp
CHANGED
|
@@ -40,6 +40,7 @@ struct server_params
|
|
| 40 |
{
|
| 41 |
std::string hostname = "127.0.0.1";
|
| 42 |
std::string public_path = "examples/server/public";
|
|
|
|
| 43 |
|
| 44 |
int32_t port = 8080;
|
| 45 |
int32_t read_timeout = 600;
|
|
@@ -161,6 +162,7 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
|
|
| 161 |
fprintf(stderr, " --host HOST, [%-7s] Hostname/ip-adress for the server\n", sparams.hostname.c_str());
|
| 162 |
fprintf(stderr, " --port PORT, [%-7d] Port number for the server\n", sparams.port);
|
| 163 |
fprintf(stderr, " --public PATH, [%-7s] Path to the public folder\n", sparams.public_path.c_str());
|
|
|
|
| 164 |
fprintf(stderr, " --convert, [%-7s] Convert audio to WAV, requires ffmpeg on the server", sparams.ffmpeg_converter ? "true" : "false");
|
| 165 |
fprintf(stderr, "\n");
|
| 166 |
}
|
|
@@ -208,6 +210,7 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params, serve
|
|
| 208 |
else if ( arg == "--port") { sparams.port = std::stoi(argv[++i]); }
|
| 209 |
else if ( arg == "--host") { sparams.hostname = argv[++i]; }
|
| 210 |
else if ( arg == "--public") { sparams.public_path = argv[++i]; }
|
|
|
|
| 211 |
else if ( arg == "--convert") { sparams.ffmpeg_converter = true; }
|
| 212 |
else {
|
| 213 |
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
|
@@ -480,12 +483,12 @@ int main(int argc, char ** argv) {
|
|
| 480 |
std::string const default_content = "<html>hello</html>";
|
| 481 |
|
| 482 |
// this is only called if no index.html is found in the public --path
|
| 483 |
-
svr.Get("/", [&default_content](const Request &, Response &res){
|
| 484 |
res.set_content(default_content, "text/html");
|
| 485 |
return false;
|
| 486 |
});
|
| 487 |
|
| 488 |
-
svr.Post("/inference", [&](const Request &req, Response &res){
|
| 489 |
// acquire whisper model mutex lock
|
| 490 |
whisper_mutex.lock();
|
| 491 |
|
|
@@ -722,7 +725,7 @@ int main(int argc, char ** argv) {
|
|
| 722 |
// return whisper model mutex lock
|
| 723 |
whisper_mutex.unlock();
|
| 724 |
});
|
| 725 |
-
svr.Post("/load", [&](const Request &req, Response &res){
|
| 726 |
whisper_mutex.lock();
|
| 727 |
if (!req.has_file("model"))
|
| 728 |
{
|
|
@@ -778,11 +781,11 @@ int main(int argc, char ** argv) {
|
|
| 778 |
res.status = 500;
|
| 779 |
});
|
| 780 |
|
| 781 |
-
svr.set_error_handler([](const Request
|
| 782 |
if (res.status == 400) {
|
| 783 |
res.set_content("Invalid request", "text/plain");
|
| 784 |
} else if (res.status != 500) {
|
| 785 |
-
res.set_content("File Not Found", "text/plain");
|
| 786 |
res.status = 404;
|
| 787 |
}
|
| 788 |
});
|
|
|
|
| 40 |
{
|
| 41 |
std::string hostname = "127.0.0.1";
|
| 42 |
std::string public_path = "examples/server/public";
|
| 43 |
+
std::string request_path = "";
|
| 44 |
|
| 45 |
int32_t port = 8080;
|
| 46 |
int32_t read_timeout = 600;
|
|
|
|
| 162 |
fprintf(stderr, " --host HOST, [%-7s] Hostname/ip-adress for the server\n", sparams.hostname.c_str());
|
| 163 |
fprintf(stderr, " --port PORT, [%-7d] Port number for the server\n", sparams.port);
|
| 164 |
fprintf(stderr, " --public PATH, [%-7s] Path to the public folder\n", sparams.public_path.c_str());
|
| 165 |
+
fprintf(stderr, " --request-path PATH, [%-7s] Request path for all requests\n", sparams.request_path.c_str());
|
| 166 |
fprintf(stderr, " --convert, [%-7s] Convert audio to WAV, requires ffmpeg on the server", sparams.ffmpeg_converter ? "true" : "false");
|
| 167 |
fprintf(stderr, "\n");
|
| 168 |
}
|
|
|
|
| 210 |
else if ( arg == "--port") { sparams.port = std::stoi(argv[++i]); }
|
| 211 |
else if ( arg == "--host") { sparams.hostname = argv[++i]; }
|
| 212 |
else if ( arg == "--public") { sparams.public_path = argv[++i]; }
|
| 213 |
+
else if ( arg == "--request-path") { sparams.request_path = argv[++i]; }
|
| 214 |
else if ( arg == "--convert") { sparams.ffmpeg_converter = true; }
|
| 215 |
else {
|
| 216 |
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
|
|
|
| 483 |
std::string const default_content = "<html>hello</html>";
|
| 484 |
|
| 485 |
// this is only called if no index.html is found in the public --path
|
| 486 |
+
svr.Get(sparams.request_path + "/", [&default_content](const Request &, Response &res){
|
| 487 |
res.set_content(default_content, "text/html");
|
| 488 |
return false;
|
| 489 |
});
|
| 490 |
|
| 491 |
+
svr.Post(sparams.request_path + "/inference", [&](const Request &req, Response &res){
|
| 492 |
// acquire whisper model mutex lock
|
| 493 |
whisper_mutex.lock();
|
| 494 |
|
|
|
|
| 725 |
// return whisper model mutex lock
|
| 726 |
whisper_mutex.unlock();
|
| 727 |
});
|
| 728 |
+
svr.Post(sparams.request_path + "/load", [&](const Request &req, Response &res){
|
| 729 |
whisper_mutex.lock();
|
| 730 |
if (!req.has_file("model"))
|
| 731 |
{
|
|
|
|
| 781 |
res.status = 500;
|
| 782 |
});
|
| 783 |
|
| 784 |
+
svr.set_error_handler([](const Request &req, Response &res) {
|
| 785 |
if (res.status == 400) {
|
| 786 |
res.set_content("Invalid request", "text/plain");
|
| 787 |
} else if (res.status != 500) {
|
| 788 |
+
res.set_content("File Not Found (" + req.path + ")", "text/plain");
|
| 789 |
res.status = 404;
|
| 790 |
}
|
| 791 |
});
|