Skip to content

Commit 244bee4

Browse files
eschmidbauerjiahansu
authored andcommitted
server : add request path option(ggml-org#1741)
1 parent fe2d657 commit 244bee4

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

examples/server/server.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct server_params
4040
{
4141
std::string hostname = "127.0.0.1";
4242
std::string public_path = "examples/server/public";
43+
std::string request_path = "";
4344

4445
int32_t port = 8080;
4546
int32_t read_timeout = 600;
@@ -161,6 +162,7 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
161162
fprintf(stderr, " --host HOST, [%-7s] Hostname/ip-adress for the server\n", sparams.hostname.c_str());
162163
fprintf(stderr, " --port PORT, [%-7d] Port number for the server\n", sparams.port);
163164
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());
164166
fprintf(stderr, " --convert, [%-7s] Convert audio to WAV, requires ffmpeg on the server", sparams.ffmpeg_converter ? "true" : "false");
165167
fprintf(stderr, "\n");
166168
}
@@ -208,6 +210,7 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params, serve
208210
else if ( arg == "--port") { sparams.port = std::stoi(argv[++i]); }
209211
else if ( arg == "--host") { sparams.hostname = argv[++i]; }
210212
else if ( arg == "--public") { sparams.public_path = argv[++i]; }
213+
else if ( arg == "--request-path") { sparams.request_path = argv[++i]; }
211214
else if ( arg == "--convert") { sparams.ffmpeg_converter = true; }
212215
else {
213216
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
@@ -480,12 +483,12 @@ int main(int argc, char ** argv) {
480483
std::string const default_content = "<html>hello</html>";
481484

482485
// this is only called if no index.html is found in the public --path
483-
svr.Get("/", [&default_content](const Request &, Response &res){
486+
svr.Get(sparams.request_path + "/", [&default_content](const Request &, Response &res){
484487
res.set_content(default_content, "text/html");
485488
return false;
486489
});
487490

488-
svr.Post("/inference", [&](const Request &req, Response &res){
491+
svr.Post(sparams.request_path + "/inference", [&](const Request &req, Response &res){
489492
// acquire whisper model mutex lock
490493
whisper_mutex.lock();
491494

@@ -722,7 +725,7 @@ int main(int argc, char ** argv) {
722725
// return whisper model mutex lock
723726
whisper_mutex.unlock();
724727
});
725-
svr.Post("/load", [&](const Request &req, Response &res){
728+
svr.Post(sparams.request_path + "/load", [&](const Request &req, Response &res){
726729
whisper_mutex.lock();
727730
if (!req.has_file("model"))
728731
{
@@ -778,11 +781,11 @@ int main(int argc, char ** argv) {
778781
res.status = 500;
779782
});
780783

781-
svr.set_error_handler([](const Request &, Response &res) {
784+
svr.set_error_handler([](const Request &req, Response &res) {
782785
if (res.status == 400) {
783786
res.set_content("Invalid request", "text/plain");
784787
} else if (res.status != 500) {
785-
res.set_content("File Not Found", "text/plain");
788+
res.set_content("File Not Found (" + req.path + ")", "text/plain");
786789
res.status = 404;
787790
}
788791
});

0 commit comments

Comments
 (0)