mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-01-02 18:53:36 +00:00
feat: add --serve-html-path option to example server (#1123)
This commit is contained in:
parent
3e812460cf
commit
96fcb13fc0
@ -6,6 +6,7 @@ usage: ./bin/sd-server [options]
|
|||||||
Svr Options:
|
Svr Options:
|
||||||
-l, --listen-ip <string> server listen ip (default: 127.0.0.1)
|
-l, --listen-ip <string> server listen ip (default: 127.0.0.1)
|
||||||
--listen-port <int> server listen port (default: 1234)
|
--listen-port <int> server listen port (default: 1234)
|
||||||
|
--serve-html-path <string> path to HTML file to serve at root (optional)
|
||||||
-v, --verbose print extra info
|
-v, --verbose print extra info
|
||||||
--color colors the logging tags according to level
|
--color colors the logging tags according to level
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
|
|||||||
@ -104,6 +104,7 @@ std::string iso_timestamp_now() {
|
|||||||
struct SDSvrParams {
|
struct SDSvrParams {
|
||||||
std::string listen_ip = "127.0.0.1";
|
std::string listen_ip = "127.0.0.1";
|
||||||
int listen_port = 1234;
|
int listen_port = 1234;
|
||||||
|
std::string serve_html_path;
|
||||||
bool normal_exit = false;
|
bool normal_exit = false;
|
||||||
bool verbose = false;
|
bool verbose = false;
|
||||||
bool color = false;
|
bool color = false;
|
||||||
@ -115,7 +116,11 @@ struct SDSvrParams {
|
|||||||
{"-l",
|
{"-l",
|
||||||
"--listen-ip",
|
"--listen-ip",
|
||||||
"server listen ip (default: 127.0.0.1)",
|
"server listen ip (default: 127.0.0.1)",
|
||||||
&listen_ip}};
|
&listen_ip},
|
||||||
|
{"",
|
||||||
|
"--serve-html-path",
|
||||||
|
"path to HTML file to serve at root (optional)",
|
||||||
|
&serve_html_path}};
|
||||||
|
|
||||||
options.int_options = {
|
options.int_options = {
|
||||||
{"",
|
{"",
|
||||||
@ -159,6 +164,11 @@ struct SDSvrParams {
|
|||||||
LOG_ERROR("error: listen_port should be in the range [0, 65535]");
|
LOG_ERROR("error: listen_port should be in the range [0, 65535]");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!serve_html_path.empty() && !fs::exists(serve_html_path)) {
|
||||||
|
LOG_ERROR("error: serve_html_path file does not exist: %s", serve_html_path.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,6 +177,7 @@ struct SDSvrParams {
|
|||||||
oss << "SDSvrParams {\n"
|
oss << "SDSvrParams {\n"
|
||||||
<< " listen_ip: " << listen_ip << ",\n"
|
<< " listen_ip: " << listen_ip << ",\n"
|
||||||
<< " listen_port: \"" << listen_port << "\",\n"
|
<< " listen_port: \"" << listen_port << "\",\n"
|
||||||
|
<< " serve_html_path: \"" << serve_html_path << "\",\n"
|
||||||
<< "}";
|
<< "}";
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
@ -312,7 +323,18 @@ int main(int argc, const char** argv) {
|
|||||||
|
|
||||||
// health
|
// health
|
||||||
svr.Get("/", [&](const httplib::Request&, httplib::Response& res) {
|
svr.Get("/", [&](const httplib::Request&, httplib::Response& res) {
|
||||||
res.set_content(R"({"ok":true,"service":"sd-cpp-http"})", "application/json");
|
if (!svr_params.serve_html_path.empty()) {
|
||||||
|
std::ifstream file(svr_params.serve_html_path);
|
||||||
|
if (file) {
|
||||||
|
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||||
|
res.set_content(content, "text/html");
|
||||||
|
} else {
|
||||||
|
res.status = 500;
|
||||||
|
res.set_content("Error: Unable to read HTML file", "text/plain");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.set_content("Stable Diffusion Server is running", "text/plain");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// models endpoint (minimal)
|
// models endpoint (minimal)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user