Skip to content

Commit de5dd79

Browse files
committed
Load user-provided front-ends
1 parent 968025d commit de5dd79

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

examples/server/main.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ struct SDParams {
181181
// server things
182182
int port = 8080;
183183
std::string host = "127.0.0.1";
184+
185+
std::string custom_frontend_path = "";
184186
};
185187

186188
void print_params(SDParams params) {
@@ -525,7 +527,9 @@ void parse_args(int argc, const char** argv, SDParams& params) {
525527
{"", "--control-net-dir", "path to controlnet models directory", &params.controlnet_dir},
526528
{"", "--photo-maker-dir", "path to PHOTOMAKER models directory", &params.photomaker_dir},
527529
{"", "--upscaler-dir", "path to upscaler models directory", &params.upscaler_dir},
528-
{"", "--host", "host to listen on (default: 0.0.0.0)", &params.host}};
530+
{"", "--host", "host to listen on (default: 0.0.0.0)", &params.host},
531+
{"", "--custom-frontend-path", "path to custom frontend directory", &params.custom_frontend_path},
532+
};
529533

530534
options.int_options = {
531535
{"-t", "--threads", "number of threads to use during computation (default: -1). If threads <= 0, then threads will be set to the number of CPU physical cores", &params.ctxParams.n_threads},
@@ -2489,8 +2493,31 @@ void start_server(SDParams params) {
24892493
});
24902494

24912495
// redirect base url to index
2492-
svr->Get("/", [](const httplib::Request& req, httplib::Response& res) {
2493-
res.set_redirect("/index.html");
2496+
svr->Get("/", [&params](const httplib::Request& req, httplib::Response& res) {
2497+
if (params.custom_frontend_path != "") {
2498+
res.set_redirect("/frontend-custom.html");
2499+
} else {
2500+
res.set_redirect("/index.html");
2501+
}
2502+
});
2503+
2504+
svr->Get("/frontend-custom.html", [&params](const httplib::Request& req, httplib::Response& res) {
2505+
try {
2506+
std::string def_frontend_path = params.custom_frontend_path;
2507+
std::string html = "";
2508+
std::ifstream file(def_frontend_path);
2509+
if (file.is_open()) {
2510+
std::stringstream buffer;
2511+
buffer << file.rdbuf();
2512+
html = buffer.str();
2513+
file.close();
2514+
} else {
2515+
html = "Error: Unable to open file " + def_frontend_path;
2516+
}
2517+
res.set_content(html, "text/html");
2518+
} catch (const std::exception& e) {
2519+
res.set_content("Error loading page", "text/plain");
2520+
}
24942521
});
24952522

24962523
// bind HTTP listen port, run the HTTP server in a thread

0 commit comments

Comments
 (0)